-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Generics: False negative on type constraint validation involving a conditional type #25413
Comments
Interesting followup. As of now, at least, with TypeScript 3.3.3, I can get the correct behavior with a small change: use
The change to I'm still a bit unclear on when it is appropriate to use type |
I think should be covered by #26933 provided you convert the conditional type to be non-distributive. |
@jack-williams I'm not familiar with what a "distributive" vs "non-distributive" type is. Could you elaborate on what you mean by "convert the conditional type to be non-distributive."? |
@UselessPickles Here is a relevant comment that should have the information (or at least point it). |
Thanks. I like the trick for preventing distribution (in this comment, referenced by the comment you pointed me to) :) |
@jack-williams is correct - #26933 + using a non-distributive conditional does fix this. The reason a distributive conditional can't be assumed to work is because the |
…ional when assignable to both branches (#30639) * Finally add that missing relationship allowing a type to be assignable to both branches of a conditional * Explicitly write out Ternary.Maybe * Add slightly modified example from #25413 * fix sick sentence * Loosen check to skip false branch constraint check to consider `infer` parameters as always satisfied in the extends clause * Simplify things a bit, only instantiate once Co-authored-by: Nathan Shively-Sanders <293473+sandersn@users.noreply.github.com>
TypeScript Version: 2.9.2
Search Terms:
conditional type constraint
Code
Expected behavior:
No compiler error.
Because
M extends MR
, andK extends keyof M
:K extends keyof MR
, then I expectM[K]
to satisfy the constraintMR[K]
.M[K]
to satisfy the constraintM[K]
.M[K]
should satisfy the constraintK extends keyof MR ? MR[K] : M[K]
Actual behavior:
Compiler error: Type 'M[K]' does not satisfy the constraint 'K extends keyof MR ? MR[K] : M[K]'.
Playground Link:
https://www.typescriptlang.org/play/#src=declare%20class%20Model%3CM%20extends%20MR%2C%20MR%20extends%20%7B%7D%3E%20%7B%0D%0A%20%20%20%20%2F%2F%20Compiler%20error%3A%20%0D%0A%20%20%20%20%2F%2F%20Type%20'M%5BK%5D'%20does%20not%20satisfy%20the%20constraint%20'K%20extends%20keyof%20MR%20%3F%20MR%5BK%5D%20%3A%20M%5BK%5D'.%0D%0A%20%20%20%20public%20getField2%3CK%20extends%20keyof%20M%3E()%3A%20Field%3CM%5BK%5D%2C%20K%20extends%20keyof%20MR%20%3F%20MR%5BK%5D%20%3A%20M%5BK%5D%3E%0D%0A%7D%0D%0A%0D%0Adeclare%20class%20Field%3CT%20extends%20TR%2C%20TR%3E%20%7B%0D%0A%7D
Related Issues:
Possibly?
The text was updated successfully, but these errors were encountered: