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
"type guard typescript not working for single case switch"
https://www.typescriptlang.org/pt/play/?#code/C4TwDgpgBAYg9nKBeKBvAUASFJAXFAcgEMCAaLI-AZ2ACcBLAOwHNyBfddAMwFdGBjYPTiMoXBAApxcfPDgBKNFioB3esH4ALKFIQA6HBEUZMmfkSrRiBXFlP8RVOABsIe53Ga64eovLuYtBDAPLSMANycpgAmEFxEPM7AtqZmjsBQAPoQAB6aCTT0AG4QAMKaEPwA1viMECW0yGIIkZgcHOiGUABCRI0oJob41uSYVDwARkOEJKOUUDQMLOxQAD5K2OAQw7PKk9MEE2RYE9R0TKzoHbwCQiJQE30Sj7T4vbTGymoa2s99BltPvYLFYSCl7I4XG4PF5rFAmA8+lAAPyI2i+KD4F56Cb+VJBEJhSJ2WLxRLJAIORg0LK5fI8QolcqVGpQOoNJovVrtIA
type Foo = { type: 'a', a: string, } function foo(foo: Foo) { switch (foo.type) { case 'a': console.log(foo.a) return; default: const _exhaustiveCheck: never = foo; } } type Bar = { type: 'a', subtype: 'a', a: string, } | { type: 'a', subtype: 'b', b: string, } function bar(bar: Bar) { switch (bar.type) { case 'a': console.log('a' in bar ? bar.a : bar.b) return; default: const _exhaustiveCheck: never = bar; } }
No type reduction occurs in the default case. Typescript marks foo not assignable to type never.
foo
never
Both foo and bar should be reduced to type never in the default case.
bar
Related: #2214
Workaround:
Manually exclude types from foo that are handled by case 'a'.
case 'a'
type Foo = { type: 'a', a: string, } function foo(foo: Foo) { switch (foo.type) { case 'a': console.log(foo.a) return; default: const _exhaustiveCheck: never = foo as Exclude<typeof foo, { type: 'a' }>; } }
The text was updated successfully, but these errors were encountered:
Duplicate of #18056. A bit of time has passed since then, maybe things changed and it's not too complex anymore.
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
"type guard typescript not working for single case switch"
🕗 Version & Regression Information
This changed between versions ______ and _______This changed in commit or PR _______This is the behavior in every version I tried, and I reviewed the FAQ for entries about _________I was unable to test this on prior versions because _______⏯ Playground Link
https://www.typescriptlang.org/pt/play/?#code/C4TwDgpgBAYg9nKBeKBvAUASFJAXFAcgEMCAaLI-AZ2ACcBLAOwHNyBfddAMwFdGBjYPTiMoXBAApxcfPDgBKNFioB3esH4ALKFIQA6HBEUZMmfkSrRiBXFlP8RVOABsIe53Ga64eovLuYtBDAPLSMANycpgAmEFxEPM7AtqZmjsBQAPoQAB6aCTT0AG4QAMKaEPwA1viMECW0yGIIkZgcHOiGUABCRI0oJob41uSYVDwARkOEJKOUUDQMLOxQAD5K2OAQw7PKk9MEE2RYE9R0TKzoHbwCQiJQE30Sj7T4vbTGymoa2s99BltPvYLFYSCl7I4XG4PF5rFAmA8+lAAPyI2i+KD4F56Cb+VJBEJhSJ2WLxRLJAIORg0LK5fI8QolcqVGpQOoNJovVrtIA
💻 Code
🙁 Actual behavior
No type reduction occurs in the default case. Typescript marks
foo
not assignable to typenever
.🙂 Expected behavior
Both
foo
andbar
should be reduced to typenever
in the default case.Additional information about the issue
Related: #2214
Workaround:
Manually exclude types from
foo
that are handled bycase 'a'
.The text was updated successfully, but these errors were encountered: