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

IfExtends<T, P, Do, Else>: T extends P
    ? Do
    : Else

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

Type Parameters

  • T
  • P
  • Do
  • Else

Example

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

type IsString<T> = IfExtends<T, string, true, false>;
type IsNumber<T> = IfExtends<T, number, true, false>;

type TestString = IsString<string>; // is true
type TestNumber = IsNumber<number>; // is true
type TestBoolean = IsNumber<boolean>; // is false