Closed
Description
TypeScript Version: 2.4.1
Code
Compile with strictNullChecks = true
Playground link: here (make sure to enable strictNullChecks
)
class A {
a: string;
}
let foo: A | undefined = void 0;
try {
foo = new A();
throw new Error();
} catch (e) {
if (foo) {
foo.a; // ERROR: Property 'a' does not exist on type 'never'.
}
}
finally {
if (foo) {
foo.a; // ERROR: Property 'a' does not exist on type 'never'.
}
}
Expected behavior:
Should compile without errors
Actual behavior:
Compilation fails at the indicated lines. Control flow analysis assumes that foo
is of type undefined
as it enters the try
/ catch
/ finally
block, and is unable to detect that foo
will actually be of type A
in both the catch
and finally
blocks.