Utility Types
Introduction
Partial<T>
Constructs a type with all properties of T
set to optional. This utility will return a type that represents all subsets of a given type.
Example
Readonly<T>
Constructs a type with all properties of T
set to readonly
, meaning the properties of the constructed type cannot be reassigned.
Example
interface Todo {
title: string;
}
const todo: Readonly<Todo> = {
title: 'Delete inactive users',
};
todo.title = 'Hello'; // Error: cannot reassign a readonly property
This utility is useful for representing assignment expressions that will fail at runtime (i.e. when attempting to reassign properties of a ).
Object.freeze
function freeze<T>(obj: T): Readonly<T>;
Record<K,T>
Constructs a type with a set of properties K
of type T
. This utility can be used to map the properties of a type to another type.
Example
interface PageInfo {
title: string;
}
type Page = 'home' | 'about' | 'contact';
const x: Record<Page, PageInfo> = {
about: { title: 'about' },
contact: { title: 'contact' },
};
Pick<T,K>
Example
Omit<T,K>
Constructs a type by picking all properties from T
and then removing K
.
Example
interface Todo {
title: string;
description: string;
completed: boolean;
}
type TodoPreview = Omit<Todo, 'description'>;
const todo: TodoPreview = {
completed: false,
};
Exclude<T,U>
Constructs a type by excluding from T
all properties that are assignable to U
.
Example
type T0 = Exclude<"a" | "b" | "c", "a">; // "b" | "c"
type T1 = Exclude<"a" | "b" | "c", "a" | "b">; // "c"
type T2 = Exclude<string | number | (() => void), Function>; // string | number
Extract<T,U>
Constructs a type by extracting from T
all properties that are assignable to U
.
Example
type T0 = Extract<"a" | "b" | "c", "a" | "f">; // "a"
type T1 = Extract<string | number | (() => void), Function>; // () => void
NonNullable<T>
Constructs a type by excluding null
and undefined
from T
.
Example
ReturnType<T>
Example
type T0 = ReturnType<() => string>; // string
type T1 = ReturnType<(s: string) => void>; // void
type T2 = ReturnType<(<T>() => T)>; // {}
type T3 = ReturnType<(<T extends U, U extends number[]>() => T)>; // number[]
type T4 = ReturnType<typeof f1>; // { a: number, b: string }
type T6 = ReturnType<never>; // any
type T7 = ReturnType<string>; // Error
type T8 = ReturnType<Function>; // Error
InstanceType<T>
Constructs a type consisting of the instance type of a constructor function type T
.
Example
class C {
x = 0;
y = 0;
}
type T0 = InstanceType<typeof C>; // C
type T1 = InstanceType<any>; // any
type T2 = InstanceType<never>; // any
type T3 = InstanceType<string>; // Error
type T4 = InstanceType<Function>; // Error
Required<T>
Constructs a type consisting of all properties of T
set to required.
Example
interface Props {
a?: number;
b?: string;
};
const obj: Props = { a: 5 }; // OK
const obj2: Required<Props> = { a: 5 }; // Error: property 'b' missing
ThisType<T>
This utility does not return a transformed type. Instead, it serves as a marker for a contextual this
type. Note that the —noImplicitThis
flag must be enabled to use this utility.
Example
In the example above, the methods
object in the argument to makeObject
has a contextual type that includes ThisType<D & M>
and therefore the type of this
in methods within the methods
object is { x: number, y: number } & { moveBy(dx: number, dy: number): number }
. Notice how the type of the methods
property simultaneously is an inference target and a source for the this
type in methods.