Type alias IfEquals<T, P, Do, Else>

IfEquals<T, P, Do, Else>: Equals<T, P> extends true
    ? Do
    : Else

Conditional type that checks if type T is equal to type P. If T is equal to P, the type resolves to Do, otherwise Else.

Type Parameters

  • T
  • P
  • Do
  • Else

Example

type Result1 = IfEquals<string, string, true, false>; // is true
type Result2 = IfEquals<number, string, true, false>; // is false
type Result3 = IfEquals<boolean, boolean, true, false>; // is true

type IsExactlyString<T> = IfEquals<T, string, true, false>;
type IsExactlyNumber<T> = IfEquals<T, number, true, false>;

type TestString = IsExactlyString<string>; // is true
type TestNumber = IsExactlyNumber<number>; // is false
type TestBoolean = IsExactlyString<boolean>; // is false