Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggestion: CFA, assignment, strict null checks and function scope #17449

Closed
kitsonk opened this issue Jul 27, 2017 · 2 comments · Fixed by #56908
Closed

Suggestion: CFA, assignment, strict null checks and function scope #17449

kitsonk opened this issue Jul 27, 2017 · 2 comments · Fixed by #56908
Labels
Suggestion An idea for TypeScript Too Complex An issue which adding support for may be too complex for the value it adds

Comments

@kitsonk
Copy link
Contributor

kitsonk commented Jul 27, 2017

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'

@DanielRosenwasser
Copy link
Member

We could do wider analysis, but I don't know how computationally expensive that would get.

I personally do think that a solution in the same vein of #11498 would be more ideal, but we haven't explored that very deeply.

@DanielRosenwasser DanielRosenwasser added In Discussion Not yet reached consensus Suggestion An idea for TypeScript labels Jul 28, 2017
@mhegazy mhegazy added Too Complex An issue which adding support for may be too complex for the value it adds and removed In Discussion Not yet reached consensus labels Nov 20, 2017
@mhegazy
Copy link
Contributor

mhegazy commented Nov 20, 2017

As @DanielRosenwasser noted, i do not think we can do the full analysis, where a function body is inlined at the call site for control flow analysis purposes.. #11498 might be a more viable path here.

@mhegazy mhegazy closed this as completed Nov 20, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Suggestion An idea for TypeScript Too Complex An issue which adding support for may be too complex for the value it adds
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants