-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Discriminated unions do not work when discriminating on a tuple type #56920
Comments
Duplicate of #48500 (comment) and/or #55246.
A tuple is not a literal type, even if it contains literal-typed elements. |
Is this documented and/or considered not a bug? |
GitHub issues act as documentation as well. |
The documentation never claims that referenced-typed discriminants are supported. Not sure how we could document every negative statement about the language. |
@RyanCavanaugh This is also the case if all of the entries in the tuple are literals. I don't think a referenced type is necessary: type Union =
| {
discriminator: ['foo'];
value: string;
}
| {
discriminator: ['bar'];
value: number;
};
let foo: Union = {} as any;
if (foo.discriminator[0] === 'foo') {
foo.discriminator; // ["foo"]
foo.value; // string | number
} |
@AaronFriel That case would be covered by #18758 |
This issue has been marked as "Not a Defect" and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Well that's unfortunate, is there a better label for this so that it remains an open issue? |
π Search Terms
tuplenarrowing
discriminated union tuple
π Version & Regression Information
β― Playground Link
Playground Link
π» Code
π Actual behavior
Discriminating on the
parseEvent.path
value, a tuple type, in a conditional expression, theif
conditions of the code above, does not work to narrow the type of the entire value.When discriminating on the entire tuple, the
parseEvent.path
type is narrowed, butparseEvent.value
type is not.When discriminating on a part of the tuple, e.g.: the 3rd element of the tuple which exists in one of the unioned types and not the other, neither the types of
parseEvent.path
norparseEvent.value
are narrowed.π Expected behavior
The code above should have no errors, and using a tuple as a discriminator should largely succeed.
Additional information about the issue
No response
The text was updated successfully, but these errors were encountered: