We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
inference discriminated union narrowing ternary
type Unknown = {$type: string} type B = {$type: 'b'; str: string} const b: B = {$type: 'b', str: 'foo'} type A = {$type: 'a'; num: number; embed: B | Unknown} function isA<V extends {$type?: string}>(v: V): v is V & A { return v.$type === 'a' } const a: A = {$type: 'a', num: 42, embed: b} const maybeA: A | B = a // typed as `Unknown` instead of `Unknown | B` const result = isA(maybeA) ? maybeA.embed : b // return type is not `Unknown | B` function getEmbed(v: A | B) { if (isA(v)) return v.embed return v }
Playground
Better preserve inferred types when working with discriminated unions.
The text was updated successfully, but these errors were encountered:
To quote @MartinJohns:
This is working as intended and happens because of type reduction. See #50171 and many many others. Also: https://www.typescriptlang.org/docs/handbook/type-inference.html#best-common-type
This is working as intended and happens because of type reduction. See #50171 and many many others.
Also: https://www.typescriptlang.org/docs/handbook/type-inference.html#best-common-type
Sorry, something went wrong.
I wasn't aware of the "Best common type" type inference strategy. Thanks for pointing it out. Sorry about the noise π.
No branches or pull requests
π Search Terms
inference discriminated union narrowing ternary
β Viability Checklist
β Suggestion
Playground
π Motivating Example
Better preserve inferred types when working with discriminated unions.
π» Use Cases
The text was updated successfully, but these errors were encountered: