简体   繁体   中英

Type 'T' is not assignable to type 'T extends T ? T : T' in TypeScript

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'. 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 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM