Cheatsheet for TypeScript Utility Types
Quick start on Partial, Required, Readonly, Record, Pick, Omit, Exclude, Extract, Parameters, etc.
Partial<T>
— — Make all properties of a type optional.
class StateStore<T> {
constructor(public current: T) { }
update(next: Partial<T>) {
this.current = { ...this.current, ...next };
}
}const state = new StateStore({ x: 0, y: 0 });
state.update({ y: 123 });
console.log(state.current); // { x: 0, y: 123 }
Required<T>
— — Make all the members of a type required.
type Example = {
a?: string,
b?: number,
}
const example: Required<Example> = { a: "a" }
// Property 'b' is missing in type '{ a: string; }' but required in type 'Required<Example>'.
Readonly<T>
— — Make all properties of a type read only.
type Example = {
a: string,
b: number,
}function makeReadonly<T>(object: T): Readonly<T> {
return Object.freeze({ ...object });
}const example: Example = { a: "a", b: 1 }
const readonlyExample = makeReadonly(example);
readonlyExample.a = "no readonly"; // Error: readonly
Record<K, T>
— — Match type A as K
and type B as T
and create a new type.
Note the new type will be an object type with property keys as K
and property values as T
. So keyof Record<K, T> === K
, and Record<K, T>[K] === T
.
Example from the TypeScript Handbook:
interface CatInfo {
age: number;
breed: string;
}type CatName = “miffy” | “boris”const cats: Record<CatName, CatInfo> = {
miffy: { age: 10, breed: “Persian” },
boris: { age: 5, breed: “Maine Coon” },
};cats.boris; // const cats: Record<CatName, CatInfo>
Pick<T, Keys>
— — Pick only the specified Keys
from T
.
type Point3D = {
x: number,
y: number,
z: number,
};type Point2D = Pick<Point3D, 'x' | 'y'>;
// type Point2D = {
x: number;
y: number;
}
Omit<T, Keys>
— — Omit the specified Keys
from the T
:
type Point3D = {
x: number,
y: number,
z: number,
};
type Point2D = Omit<Point3D, 'z'>;
// type Point2D = {
x: number;
y: number;
}
Exclude<Union, ExcludedUnion>
— — Exclude ExcludedUnion
from T
, which is normally a union.
type flat3D = 'x' | 'y' | 'z';
type flat2D = Exclude<flat3D, 'z'>
// 'x' | 'y'
Extract<Union, ExtractedUnion>
— — Extract ExtractedUnion
from T
, which is normally a union.
type flat3D = 'x' | 'y' | 'z';
type flat2D = Extract<flat3D, 'z'>
// 'z'
It can be used to find the intersection of two types:
type flatD= Extract<flat3D, flat2D>;
// 'z'
type flatD= flat3D & flat2D
// 'z'
NonNullable<T>
— — Exclude null
and undefined
from Type
.
type StrOrNullOrUndefined = string | null | undefined;
type Str = NonNullable<StrOrNullOrUndefined>;
Parameters<Function>
— — Save types of a function’s parameters to a tuple.
function f1(a: number, b: number) {
return a + b;
}
type f1Params = Parameters<typeof f1>; //[number, number]type f2 = Parameters<(s: string) => void> // [string]
ConstructorParameters<ClassConstructor>
— — Save types of a class constructor’s parameters to a tuple.
class Point2D {
private x: number;
private y: number;
constructor( x: number, y: number ) {
this.x = x;
this.y = y;
}
}type point2DConstructor = ConstructorParameters<typeof Point2D>
// [number, number]const point2DConstructorExample: ConstructorParameters<typeof Point2D>[0] = 0
ReturnType<Function>
— — Get the type returned by the function.
function f1(a: number, b: number) {
return a + b;
}type f1ReturnType = ReturnType<typeof f1>;
//type f1ReturnType = number