-
Notifications
You must be signed in to change notification settings - Fork 12.8k
TypeScript doesn't reverse-infer generic constraints from discriminated unions #61661
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
Comments
This is working as intended.
|
And why should there be a type error on |
@jcalz In which case, you still get the error from @MartinJohns |
Adding To give an example, this is your issue: function foo<T>(x: T) {}
foo<"bar">("any string") Do you think that narrowing |
🔎 Search Terms
narrowing, discriminated types, extends, generic, constraints
🕗 Version & Regression Information
⏯ Playground Link
https://www.typescriptlang.org/play/?ts=4.0.5#code/KYDwDg9gTgLgBDAnmYcAiiB2AVZwA82AfHALwBQAPnABTZygzCYAmAznGzFAJaYDmcAPxwA3uTgIAhoIBccAORdeAhXADcEuAH1tMGXHnK+ggGRx6mgL6G4mYADdgUAJRVa9Rs3Z2ArgFsAI2dhMS19OUVMAOCoNU1JXQjbaKCQ80tyG3l7J1dycgAzX0wAYxgeCEw4FgD-RGxCIhoQWwwcPCaXMMkeQtoQADpk0lHFY1Vu8UlJUqquHVt6UkUAGwhV+K1JKGAYXyhqhXXNjS0rLT6B4YNRlYVU2IVu3f3DxQAWACYtnb2D6qtaxAA
💻 Code
🙁 Actual behavior
There is a type error on
const _
:There is no type error on
return '42'
.🙂 Expected behavior
There should be no type error on the
const _
.T
is indeed of typestring
in that branch, and can not benever
, as it has a.tag
property.There should be a type error on
return '42'
.T
is of typenumber
in that branch, which is incompatible with the type"42"
.Additional information about the issue
I know it's a complicated case, but it would let me express pseudo-GADTs powerful enough to do some type of Zod/Runtype-like dynamic types that I can dynamically pattern-match over. That way, I could do well-typed svelte component generation for forms or displays, among other things.
Cheers!
The text was updated successfully, but these errors were encountered: