-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Control flow based type analysis #8010
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Adds tests to control flow types branch
This was referenced Apr 21, 2016
basarat
added a commit
to alm-tools/alm
that referenced
this pull request
Apr 22, 2016
5 tasks
4 tasks
This was referenced Apr 26, 2016
dokidokivisual
added a commit
to karen-irc/karen
that referenced
this pull request
May 4, 2016
chore(TypeScript): Enable 'strictNullChecks' option This tries to enable [`--strictNullChecks` option](microsoft/TypeScript#7140) of TypeScript compiler. - [Non-nullable types by ahejlsberg · Pull Request #7140 · Microsoft/TypeScript](microsoft/TypeScript#7140) - [Non-strict type checking · Issue #7489 · Microsoft/TypeScript](microsoft/TypeScript#7489) - [[Request for feedback] Nullable types, `null` and `undefined` · Issue #7426 · Microsoft/TypeScript](microsoft/TypeScript#7426) - [Control flow based type analysis by ahejlsberg · Pull Request #8010 · Microsoft/TypeScript](microsoft/TypeScript#8010) <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="35" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/karen-irc/karen/604) <!-- Reviewable:end -->
Just trying out typescript now and working out for to get type check actions in redux. Looks like this feature could be very helpful for this? |
6 tasks
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR introduces control flow based type analysis for local variables and parameters as initially suggested in #2388 and prototyped in #6959. Previously, the type analysis performed for type guards was limited to
if
statements and?:
conditional expressions and didn't include effects of assignments and control flow constructs such asreturn
andbreak
statements. With this PR, the type checker analyses all possible flows of control in statements and expressions to produce the most specific type possible (the narrowed type) at any given location for a local variable or parameter that is declared to have a union type.Some examples:
Control flow based type analysis is particuarly relevant in
--strictNullChecks
mode because nullable types are represented using union types:Furthermore, in
--strictNullChecks
mode, control flow based type analysis includes definite assignment analysis for local variables of types that don't permit the valueundefined
.The narrowed type of a local variable or parameter at a given source code location is computed by starting with the initial type of the variable and then following each possible code path that leads to the given location, narrowing the type of the variable as appropriate based on type guards and assignments.
undefined
.The type T narrowed by S is computed as follows:
Thanks to @ivogabe for providing inspiration and tests for this PR.
Fixes #2388.