Type operators#
Built-in operators that compute new types.
Operator |
Meaning |
|---|---|
|
union of |
|
the type of a runtime value |
|
indexed access. |
|
conditional type. |
|
bind an inferred sub-type. |
|
every property optional. |
|
every property required. |
|
every property readonly. |
|
subset of properties. |
|
drop properties. |
|
object type with keys |
|
return type of a function type. |
|
unwrap a |
|
drop |
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#
Generics for parameterising types over
T.Conditional and mapped for
extends/infermechanics.