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

Narrow string and number types in literal equality checks#11587

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 5 commits intomasterfromnarrowStringAndNumber
Oct 13, 2016

Conversation

@ahejlsberg
Copy link
Member

@ahejlsbergahejlsberg commentedOct 13, 2016
edited
Loading

With this PR we narrow thestring andnumber types as appropriate when they are compared with values of literal types.

functionmove(direction:"up"|"down"){// ...}functiondo1(command:string){if(command==="up"||command==="down"){move(command);// Narrowed to type "up" | "down"}}functiondo2(command:string){switch(command){case"up":case"down":move(command);// Narrowed to type "up" | "down"break;}}functionf1(x:number,y:1|2){if(x===0||x===y){x;// Narrowed to type 0 | 1 | 2}}functionf2(x:number|"foo"|"bar",y:1|2|string){if(x===y){x;// Narrowed to type "foo" | "bar" | 1 | 2y;// Narrowed to type "foo" | "bar" | 1 | 2}}

Interestingly this found abug in our scanner, pointing out that type2 is not comparable to type8.

Fixes#7447.
Fixes#10417.
Fixes#11306.

HerringtonDarkholme, tinganho, normalser, aluanhaddad, robertknight, tekacs, malekpour, rictic, GoToLoop, SlurpTheo, and nikeee reacted with thumbs up emojirictic and GoToLoop reacted with hooray emoji
@ahejlsbergahejlsberg merged commit17c2ab2 intomasterOct 13, 2016
@ahejlsbergahejlsberg deleted the narrowStringAndNumber branchOctober 13, 2016 21:00
@zpdDG4gta8XKpMCd
Copy link

i am not at the computer to check, but would it fix#11433 as well?

@mhegazy
Copy link
Contributor

i am not at the computer to check, but would it fix#11433 as well?

nope. that is a different issue.

Sign up for freeto subscribe to this conversation on GitHub. Already have an account?Sign in.

Reviewers

1 more reviewer

@mhegazymhegazymhegazy approved these changes

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

5 participants

@ahejlsberg@zpdDG4gta8XKpMCd@mhegazy@msftclas

[8]ページ先頭

©2009-2025 Movatter.jp