This example showcases the inner workings of PR/11263. Through control flow analysis, TypeScript can determine the type of variable foo
.
let foo;
foo = "bar";
foo = 2;
foo
//^? foo: number
In this scenario, the type of foo
would be identified as number
. Control flow analysis is effective in determining variable types at various references, ensuring no errors are reported even with the --noImplicitAny
flag set.
An illustration involving changing the type of foo
through assignments demonstrates the precision of control flow analysis.
let a: string
let b: number
let foo;
foo = "bar";
a = foo
b = foo // Type 'string' is not assignable to type 'number'
foo = 2;
a = foo // Type 'number' is not assignable to type 'string'
b = foo