AlexHv
AlexHv

Reputation: 1734

Typescript ternary operator breaks identity type

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

Upvotes: 1

Views: 1306

Answers (1)

Chris Heald
Chris Heald

Reputation: 62648

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;
}

Upvotes: 5

Related Questions