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
strictNullChecks thinks a value may be null if null is assigned to it in its non-null branch #7630
classProp{value: any;}varx: Prop|null=null;if(x===null){;}elseif(x.value===null){// Line 9x=null;}
tsc -t es5 --strictNullChecks ./foo.ts
Expected behavior:
Compiles.
Actual behavior:
foo.ts(9,10): error TS2531: Object is possibly 'null' or 'undefined'.
If the x = null; assignment (line 10) is removed, it does compile, and x is correctly inferred to be non-null in the condition and body of the else-if block. But this assignment should not have any effect on the nullability of x in the else-if condition.
Im pretty sure the reason is this: Assignment within a block disabled narrowing within it. If so, the solution is likely already in the works: Control flow based type guards (See PR #6959)
Activity
weswigham commentedon Mar 22, 2016
Im pretty sure the reason is this: Assignment within a block disabled narrowing within it. If so, the solution is likely already in the works: Control flow based type guards (See PR #6959)
Arnavion commentedon Mar 22, 2016
Disabling narrowing within the block is one thing, but line 9 is before the block.
RyanCavanaugh commentedon Mar 22, 2016
Assignments anywhere within the scope of a variable disable type guards (for now).
As you've noticed, this makes null checks extremely cumbersome, which is why control flow analysis was bumped up in priority.