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
"generic type discriminator"
Version 5.2.2
No response
type A = { 1: { dt: string }, 2: { num: number }, }; type B = { [K in keyof A]: { extra: { id: number, reason: K } data: A[K] } }[keyof A]; type C = { [K in keyof A]: { data: A[K] & { reason: K } } }[keyof A]; const test:B = {} as any; if (test.extra.reason === 2) { // Property 'num' does not exist on type '{ dt: string; } | { num: number; }'. Property 'num' does not exist on type '{ dt: string; }'.ts(2339) console.log(test.data.num); } // expected no error const test1:C = {} as any; if (test1.data.reason == 2) { console.log(test1.data.num); } // expected error const test2:B = { extra: { id: 1, reason: 2 }, data: { dt: 'string' }, }; // expected no error const test3:B = { extra: { id: 1, reason: 2 }, data: { num: 123 }, };
Typescript does not discriminate the type according to its constraints
discrimination should occur
The text was updated successfully, but these errors were encountered:
Duplicate #18758
Sorry, something went wrong.
This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes.
No branches or pull requests
π Search Terms
"generic type discriminator"
π Version & Regression Information
Version 5.2.2
β― Playground Link
No response
π» Code
π Actual behavior
Typescript does not discriminate the type according to its constraints
π Expected behavior
discrimination should occur
Additional information about the issue
No response
The text was updated successfully, but these errors were encountered: