Uh oh!
There was an error while loading. Please reload this page.
Allow non-unit types in union discriminants - #27695
Conversation
Jack Williams (jack-williams)
commented
Oct 10, 2018
I mentioned it in the old PR, but I think it is worth recording in the PR that gets merged: This change will have the side-effect of enabling excess property checking for certain union types that were previous not subject to the checks. Unions types are only checked if they have a discriminant property, and this change distinguishes more discriminant types. Technically this is a breaking change. |
| x.b; // Error | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Could it be worth adding a test that includes constrained generics? My reasoning being that in the future it might be worth considering discriminants that have constrained parameters because they are comparable to literals in some form. Having a test that would flag any changes might be helpful. Something like:
functionf21<Textendsnumber>(x: {a: undefined;b: {x: number}}|{a: T,b: {y: number}}){if(x.a===undefined){x.b={y: 42};}}Wesley Wigham (weswigham)
commented
Oct 11, 2018
functionf(x: {kind: true,a: string}|{kind: "b"}|{kind: string,c: string}){if(x.kind===true){x.a;}elseif(x.kind!=="b"){x.c;// Should be `{ kind: string, c: string }`}else{x;// should be `{ kind: "b" } | { kind: string, c: string }`}}doesn't seem to work yet with this PR. |
Wesley Wigham (@weswigham)
|
Anders Hejlsberg (ahejlsberg)
commented
Oct 13, 2018
Jack Williams (@jack-williams) You're actually right about type I think it is fine to document that discriminants must be truly disjoint types, and that |
Jack Williams (jack-williams)
commented
Oct 15, 2018
Anders Hejlsberg (@ahejlsberg) Thanks for the explanation. I managed to confuse myself when I experimented with turning off union reduction and finding that it did not have an effect in this example.
Yes, I agree with your assessment here. The asymmetry brought about by trying to narrow types like |
Wesley Wigham (weswigham)
commented
Oct 15, 2018
Fair enough. I just wanted to point out that we still don't narrow all the things we look like we could because of how the broader string type and the specific literal type are related. |
Brian Ingles (bmingles)
commented
Oct 30, 2018
Any chance this will allow using tuples as discriminants? e.g. |
Kevin Saldaña (ksaldana1)
commented
Nov 14, 2018
Brian Ingles (@bmingles) I believe your example will narrow correctly since v3.1. |
With this PR we permit non-unit types in union type discriminants. Specifically, we now consider a property of a union type to be a discriminant property if it has a union type containing at least one unit type and no instantiable types. For example:
Inspired by #27631 which was closed in favor of this PR.
Fixes#24193.