You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Type is lost when result of type guard is assigned and strictNullChecks is on #39657
// @ts-check// NOTE: Problem only happens when strictNullChecks is on. // repro: https://www.typescriptlang.org/play?strictNullChecks=true&ts=3.9.3&useJavaScript=true#/** * * @typedef Thing * @property {number} id *//** * @param {Thing|null} variable * @return {variable is Thing} */constisNotNull=variable=>variable!==null;/** * @param {Thing|null} thing */constfn=thing=>{thing;// as expected, type is Thing | nullif(isNotNull(thing)){thing;// as expected, type is Thingthing.id;// no error}constisAvailable=isNotNull(thing);if(isAvailable){thing;// unexpected: type is Thing | nullthing.id;// error: Object is possibly null}};
Expected behavior:
In the body of the second if, thing will be typed as Thing.
Actual behavior:
In the body of the second if, thing is typed as Thing | null
Activity
MartinJohns commentedon Jul 18, 2020
Duplicate of #12184.
sandinmyjoints commentedon Jul 18, 2020
Well, now I know what should have searched for 😆
typescript-bot commentedon Jul 21, 2020
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.