Skip to content
typescript questions

TypeScript · 3 of 10middle~1 min

What are utility types and which ones do you use most often?

#types#utility

Utility Types

TypeScript ships with built-in generic types that transform existing types.

Most commonly used

ts
interface User { id: number; name: string; email: string; password: string; }

Partial<T> - makes all properties optional

ts
function updateUser(id: number, patch: Partial<User>) {}

Required<T> - makes all properties required

ts
type StrictUser = Required<User>;

Pick<T, K> - selects a subset of properties

ts
type PublicUser = Pick<User, 'id' | 'name'>;

Omit<T, K> - removes specific properties

ts
type CreateUser = Omit<User, 'id'>;

Readonly<T> - makes all properties read-only

ts
const config: Readonly<Config> = { ... };

Record<K, V> - constructs an object type

ts
type RoleMap = Record<'admin' | 'user' | 'guest', string[]>;

ReturnType<T> - extracts the return type of a function

ts
function getUser() { return { id: 1, name: 'Alice' }; } type LoadedUser = ReturnType<typeof getUser>; // { id: number; name: string }

Awaited<T> - unwraps a Promise type

ts
type Data = Awaited<ReturnType<typeof fetchUsers>>; // User[] instead of Promise<User[]>

Partial and Omit are probably the most used in day-to-day work - especially when typing form state and API payloads.

updated

did you know it?

read more on the blog

view all

related questions