-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Closed
Labels
SuggestionAn idea for TypeScriptAn idea for TypeScriptToo ComplexAn issue which adding support for may be too complex for the value it addsAn issue which adding support for may be too complex for the value it adds
Description
TypeScript Version: 2.4.2
Code
// @strictNullChecks: true
let foo: { foo: string; } | undefined;
foo = { foo: 'bar' };
document.addEventListener('click', () => {
foo.foo; // Object is possibly 'undefined'
});
foo.foo;
Currently, TypeScript resets the type of variables within a function scope, giving the error that the variable is possibly undefined
, but it can be statically determined that after the function, the outer scope does no re-assignment. So it is possible to determine that there is no re-assignment and therefore foo
will always be assigned.
Related to #9998 (trade-offs in CFA)
Related to #11498 (annotate immediately invoked callbacks)
Expected behavior:
No errors.
Actual behavior:
Object is possibly 'undefined'
Metadata
Metadata
Assignees
Labels
SuggestionAn idea for TypeScriptAn idea for TypeScriptToo ComplexAn issue which adding support for may be too complex for the value it addsAn issue which adding support for may be too complex for the value it adds