Home > Mobile >  Typescript ternary operator breaks identity type
Typescript ternary operator breaks identity type

Time:01-24

Why is my SupposedId type below not a type identity?

Typescript is complaining that Type 'T' is not assignable to type 'SupposedId<T>'.

How comes T cannot be assignable to either T or T, what am I missing?

type SupposedId<T> = T extends object ? T : T;

function makeId<T>(test: T): SupposedId<T> {
  return test // <- Type 'T' is not assignable to type 'SupposedId<T>'
}

Playground example

CodePudding user response:

This is because of distributed conditional types. SupposedId distributes to T extends any | T extends object. Yours is certainly a novel case, since they both actually resolve to T, but the conditional type performs different narrowing based on whether or not T extends object.

However, per the documentation, you can fix it by adding square brackets around each side of the extends keyword:

type SupposedId<T> = [T] extends [object] ? T : T;

function makeId<T>(test: T): SupposedId<T> {
  /* `test` is now one of `T extends object` or `T extends any`,
     not `T extends object | T extends any` */
  return test;
}
  • Related