Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Control flow based type analysis#8010

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
ahejlsberg merged 70 commits intomasterfromcontrolFlowTypes
Apr 22, 2016
Merged

Control flow based type analysis#8010

ahejlsberg merged 70 commits intomasterfromcontrolFlowTypes
Apr 22, 2016

Conversation

ahejlsberg
Copy link
Member

This PR introducescontrol 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 toif 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 (thenarrowed type) at any given location for a local variable or parameter that is declared to have a union type.

Some examples:

functionfoo(x:string|number|boolean){if(typeofx==="string"){x;// type of x is string herex=1;x;// type of x is number here}x;// type of x is number | boolean here}functionbar(x:string|number){if(typeofx==="number"){return;}x;// type of x is string here}

Control flow based type analysis is particuarly relevant in--strictNullChecks mode because nullable types are represented using union types:

functiontest(x:string|null){if(x===null){return;}x;// type of x is string in remainder of function}

Furthermore, in--strictNullChecks mode, control flow based type analysis includesdefinite assignment analysis for local variables of types that don't permit the valueundefined.

functionmumble(check:boolean){letx:number;// Type doesn't permit undefinedx;// Error, x is undefinedif(check){x=1;x;// Ok}x;// Error, x is possibly undefinedx=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 initial type of local variable isundefined.
  • The initial type of a parameter is the declared type of the parameter.
  • The initial type of an outer local variable or a global variable is the declared type of that variable.
  • A type guard narrows the type of a variable in the code path that follows the type guard.
  • An assignment (including an initializer in a declaration) of a value of type S to a variable of type T changes the type of that variable to Tnarrowed by S in the code path that follows the assignment.
  • When multiple code paths lead to a particular location, the narrowed type of a given variable at that location is the union type of the narrowed types of the variable in those code paths.

The type Tnarrowed by S is computed as follows:

  • If T is not a union type, the result is T.
  • If T is a union type, the result is the union of each constituent type in T to which S is assignable.

Thanks to@ivogabe for providing inspiration and tests for this PR.

Fixes#2388.

HerringtonDarkholme, cevek, danielepolencic, svieira, jednano, RobertoUa, SlurpTheo, alitaheri, AbubakerB, NoelAbrahams, and 11 more reacted with thumbs up emojiRyanCavanaugh, mquandalle, jwbay, tinganho, svieira, jednano, ViIvanov, drub0y, RobertoUa, NoahZhang, and 13 more reacted with hooray emojiweswigham, mquandalle, mhegazy, svieira, jednano, elisee, toolness, RobertoUa, pkondzior, alitaheri, and 21 more reacted with heart emoji
ahejlsbergand others added30 commitsMarch 22, 2016 10:20
Adds tests to control flow types branch
@ahejlsbergahejlsberg merged commit5ed6f30 intomasterApr 22, 2016
basarat added a commit to alm-tools/alm that referenced this pull requestApr 22, 2016
@mhegazymhegazy mentioned this pull requestApr 22, 2016
5 tasks
@mquandallemquandalle mentioned this pull requestApr 23, 2016
4 tasks
dokidokivisual added a commit to karen-irc/karen that referenced this pull requestMay 4, 2016
chore(TypeScript): Enable 'strictNullChecks' optionThis 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 -->
@kristian-puccio
Copy link

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?

reduxjs/redux#992

@mhegazymhegazy deleted the controlFlowTypes branchMay 16, 2016 17:33
@microsoftmicrosoft locked and limited conversation to collaboratorsJun 19, 2018
Sign up for freeto subscribe to this conversation on GitHub. Already have an account?Sign in.
Reviewers

@birbilisbirbilisbirbilis left review comments

Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

14 participants
@ahejlsberg@jods4@RyanCavanaugh@ivogabe@zpdDG4gta8XKpMCd@JsonFreeman@thebanjomatic@kristian-puccio@sandersn@DanielRosenwasser@weswigham@birbilis@mhegazy@msftclas

[8]ページ先頭

©2009-2025 Movatter.jp