-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Allow non-unit types in union discriminants #27695
Conversation
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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:
function f21<T extends number>(x: { a: undefined; b: { x: number } } | { a: T, b: { y: number } }) {
if (x.a === undefined) {
x.b = { y: 42 };
}
}
function f(x: { kind: true, a: string } | { kind: "b" } | { kind: string, c: string }) {
if (x.kind === true) {
x.a;
}
else if (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. |
@weswigham
|
@jack-williams You're actually right about type I think it is fine to document that discriminants must be truly disjoint types, and that |
@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 |
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In any case, this definitely looks like an improvement, and we can always adjust it to recognize more types as discriminable in the future if need be.
Any chance this will allow using tuples as discriminants? e.g.
|
@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.