Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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

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;
}
like image 369
govizlora Avatar asked Sep 06 '25 22:09

govizlora


1 Answers

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 
}
like image 114
Titian Cernicova-Dragomir Avatar answered Sep 09 '25 04:09

Titian Cernicova-Dragomir