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

Fix #24991: Weaken narrowing for == #29840

Merged

Conversation

jack-williams
Copy link
Collaborator

With this PR narrowing for == is now weaker for numbers, booleans, and string.

Previously == assumed that the types could not be coerced to eachother; now we no longer prune a number, string, or boolean when comparing against a number, string, or boolean.

declare let x: number | string | boolean
declare let n: number;

if (x == n) {
    x; // previously x has type number; now x has type number | string | boolean
}

This solution is not sound: we still prune when comparing to a literal type. Examples

declare let x: number | string | boolean
declare let n: number;

if (x == 1) {
    x; // x still has type 1
}

This choice was made to reduce potential breaking changes. I'm open to tweaking the semantics based on RWC.

Fixes #24991

@RyanCavanaugh
Copy link
Member

@typescript-bot test this

@typescript-bot
Copy link
Collaborator

typescript-bot commented Apr 25, 2019

Heya @RyanCavanaugh, I've started to run the extended test suite on this PR at 0fe03d6. You can monitor the build here. It should now contribute to this PR's status checks.

@RyanCavanaugh
Copy link
Member

@typescript-bot test this new merge commit

@typescript-bot
Copy link
Collaborator

typescript-bot commented Apr 25, 2019

Heya @RyanCavanaugh, I've started to run the extended test suite on this PR at 6542d65. You can monitor the build here. It should now contribute to this PR's status checks.

@RyanCavanaugh
Copy link
Member

RWC is clean

@RyanCavanaugh RyanCavanaugh merged commit 0949ad1 into microsoft:master Apr 26, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Invalid type coercion with == operator and strings
3 participants