Some built-in conditional types in Typescript

| Tag typescript  types 

Exclude<T, U>

Computes those types in T that are not in U:

type A = number | string;
type B = string;

type C = Exclude<A, B>; // number

Extract<T, U>

Computes those types in T that we can assign to U:

type D = Extract<A, B>; // string

NonNullable

Computes a version of T that excludes null and undefined:

type E = {a?: number | null};
type F = NonNullable<E['a']>; // number

ReturnType

Passing a function type, and computes the function’s return type:

type Fun = (a:number) => string;
type R = ReturnType<Fun>; // string

InstanceType

Computes the instance type of a class constructor:

type ConstructorType = {new(): I};
type I = {b: number};
type IT = InstanceType<ConstructorType>; // {b: number}

Prev     Next