DEV Community

Cover image for 4 TypeScript Utility Types That Delete Half Your Types
Muiduzzaman Lipu for Refactorly

Posted on

4 TypeScript Utility Types That Delete Half Your Types

Do you have a User type, plus UserUpdate, PublicUser and UserPreview, all copying the same fields? Every time you add a field, you have to update all of them, and one day you'll forget.

TypeScript's built-in utility types build those types from one source of truth. Here are the four you'll use every day, plus two bonuses. Every error message below is the real output of tsc --strict (TypeScript 7.0).

Video version:

Start with one type

interface User {
  id: number;
  name: string;
  email: string;
  password: string;
  role: "admin" | "editor" | "viewer";
}
Enter fullscreen mode Exit fullscreen mode

Every other user type will be derived from this one.

Partial<T>: every field optional

Perfect for update functions, where you only send what changed:

function updateUser(id: number, changes: Partial<User>) { /* … */ }

updateUser(1, { name: "Ada" });   // ✅ only what changed
updateUser(1, { nmae: "Ada" });   // ❌
Enter fullscreen mode Exit fullscreen mode
error TS2353: Object literal may only specify known properties,
and 'nmae' does not exist in type 'Partial<User>'.
Enter fullscreen mode Exit fullscreen mode

Optional doesn't mean unchecked: typos are still caught.

Pick and Omit: choose fields

Pick keeps only the fields you list. Omit removes them.

type UserPreview = Pick<User, "id" | "name">;
// { id: number; name: string }

type PublicUser = Omit<User, "password">;

const pub: PublicUser = {
  id: 1, name: "Ada", email: "a@x.dev", role: "admin",
  password: "hunter2",   // ❌
};
Enter fullscreen mode Exit fullscreen mode
error TS2353: Object literal may only specify known properties,
and 'password' does not exist in type 'PublicUser'.
Enter fullscreen mode Exit fullscreen mode

When you add a field to User, both types update automatically.

Record<K, V>: an object with known keys

type Role = User["role"];   // "admin" | "editor" | "viewer"

const permissions: Record<Role, string[]> = {
  admin:  ["read", "write", "delete"],
  editor: ["read", "write"],
};
Enter fullscreen mode Exit fullscreen mode
error TS2741: Property 'viewer' is missing in type
'{ admin: string[]; editor: string[]; }' but required in type
'Record<"admin" | "editor" | "viewer", string[]>'.
Enter fullscreen mode Exit fullscreen mode

I forgot viewer, and TypeScript caught it. Add a new role later and every Record<Role, …> tells you exactly what's missing. That's much safer than { [key: string]: string[] }.

Bonus: ReturnType<typeof fn>

function createUser(data: Omit<User, "id">) {
  return { ...data, id: 42, createdAt: new Date() };
}

type CreatedUser = ReturnType<typeof createUser>;
// User fields + createdAt: Date, without writing it by hand
Enter fullscreen mode Exit fullscreen mode

The type stays in sync with the function. Change what it returns and the type follows.

Combine them (+ Readonly)

The real power comes from nesting them:

type UserPatch = Partial<Omit<User, "id" | "password">>;

const patch: UserPatch = { email: "new@x.dev" };  // ✅
const bad: UserPatch = { id: 7 };                 // ❌
Enter fullscreen mode Exit fullscreen mode
error TS2353: Object literal may only specify known properties,
and 'id' does not exist in type 'Partial<Omit<User, "id" | "password">>'.
Enter fullscreen mode Exit fullscreen mode

Every field is optional, but id and password can't be patched at all.

Readonly<T> blocks changes, which is great for config or state:

const user: Readonly<User> = loadUser();
user.role = "viewer";
// error TS2540: Cannot assign to 'role' because it is a read-only property.
Enter fullscreen mode Exit fullscreen mode

(Readonly is shallow, and it's a compile-time check only, so nested objects can still be mutated.)

Cheat sheet

Utility What it does Typical use
Partial<T> All fields optional Updates, patches
Pick<T, K> Keep only these fields Previews, list items
Omit<T, K> Remove these fields Hide secrets
Record<K, V> Object with exactly these keys Lookup tables
Readonly<T> No assignments allowed Config, state
ReturnType<typeof fn> Type of what a function returns Keep types in sync

Write the type once and derive the rest. Which utility type do you use the most? 👇


I post one tested coding tip every day on YouTube at Refactorly. The 30-second version of this one is here.

Top comments (0)