Account imageLoginSign UpAccount image
Loading votes....
Save Question

What's the difference between Omit, Pick, Partial, Required, and Readonly in TypeScript? Can you show practical examples?

clock icon

asked 3 months ago

Message icon

1

Eye icon

23

I see these utility types everywhere but I'm confused when to use each one. For example:

1interface User {
2 id: number;
3 name: string;
4 email: string;
5 password: string;
6}
1interface User {
2 id: number;
3 name: string;
4 email: string;
5 password: string;
6}

How do I create a type for:

  • Updating a user (all fields optional)
  • Creating a user without id (autogenerated by DB)
  • A public profile (no password)

1 Answer

The utility types at a glance

UtilityEffectExample
PartialAll properties become optional{ id?: number; name?: string; ... }
RequiredAll properties become required (removes ?)Opposite of Partial
ReadonlyAll properties become readonlyCannot reassign
Pick<T, K>Select only specific keysPick<User, 'id'
Omit<T, K>Remove specific keysOmit<User, 'password'>
Record<K, T>Object with keys of type K and values TRecord<'id'

Solutions to your examples

typescript

1interface User {
2 id: number;
3 name: string;
4 email: string;
5 password: string;
6}
7
8// 1. Updating a user (all optional)
9type UserUpdate = Partial<User>;
10// { id?: number; name?: string; email?: string; password?: string }
11
12// 2. Creating a user without id (id is auto-generated)
13type UserCreation = Omit<User, 'id'>;
14// { name: string; email: string; password: string }
15
16// 3. Public profile (no password)
17type PublicProfile = Omit<User, 'password'>;
18// { id: number; name: string; email: string }
19
20// Or using Pick (if you want only certain fields)
21type PublicProfile2 = Pick<User, 'id' | 'name' | 'email'>; // same result
1interface User {
2 id: number;
3 name: string;
4 email: string;
5 password: string;
6}
7
8// 1. Updating a user (all optional)
9type UserUpdate = Partial<User>;
10// { id?: number; name?: string; email?: string; password?: string }
11
12// 2. Creating a user without id (id is auto-generated)
13type UserCreation = Omit<User, 'id'>;
14// { name: string; email: string; password: string }
15
16// 3. Public profile (no password)
17type PublicProfile = Omit<User, 'password'>;
18// { id: number; name: string; email: string }
19
20// Or using Pick (if you want only certain fields)
21type PublicProfile2 = Pick<User, 'id' | 'name' | 'email'>; // same result

Combining utilities

typescript

1// All fields optional except id is required
2type UserUpdateWithId = Partial<User> & Pick<User, 'id'>;
3
4// Readonly public profile
5type ReadonlyPublicProfile = Readonly<PublicProfile>;
6// { readonly id: number; readonly name: string; readonly email: string }
7
8// Required version of a partial type (makes everything required again)
9type FullUser = Required<Partial<User>>; // back to original User
1// All fields optional except id is required
2type UserUpdateWithId = Partial<User> & Pick<User, 'id'>;
3
4// Readonly public profile
5type ReadonlyPublicProfile = Readonly<PublicProfile>;
6// { readonly id: number; readonly name: string; readonly email: string }
7
8// Required version of a partial type (makes everything required again)
9type FullUser = Required<Partial<User>>; // back to original User

When to use each

  • Partial – Update operations, form state
  • Pick – Selecting a subset of fields (keep exactly those)
  • Omit – Removing sensitive or auto-generated fields
  • Required – When you have optional fields but need to enforce them in a specific context
  • Readonly – Configuration objects, Redux state, or any data that shouldn't mutate
  • Record – Creating key‑value maps (e.g., error messages by field name)

Real‑world example

typescript

1type FormErrors<T> = Partial<Record<keyof T, string>>;
2
3interface LoginForm {
4 email: string;
5 password: string;
6}
7
8const errors: FormErrors<LoginForm> = {
9 email: 'Invalid email' // password is optional (no error)
10};
1type FormErrors<T> = Partial<Record<keyof T, string>>;
2
3interface LoginForm {
4 email: string;
5 password: string;
6}
7
8const errors: FormErrors<LoginForm> = {
9 email: 'Invalid email' // password is optional (no error)
10};

Pro tip: Omit is implemented as Pick<T, Exclude<keyof T, K>>. Use Omit when the keys to remove are few, Pick when the keys to keep are few.

1

Write your answer here

Top Questions