Skip to content

Fix #24991: Weaken narrowing for == - #29840

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

Ryan Cavanaugh (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

@RyanCavanaugh

Copy link
Copy Markdown
Member

TypeScript Bot (@typescript-bot) test this

@typescript-bot

TypeScript Bot (typescript-bot) commented Apr 25, 2019 •

Copy link
Copy Markdown
Contributor

Heya Ryan Cavanaugh (@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 (@typescript-bot) test this new merge commit

@typescript-bot

TypeScript Bot (typescript-bot) commented Apr 25, 2019 •

Copy link
Copy Markdown
Contributor

Heya Ryan Cavanaugh (@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
Ryan Cavanaugh (RyanCavanaugh) merged commit 0949ad1 into microsoft:master Apr 26, 2019
@microsoft 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