Type equality

There is no equality operator for comparing types (e.g. == or ===) like there is for values. Instead, the extends keyword can be used in a generic type construct to produce types depending on the result of an equality check.

However, this does not work for more complex types like object types with optional properties, and with never/any types.

A more precise solution for type equality is shown below, which uses a technique called tuple wrapping. This allows us to avoid conditional type distribution, and works better with edge cases like any, never, and union types.

Was this page helpful?