Skip to content

Fix #24991: Weaken narrowing for ==#29840

Merged
RyanCavanaugh merged 2 commits into
microsoft:masterfrom
jack-williams:weaken-narrowing-of-equality
Apr 26, 2019
Merged

Fix #24991: Weaken narrowing for ==#29840
RyanCavanaugh merged 2 commits into
microsoft:masterfrom
jack-williams:weaken-narrowing-of-equality

Conversation

@jack-williams
Copy link
Copy Markdown
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

@jack-williams jack-williams force-pushed the weaken-narrowing-of-equality branch from 98d7a7e to 0fe03d6 Compare February 9, 2019 18:16
@RyanCavanaugh
Copy link
Copy Markdown
Member

@typescript-bot test this

@typescript-bot
Copy link
Copy Markdown
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
Copy Markdown
Member

@typescript-bot test this new merge commit

@typescript-bot
Copy link
Copy Markdown
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
Copy Markdown
Member

RWC is clean

@RyanCavanaugh RyanCavanaugh merged commit 0949ad1 into microsoft:master Apr 26, 2019
@microsoft microsoft locked as resolved and limited conversation to collaborators Oct 21, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

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