Utility Types

Built-in generic types that turn an existing type into a new one, so you don't redefine similar shapes by hand.

What is it?

Once you have an interface like User, you often need variations: every field optional (for an update form), only a few fields (for a preview card), or one sensitive field removed (before sending to the client). Writing a near-copy for each is repetitive, and the copies drift out of sync as User changes.

TypeScript's built-in utility types are generic types that transform an existing type into a new one. Four of the most used:

  • Partial<T> makes every property optional.
  • Pick<T, Keys> keeps only the listed properties.
  • Omit<T, Keys> keeps everything except the listed properties.
  • Record<Keys, ValueType> builds an object type mapping each key to the same value type.

Explain like I'm 10

Utility types are like photocopier settings for a type. Instead of retyping a document with some fields blanked out, you pick a setting ("make every field optional," "keep only these two," "drop that one") and get a related copy, without touching the original.

Examples

Partial, Pick, and Omit

interface User {
  id: string;
  name: string;
  email: string;
  age: number;
}

// Every property becomes optional — handy for an "update" function
type UserUpdate = Partial<User>;
const update: UserUpdate = { name: "New Name" }; // valid, other fields omitted

// Keep only name and email
type UserPreview = Pick<User, "name" | "email">;
const preview: UserPreview = { name: "Kai", email: "kai@example.com" };

// Keep everything except id (e.g. before the id is assigned)
type NewUser = Omit<User, "id">;
const draft: NewUser = { name: "Sam", email: "sam@example.com", age: 22 };

Each utility type derives a new shape from User without copying it. If User gains or loses a field, UserUpdate, UserPreview and NewUser stay in sync automatically.

Record for building object types

type Role = "admin" | "editor" | "viewer";

// An object type with exactly these three keys, each mapping to a boolean
type RolePermissions = Record<Role, boolean>;

const permissions: RolePermissions = {
  admin: true,
  editor: true,
  viewer: false,
};

// Missing a key is an error:
const incomplete: RolePermissions = { admin: true, editor: true };
// Error: Property 'viewer' is missing.

Record<Keys, ValueType> builds an object type where every key in Keys maps to ValueType. It's great for lookup tables keyed by a union of specific strings.

How it works

Utility types aren't special syntax. They're ordinary generic types, built with features like mapped types (covered later), that ship in TypeScript's standard type definitions.

For example, Partial<T> is a mapped type that goes through every key in T and re-adds it with a ?. When you write Partial<User>, the compiler expands it with User in place of T. Like interfaces and type aliases, utility types only affect compile-time checking and leave no trace in the compiled JavaScript.

Why does it exist?

Deriving a related type from an existing one is very common. Utility types cover the usual patterns (optional versions, subsets, everything-except, lookup tables), so you don't maintain near-duplicate interfaces that silently drift from the original.

When to use it

  • Partial for update or patch functions where any subset of fields may be given.
  • Pick/Omit for a narrower or almost-complete view of an existing type.
  • Record for a lookup table keyed by a known, fixed set of values.

When not to use it

If the new type differs a lot from the original (different property names, unrelated structure), a fresh interface is clearer. Utility types shine when the new type is a direct derivative of an existing one.

Common mistakes

  • Assuming Partial<T> lets properties be null. It only makes them optional (may be omitted), not nullable.

  • Passing Pick a key that doesn't exist on the source type. TypeScript flags it as an error instead of ignoring it (Omit doesn't check, as explained below).

  • Using Record<string, T> when the keys are a known, fixed set. Using the specific key union gives much better checking (catching missing or misspelled keys).

Practice exercises

  1. Easy:

    Given Product { id: string; name: string; price: number; }, create a Partial<Product> type and an object of it that only sets price.

  2. Medium:

    Using Product, create a ProductSummary type with Pick that keeps only name and price, and a ProductWithoutId type with Omit that drops id.

  3. Hard:

    Create a Weekday type as a union of the five weekday names. Use Record<Weekday, number> to type the hours worked each day, and build a valid object of that type.

Interview questions

What does `Partial<T>` do to each property of T, mechanically?

It's a mapped type that goes through every key K in keyof T and re-adds it with ?. Each property may be omitted, but if present it must still match its original type.

How would you implement `Partial<T>` yourself as a mapped type?

type MyPartial<T> = { [K in keyof T]?: T[K] };. Copy every key of T with ? added, leaving the value type unchanged.

What is `Required<T>`, and how does it invert what `Partial<T>` does?

Required<T> makes every property of T mandatory, even originally optional ones. It's the opposite of Partial<T>, which adds optionality.

How is `Required<T>` implemented under the hood, and what does the `-?` syntax do?

type Required<T> = { [K in keyof T]-?: T[K] };. The -? modifier removes any existing ?, forcing each property to be present.

Given `type Foo = Required<Partial<User>>;`, what shape does `Foo` end up with?

The same as User, with every property required. Partial makes them optional, then Required removes that, so the two cancel out.

Does `Partial<T>` make a property nullable, i.e. assignable to `null`?

No. ? only means the property may be omitted. A string property becomes optional string, not string | null, unless null was already in the original type.

What does `Pick<T, K>` do?

It builds a type with only the properties named in K, dropping the rest of T.

What does `Omit<T, K>` do, and how does it relate to `Pick`?

