GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |
GeezerVison is showing github.com — Control flow based type analysis by ahejlsberg · Pull Request #8010 · microsoft/TypeScript Conversation
Member
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 Some examples: function foo(x: string | number | boolean) { if (typeof x === "string") { x; // type of x is string here x = 1; x; // type of x is number here } x; // type of x is number | boolean here } function bar(x: string | number) { if (typeof x === "number") { return; } x; // type of x is string here } Control flow based type analysis is particuarly relevant in function test(x: string | null) { if (x === null) { return; } x; // type of x is string in remainder of function } Furthermore, in function mumble(check: boolean) { let x: number; // Type doesn't permit undefined x; // Error, x is undefined if (check) { x = 1; x; // Ok } x; // Error, x is possibly undefined x = 2; x; // Ok } 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.
The type T narrowed by S is computed as follows:
Thanks to Ivo Gabe de Wolff (@ivogabe) for providing inspiration and tests for this PR. Fixes #2388.
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page. All reactions
Adds tests to control flow types branch
This was referenced
Basarat Ali Syed (basarat)
added a commit
to alm-tools/alm
that referenced
this pull request
5 tasks done
4 tasks
This was referenced
dokidokivisual commit bot (dokidokivisual)
added a commit
to karen-irc/karen
that referenced
this pull request
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 -->
All reactions
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page.
3 of 6 tasks
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Reviewers1 more reviewerUh oh!
There was an error while loading. Please reload this page. AssigneesNo one assignedLabels
None yet
ProjectsMilestoneNo milestoneDevelopmentSuccessfully merging this pull request may close these issues. Uh oh!
There was an error while loading. Please reload this page. 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. | |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |