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
TypeScript Version: 2.2.2
Compile with StrictNullChecks enabled.
Code
classFoo{publicbar: string="";}functiontest(){letfoo: Foo|null=null;[1].forEach((item)=>{foo=newFoo();});// Here Typescript compiler incorrectly thinks that 'foo' has null type // although it will always be an instance of Foo class.if(foo){// The next line does not compile because 'foo' variable has never type here.foo.bar;}}
Expected behavior:
Should compile. Actual behavior:
Does not compile. The compiler incorrectly narrows the type of foo variable to null type as it seems to ignore the callback given to the forEach function call, which actually will always be called.
Notes:
The code compiles without StrictNullChecks just fine.
The below code works as expected, and I believe the same would be the expected behavior for the example code as well:
classFoo{publicbar: string="";}functiontest(){letfoo: Foo|null=null;// When calling a simple lambda explicitly this works as expected(()=>{foo=newFoo();})();if(foo){foo.bar;}}
The text was updated successfully, but these errors were encountered:
TypeScript Version: 2.2.2
Compile with StrictNullChecks enabled.
Code
Expected behavior:
Should compile.
Actual behavior:
Does not compile. The compiler incorrectly narrows the type of foo variable to null type as it seems to ignore the callback given to the forEach function call, which actually will always be called.
Notes:
The code compiles without StrictNullChecks just fine.
The below code works as expected, and I believe the same would be the expected behavior for the example code as well:
The text was updated successfully, but these errors were encountered: