Utility types, as const, satisfies
Partial, Pick, Omit, Record, ReturnType and types derived from values.
Updated
What utility types are
TS comes with ready-made types that transform other types. Instead of rewriting User in 5 variants (for creating, updating, displaying), you derive them from a single User. The benefit: change User once, and every variant updates.
type User = { id: number; name: string; email: string; password: string }
type UserUpdate = Partial<User> // everything optional
type PublicUser = Omit<User, 'password'> // no password
type UserPreview = Pick<User, 'id' | 'name'> // only a few fieldsThe most used ones — what they do and when
| Utility | What it does | When |
|---|---|---|
Partial<T> |
every field optional | a patch / partial update, default values |
Required<T> |
every field required | after filling in the defaults |
Readonly<T> |
every field readonly |
config, state you don't change |
Pick<T, K> |
only the keys K |
props for a small component |
Omit<T, K> |
everything except K |
removing sensitive or generated fields (id) |
Record<K, V> |
an object with keys K and values V |
dictionaries, mappings |
ReturnType<F> |
a function's return type | typing from existing code |
Parameters<F> |
a tuple of the parameter types | wrappers around functions |
Awaited<T> |
the type inside a Promise | Awaited<ReturnType<typeof getUser>> |
NonNullable<T> |
removes null and undefined |
after a check |
typeof and keyof — types from values
const config = { theme: 'dark', lang: 'en' }
type Config = typeof config // { theme: string; lang: string }
type ConfigKey = keyof Config // 'theme' | 'lang'
async function getUser(id: number) { ... }
type UserData = Awaited<ReturnType<typeof getUser>> // the real type, without rewriting itas const — values as literals
const ROLES = ['admin', 'editor', 'viewer'] as const
type Role = (typeof ROLES)[number] // 'admin' | 'editor' | 'viewer'A single list is both a value at runtime (for a <select>) and a type at compile time. No duplication.
satisfies — checking without losing the exact type
const routes = {
home: '/',
blog: '/blog',
} satisfies Record<string, string>
routes.home // the type stays exactly '/', not string
routes.shop // ✗ doesn't existconst x: T = ... |
const x = ... satisfies T |
|
|---|---|---|
| Checks the shape | yes | yes |
The type of x |
T (wider) | the exact one from the value |
Summary
- Derive types from a single one:
Partial,Pick,Omit,Record. typeof/ReturnType/Awaited— types from existing code.as constfor lists of literals;satisfiesfor checking without widening.