Official documentation: https://www.typescriptlang.org/docs/handbook/utility-types.html
Make all properties in T
optional.
type t = Partial<{a: string; b: number}>;
// {a?: string; b?: number}
Make all properties in T
required.
type t = Required<{a?: string; b?: number}>;
// {a: string; b: number}
Make all properties in T
readonly.
type t = Readonly<{a: string; b: number; c?: boolean}>;
// {readonly a: string; readonly b: number; readonly c?: boolean}
From T
, pick a set of properties whose keys are in the union K
.
type t = Pick<{a: string; b: number; c?; boolean}, 'a' | 'c'>;
// {a: string; c?; boolean}
Construct a type with a set of properties K
of type T
.
type Demo = {...};
type t = Record<'a' | 'b' | 'c', Demo>;
// {a: Demo; b: Demo; c: Demo}
Exclude from T
those types that are assignable to U
.
type t = Exclude<'a' | 'b' | 'c' | 'd', 'b' | 'c' | 'x' | 'y'>;
// 'a' | 'd'
Extract from T
those types that are assignable to U
.
type t = Extract<'a' | 'b' | 'c' | 'd', 'b' | 'c' | 'x' | 'y'>;
// 'b' | 'c'
Exclude null
and undefined
from T
.
type t = NonNullable<'a' | string | number | undefined>;
// 'a' | string | number
Obtain the parameters of a function type in a tuple.
type t = Parameters<(a: string, b: number) => void>;
// [string, number]
Obtain the return type of a function type.
type t = ReturnType<(a: string, b: number) => string>;
// string
Obtain the return type of a constructor function type.
More info: microsoft/TypeScript#25998 (comment)
type t = InstanceType<typeof C>;
// C