-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Generic type constraint doesn't seem to propagate to conditional type #56045
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
Thank you, I see, that does look like a duplicate. Awesome to hear that there's a PR in the works! π |
I don't think so. Here is the playground for the PR and I don't see any change with OP's code. |
The only time this assignment is actually safe is when you could have written the target as an intersection instead: type IsObject<T> = T & object; which works as expected and is generally clearer all around. The problem of resolving to the truthy branch of a conditional type based on the constraint is that it then appears that a constraint not matching the |
Thanks for the tip - I can work around my original issue by patching MikroORM's For the more general issue of resolving conditionals based on constraints...
...if I understand right, you're saying that But in that case, is something like this possible? Test if constraint |
π Search Terms
"generic constraint conditional type"
π Version & Regression Information
β― Playground Link
https://www.typescriptlang.org/play?#code/C4TwDgpgBAkgzgeQEYCsIGNgB4AqA+KAXihyggA9gIA7AEzigHtUNgoB+EqALimogBuEAE4BuAFAAzAK7VMAS0bUok6rjKUa9Ji0x4AFMF44AlFADe4qFHRK4bcr3jI0mXAWLAJAXyA
π» Code
π Actual behavior
Type 'object' is not assignable to type 'IsObject'. [2322]
π Expected behavior
Since the type parameter
T
is constrained to extendobject
, I expectedtsc
to infer thatT
is a subtype ofIsObject<T>
.I'm sure I'm misunderstanding how constraints or conditional types work, but on the face of it this is very surprising to me. In the real world version of this I'm trying to to construct generic queries with MikroORM, which does an
IsObject<T>
check recursively in its query DSL type.Additional information about the issue
No response
The text was updated successfully, but these errors were encountered: