GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||
GeezerVison is showing github.com — Fix discriminant property check by ahejlsberg · Pull Request #29110 · microsoft/TypeScript Conversation
Member
In #27695 we were slightly too permissive in what we consider a discriminant property. Specifically, we required the combined union type to contain at least one unit type, but that unit type could have occurred in combination with non-unit types. We now check that at least one underlying property has a pure unit type or union of unit types. Fixes #29106.
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page. All reactions
Anders Hejlsberg (ahejlsberg)
requested review from
Daniel Rosenwasser (DanielRosenwasser) and
Wesley Wigham (weswigham)
Member
All reactions
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page.
Member
Author
All reactions
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page.
Member
All reactions
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page.
Collaborator
All reactions
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page.
Member
Author
All reactions
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page.
Member
All reactions
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page.
|
There was an error while loading. Please reload this page.
|
Also 2-3 months != 2 weeks. This has been out for awhile (and has shipped in stable versions of TS). |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
Wesley Wigham (@weswigham) No, we don't do discriminant based narrowing for type Foo = { a: string, x: number } | { a: 0, y: number }; function xxx(obj: Foo) { if (typeof obj.a === "string") { obj.x; // Error (because we don't narrow here) } if (obj.a === 0) { obj.y; // Ok (discriminant based narrowing) } } With the fix in this PR our behavior changes in the following because we no longer consider type Foo = { a: string | undefined, x: number } | { a: string, y: number }; function xxx(obj: Foo) { if (obj.a === undefined) { obj.x; // Previously would narrow, now doesn't } } As I said, I can't imagine anyone having taken a dependency on this in the last few weeks (or months or whatever). This PR is the right fix for the issue given the design constraints we currently have. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
If I understand the issue properly it seems to be a tradeoff between
The number of people affected by (1) seems likely to be larger than (2), and (2) can be fixed by replacing the equality test with a type guard. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Jack Williams (@jack-williams) Yes, that's the tradeoff. But #2 is actually even further qualified: Breaking code for early adopters of discriminant narrowing involving properties that have no single underlying property of a pure unit type or union of unit types. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
There was an error while loading. Please reload this page.