It keeps every property of T except those named in K. It's the opposite of Pick, which keeps only the named ones.

How is `Omit<T, K>` actually implemented internally in terms of `Pick` and `Exclude`?

As Pick<T, Exclude<keyof T, K>>. Exclude removes K from keyof T, and Pick keeps the remaining keys. So Omit is Pick applied to the leftover keys.

Why does passing a key that doesn't exist on T produce a compile error with `Pick<T, K>` but not with `Omit<T, K>`?

Pick constrains K extends keyof T, so an unknown key fails. Omit only constrains K extends keyof any (any property key), so an unknown key is accepted; Exclude<keyof T, K> just has nothing to remove.

What does "distributive" mean for a conditional type like `Exclude`, and why does `Omit` depend on it?

Exclude<A, B> is A extends B ? never : A. When A is a union, TypeScript checks the condition against each member separately, not the union as a whole. That's "distributive." Omit relies on it so Exclude<keyof T, K> removes exactly the matching keys from keyof T.

You need a type with every field of `User` except `id`. Would `Omit<User, "id">` or `Pick<User, "name"|"email"|"age">` be the better choice if `User` later gains a new field?

Omit<User, "id">. It only names what to remove, so new fields are included automatically. The Pick list would silently leave the new field out until someone adds it by hand.

What does `Record<K, V>` do?

It builds an object type where every key in the union K maps to the same value type V: a lookup table with a fixed set of keys.

Given `Record<Role, boolean>` for `Role = "admin"|"editor"|"viewer"`, why does an object missing the `viewer` key fail to compile?

Record expands to a mapped type with every literal in Role as a required key, like an interface requiring all three. Leaving one out is a missing-property error.

Why is `Record<string, T>` a weaker choice than `Record<SpecificKeyUnion, T>` when the keys are actually a small, fixed set?

Record<string, T> becomes an index signature ({ [key: string]: T }) that accepts any string key. Misspelled or extra keys go unnoticed and no key is guaranteed present. A literal key union requires each exact key.

How is `Record<K, V>` implemented as a mapped type?

type Record<K extends keyof any, V> = { [P in K]: V };. It goes through each member of K and gives it the value type V.

What does `Readonly<T>` do?

It's a mapped type that adds readonly to every property of T, so none can be reassigned after creation.

Does `Readonly<T>` enforce that immutability at runtime?

No. Like every utility type, it's compile-time only. The compiler rejects reassignments, but the JavaScript object itself can still be changed, for example by code that bypasses the type checker.

Given `const u: Readonly<User> = {...}; u.name = "New";`, why does the assignment fail to compile?

Readonly<User> makes name readonly, and assigning to a readonly property after creation is rejected, even with a valid string.

Is `Readonly<T>`'s restriction "deep" — does it also make properties of a nested object property read-only?

No, it's shallow. It only protects T's top-level properties. A nested object's properties stay mutable unless you write a recursive deep-readonly type.

What does `ReturnType<T>` extract, and from what kind of type?

From a function type, it extracts the return type: ReturnType<() => string> is string.

How is `ReturnType<T>` implemented, and what role does `infer` play?

type ReturnType<T extends (...args: any) => any> = T extends (...args: any) => infer R ? R : any;. The conditional checks that T is callable, and infer R captures its return type as the result.

Does `ReturnType<T>` distribute when T is a union of function types, e.g. `ReturnType<(() => string) | (() => number)>`?

Yes. T appears bare in the conditional's check, making it distributive. Each union member is checked separately and the results are unioned, giving string | number.

Does `Partial<T>` distribute the same way over a union type, e.g. `Partial<A | B>`?

No. Partial is a mapped type over keyof T, not a conditional type, so it doesn't distribute. keyof (A | B) only has keys common to both, so Partial<A | B> is built from those shared keys, which differs from Partial<A> | Partial<B>.

Why would you write `ReturnType<typeof someFunction>` instead of naming the return type directly?

When the return shape has no named type (for example an inline object literal), this lets you reference it without retyping it, and it stays in sync if the function changes.

What type does `Pick<Partial<User>, "name">` produce?

{ name?: string }. Partial makes every property optional, then Pick keeps only name, which stays optional.

Are `Partial<Pick<User, "name">>` and `Pick<Partial<User>, "name">` equivalent here?

Yes, both give { name?: string }. Picking a key then making it optional equals making everything optional then picking it, since unrelated keys don't interact.

Why do utility types exist as TypeScript built-ins instead of being something every project defines from scratch?

Deriving related shapes (optional versions, subsets, everything-except, lookup tables) is needed in almost every codebase. A shared built-in vocabulary saves each project from reinventing the same mapped and conditional types.

Do utility types like `Partial` and `Pick` leave any trace in the compiled JavaScript output?

No. Like interfaces and type aliases, they only affect type checking and are fully erased.

You need a type for a PATCH endpoint that accepts any subset of `User`'s fields except `id`, which should never be updatable. How would you compose utility types to express that?

Partial<Omit<User, "id">>. Omit removes id, then Partial makes the remaining fields optional, so callers can send any subset of updatable fields but never id.

What's the downside of hand-writing a separate interface `UserPreview { name: string; email: string }` instead of `Pick<User, "name"|"email">`?

It has no link to User. If name or email changes type in User, the hand-written copy silently goes stale. Pick<User, ...> always re-derives from User.