← Back to Notes

TypeScript utility types cheat sheet

TypeScript

TypeScript Utility Types Cheat Sheet

Partial<T>

Makes all properties optional.

type PartialUser = Partial<User>
// All User properties become optional

Required<T>

Makes all properties required.

type RequiredUser = Required<User>

Pick<T, K>

Select specific properties.

type UserPreview = Pick<User, 'id' | 'name' | 'email'>

Omit<T, K>

Exclude specific properties.

type UserWithoutPassword = Omit<User, 'password'>

Record<K, V>

Create an object type with specific key/value types.

type StatusMap = Record<string, boolean>

Exclude<T, U>

Exclude types from a union.

type StringOrNumber = string | number | boolean
type NoBoolean = Exclude<StringOrNumber, boolean> // string | number

ReturnType<T>

Get the return type of a function.

function getUser() { return { id: '1', name: 'Bagas' } }
type User = ReturnType<typeof getUser>