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[]>
PartialandOmitare probably the most used in day-to-day work - especially when typing form state and API payloads.
updated
did you know it?