TypeScript Utility Types: Partial, Pick, Omit, Record and More

What are utility types in TypeScript?
Utility types are ready-made generic types that create a new type from an existing one. Instead of writing a second, almost identical type by hand, you transform the first one: make all properties optional (Partial), keep only a few (Pick), remove some (Omit), and so on.
They are built into TypeScript, so you do not need to import anything. Their big advantage is that they stay connected to the original type. When you change the original, all derived types change with it, so you do not have to remember to update them.
The starting type
All examples in this post use the same Player type:
type Player = {
id: number;
nickname: string;
level: number;
status: 'online' | 'offline' | 'away';
};
Partial and Required
Partial<T> makes all properties optional. It is perfect for update functions, where you send only the fields that changed.
function updatePlayer(player: Player, changes: Partial<Player>): Player {
return { ...player, ...changes };
}
const neo: Player = { id: 1, nickname: 'Neo', level: 1, status: 'online' };
const updated = updatePlayer(neo, { level: 2 });
console.log(updated.level);
// Output: 2
updatePlayer(neo, { level: 'high' });
// Error: Type 'string' is not assignable to type 'number'
With Partial<Player> you can pass { level: 2 } without the other fields, but the types of the fields you do pass are still checked.
Required<T> does the opposite: it makes all properties required, even those that were optional.
type Settings = {
theme?: 'light' | 'dark';
language?: string;
};
type FullSettings = Required<Settings>;
// { theme: 'light' | 'dark'; language: string }
const settings: FullSettings = { theme: 'dark' };
// Error: Property 'language' is missing
A typical use is a function that fills in default values, so inside the rest of the code every setting is guaranteed to exist.
Readonly
Readonly<T> makes all properties read only. TypeScript will not let you assign to them.
const neo: Readonly<Player> = {
id: 1,
nickname: 'Neo',
level: 1,
status: 'online',
};
neo.level = 2;
// Error: Cannot assign to 'level' because it is a read-only property
There are two things to remember:
- It is shallow. Only the first level is protected. If a property contains an object or an array, its content can still be changed.
- It exists only at compile time. After compilation the object is a normal JavaScript object. If you need protection at runtime, you need
Object.freezetoo.
type Team = Readonly<{ name: string; members: string[] }>;
const team: Team = { name: 'Matrix', members: ['Neo'] };
team.name = 'Zion';
// Error: Cannot assign to 'name'
team.members.push('Trinity');
// No error, members itself is not read only
Pick and Omit
These two create a smaller type from a bigger one.
Pick<T, Keys> keeps only the listed properties. Omit<T, Keys> removes the listed properties and keeps the rest.
type PlayerPreview = Pick<Player, 'id' | 'nickname'>;
// { id: number; nickname: string }
type NewPlayer = Omit<Player, 'id'>;
// { nickname: string; level: number; status: 'online' | 'offline' | 'away' }
const preview: PlayerPreview = { id: 1, nickname: 'Neo' };
function createPlayer(data: NewPlayer): Player {
return { id: Date.now(), ...data };
}
createPlayer({ nickname: 'Trinity', level: 5, status: 'away' });
A simple rule for choosing: if you want to keep a few properties, use Pick. If you want to remove a few properties, use Omit.
One trap with Omit: it does not check that the key really exists. A typo gives no error:
type Broken = Omit<Player, 'nikname'>;
// No error, and nothing was removed, because 'nikname' does not exist
Pick is stricter here, it reports an error for a key that is not in the type. If you want the same safety for Omit, write a small helper:
type StrictOmit<T, K extends keyof T> = Omit<T, K>;
Record
Record<Keys, Value> creates an object type with the given keys, where every key has the same value type. It is the typed version of a dictionary or a lookup table.
type PlayerStatus = Player['status'];
// 'online' | 'offline' | 'away'
const statusLabels: Record<PlayerStatus, string> = {
online: 'Playing now',
offline: 'Not here',
away: 'Back soon',
};
The nice thing is that when the keys are a union, TypeScript forces you to write all of them:
const broken: Record<PlayerStatus, string> = {
online: 'Playing now',
offline: 'Not here',
};
// Error: Property 'away' is missing
So when you add a new status to Player, the compiler shows you every Record that needs an update.
You can also use it with a general key type, for example a dictionary of players by id:
const playersById: Record<number, Player> = {};
playersById[1] = { id: 1, nickname: 'Neo', level: 1, status: 'online' };
Note that Player['status'] in the first example is an indexed access type. It takes the type of one property from another type, so you do not have to write the union twice.
Exclude, Extract and NonNullable
The previous types work on objects. These three work on unions.
Exclude<T, U>removes from the unionTeverything that fitsU.Extract<T, U>keeps only what fitsU.NonNullable<T>removesnullandundefined.
type PlayerStatus = 'online' | 'offline' | 'away';
type Inactive = Exclude<PlayerStatus, 'online'>;
// 'offline' | 'away'
type Active = Extract<PlayerStatus, 'online' | 'away'>;
// 'online' | 'away'
type MaybeNickname = string | null | undefined;
type Nickname = NonNullable<MaybeNickname>;
// string
Note that Exclude and Omit are different tools, even though the names sound similar. Omit removes properties from an object type, and Exclude removes members from a union.
ReturnType, Parameters and Awaited
These types read information from a function type, so you do not have to write the type twice.
function createPlayer(nickname: string, level = 1) {
return { id: Date.now(), nickname, level };
}
type CreatedPlayer = ReturnType<typeof createPlayer>;
// { id: number; nickname: string; level: number }
type CreateArgs = Parameters<typeof createPlayer>;
// [nickname: string, level?: number]
Note the typeof createPlayer. ReturnType needs a type, and a function name is a value, so typeof turns the value into its type.
Awaited<T> gives you the type you get after await. It unwraps a Promise:
async function loadPlayer() {
return { id: 1, nickname: 'Neo' };
}
type LoadResult = ReturnType<typeof loadPlayer>;
// Promise<{ id: number; nickname: string }>
type LoadedPlayer = Awaited<ReturnType<typeof loadPlayer>>;
// { id: number; nickname: string }
These are very handy when a function comes from a library and you need its result type, but the library does not export it.
Combining utility types
The real power comes when you combine them. Imagine a form for editing a player. The user cannot change the id, and they do not have to send every field:
type PlayerChanges = Partial<Omit<Player, 'id'>>;
// {
// nickname?: string;
// level?: number;
// status?: 'online' | 'offline' | 'away';
// }
function updatePlayer(player: Player, changes: PlayerChanges): Player {
return { ...player, ...changes };
}
const neo: Player = { id: 1, nickname: 'Neo', level: 1, status: 'online' };
updatePlayer(neo, { status: 'away' }); // OK
updatePlayer(neo, { id: 99 });
// Error: 'id' does not exist in type PlayerChanges
Read it from the inside out: first Omit removes id, then Partial makes everything else optional.
Another common combination is a read-only copy of only some fields:
type PublicPlayer = Readonly<Pick<Player, 'nickname' | 'level'>>;
If you add a new field to Player later, PlayerChanges gets it automatically. This is the main reason to use utility types instead of copying the type by hand.
How do they work under the hood?
Utility types are not magic. They are written in TypeScript itself, using generics and mapped types. This is how Partial, Readonly, Pick and Record are defined:
type MyPartial<T> = {
[K in keyof T]?: T[K];
};
type MyReadonly<T> = {
readonly [K in keyof T]: T[K];
};
type MyPick<T, Keys extends keyof T> = {
[K in Keys]: T[K];
};
type MyRecord<Keys extends string | number | symbol, Value> = {
[K in Keys]: Value;
};
The part [K in keyof T] means "go through every property name of T", and T[K] is the type of that property. Adding ? or readonly changes the modifier on the way. You can use the same idea to write your own utility type, for example one that makes all properties nullable:
type Nullable<T> = {
[K in keyof T]: T[K] | null;
};
type NullablePlayer = Nullable<Player>;
// { id: number | null; nickname: string | null; ... }
This is the same generic constraint idea (extends keyof T) that I described in my post about generics.
Cheat sheet
| Utility type | What it does | Works on |
|---|---|---|
Partial<T> |
Makes all properties optional | Objects |
Required<T> |
Makes all properties required | Objects |
Readonly<T> |
Makes all properties read only (shallow) | Objects |
Pick<T, K> |
Keeps only the listed properties | Objects |
Omit<T, K> |
Removes the listed properties | Objects |
Record<K, V> |
Object with keys K and values V |
Keys and values |
Exclude<T, U> |
Removes members of a union that fit U |
Unions |
Extract<T, U> |
Keeps members of a union that fit U |
Unions |
NonNullable<T> |
Removes null and undefined |
Unions |
ReturnType<F> |
Type of what a function returns | Functions |
Parameters<F> |
Tuple of the function's parameter types | Functions |
Awaited<T> |
Type after await (unwraps a Promise) |
Promises |
Summary
Utility types create new types from existing ones, so you write the main type once and derive the rest. Use Partial, Required and Readonly to change how properties behave, Pick, Omit and Record to shape objects, Exclude, Extract and NonNullable to work with unions, and ReturnType, Parameters and Awaited to read types from functions. Remember that Readonly is shallow, that Omit does not check key names, and that you can combine several utility types, like Partial<Omit<Player, 'id'>>.
About the author
Front-end developer who pays special attention to the aesthetics and functionality of websites. I love playing with interesting design solutions, creating interfaces that not only work efficiently, but also attract attention with their unique design. I like to derive pleasure from it, like with my old 'Portfolio' website. After hours, I return to classic games or good movies that stimulate the imagination.
Comments
Join the discussionNo comments yet. Be the first to comment.





