I encountered this problem in my project, and below is the minimal code that can reproduce it. I want to know if it is a bug of TypeScript, or it is I using it wrongly. (The actual code is more complicated, but the error is the same)
The error is: Type 'T' is not assignable to type 'T extends T ? T : T'.
I tested on TypeScript 3.0.3 and 3.3.1
function test<T>(arg: T) {
let x: T extends T ? T : T;
x = arg;
}
Typescript will not try to resolve conditional types and thus even this seemingly trivial example will fail to assign. My guess your realworld example is more complex, but the same idea still applies.
Only if the conditional type is the same is assignment permitted:
function test<T>(arg: T) {
let x: T extends T ? T : T;
let y: T extends T ? T : T;
x = y; //ok
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With