Description
TypeScript Version: versions after 3.5.1, tested up to 4.1.0-dev.20200807
Search Terms: conditional constrain never
Code
type StringConstrain<T> = { [K in keyof T]: T[K] extends string ? T[K] : never }
type WantString<T extends string> = number
type Test<T extends StringConstrain<T>> = { [K in keyof T]: WantString<T[K]> }
Expected behavior:
The constrain in line 1 should function correctly and demands all keys of T to extend string in line 3, as such WantString<T[K]>
should be happy.
Actual behavior:
In any version greater and including 3.5.1 line 3 is not happy
Type 'T[K]' does not satisfy the constraint 'string'.
Type 'T[keyof T]' is not assignable to type 'string'.
Type 'T[string] | T[number] | T[symbol]' is not assignable to type 'string'.
Type 'T[string]' is not assignable to type 'string'.(2344)
I don't understand the new behavior and if this is not a bug, what's the intended way of achieving the same type constrain after 3.5.1?
Playground Link:
Playground link showing works in v3.3.3
Playground link showing not working in 3.9.2
Playground link showing not working in 4.1.0-dev.20200807
Related Issues:
Same question on stackoverflow from last month: https://stackoverflow.com/questions/62563003/typescript-conditional-types-constrain-no-longer-working-after-v3-5-1