TypeScript Version: 2.9.2
Search Terms:
conditional type constraint
Code
declare class Model<M extends MR, MR extends {}> {
// Compiler error:
// Type 'M[K]' does not satisfy the constraint 'K extends keyof MR ? MR[K] : M[K]'.
public getField2<K extends keyof M>(): Field<M[K], K extends keyof MR ? MR[K] : M[K]>
}
declare class Field<T extends TR, TR> {
}
Expected behavior:
No compiler error.
Because M extends MR, and K extends keyof M:
- If
K extends keyof MR, then I expect M[K] to satisfy the constraint MR[K].
- Else, I expect
M[K] to satisfy the constraint M[K].
- Therefore, it seems that
M[K] should satisfy the constraint K 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?
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?