Type operators

Contents

Type operators#

Built-in operators that compute new types.

Operator

Meaning

keyof T

union of T’s keys.

typeof x

the type of a runtime value x (in a type position).

T[K]

indexed access.

T extends U ? X : Y

conditional type.

infer T (inside conditional)

bind an inferred sub-type.

Partial<T>

every property optional.

Required<T>

every property required.

Readonly<T>

every property readonly.

Pick<T, K>

subset of properties.

Omit<T, K>

drop properties.

Record<K, V>

object type with keys K, values V.

ReturnType<F>

return type of a function type.

Awaited<P>

unwrap a Promise<T> (recursively).

NonNullable<T>

drop null and undefined.

Examples.

type User = {id: string; name: string; email?: string};

keyof produces the union of property names.

type UserKeys = keyof User;               // "id" | "name" | "email"

Partial makes every property optional.

type UserUpdate = Partial<User>;

Indexed access picks a property’s type.

type UserName = User["name"];             // string

Pick and Omit slice the structure.

type UserSlim = Pick<User, "id" | "name">;
type UserNoId = Omit<User, "id">;

Compose multiple operators.

type AsyncResult = Awaited<ReturnType<typeof fetchUser>>;

References#