Utility Types in TypeScript – Explained Simply

TypeScript Utility Types Explained with Simple Examples

TypeScript has a super useful feature called utility types. These are built-in tools that help you create new types based on existing ones — without having to rewrite everything.

Think of them like functions for types. Instead of manually adding or removing properties from a type, you use a shortcut, and TypeScript handles the rest.

Example: a basic type

type User = { name: string; age: number; email: string; };

Now let’s say you want a version of User without the email field. You could write it manually like this:

type UserWithoutEmail = { name: string; age: number; };

But there’s a better way:

type UserWithoutEmail = Omit<User, "email">;

Done! That’s the power of utility types — short and clean.

Most Useful Utility Types

Let’s go over 6 of the most commonly used ones. I’ll show simple examples for each.

1. Partial — makes all fields optional

type User = { name: string; age: number; };

const updateUser = (user: Partial<User>) => {
  console.log(user);
};

updateUser({ name: "Alice" }); // age is optional

When to use it: when updating only part of an object.

2. Required — makes all fields required

type User = { name?: string; age?: number; };

const createUser = (user: Required<User>) => {
  console.log(user.name, user.age);
};

createUser({ name: "Bob", age: 30 });

When to use it: when you want to be sure all values are present.

3. Readonly — makes fields unchangeable

type User = { name: string; age: number; };

const user: Readonly<User> = {
  name: "Eva",
  age: 25
};

user.age = 26; // ❌ Error: can't change readonly property

When to use it: if you want to protect an object from changes.

4. Pick — selects only specific fields

type User = { name: string; age: number; email: string; };

type ContactInfo = Pick<User, "name" | "email">;

const contact: ContactInfo = {
  name: "Tom",
  email: "tom@example.com"
};

When to use it: when you need just part of a type.

5. Omit — removes specific fields

type User = { name: string; age: number; email: string; };

type NoEmail = Omit<User, "email">;

const person: NoEmail = {
  name: "Lina",
  age: 22
};

When to use it: when you want to exclude some fields.

6. Record — creates a typed object from keys

type Roles = "admin" | "user" | "guest";

const roleColors: Record<Roles, string> = {
  admin: "red",
  user: "blue",
  guest: "gray"
};

When to use it: when you have a fixed list of keys and want to assign a type to their values.

Cheat Sheet

UtilityWhat it does
Partial<T>Makes all fields optional
Required<T>Makes all fields required
Readonly<T>Makes fields read-only
Pick<T, K>Gets only specified fields
Omit<T, K>Removes specified fields
Record<K, T>Creates object with given keys

Final Thoughts

Utility types are like cheat codes in TypeScript. They save time, make your code cleaner, and help you avoid mistakes. You don’t need to reinvent the wheel — just use what the language already gives you!

Leave a Reply

Your email address will not be published. Required fields are marked *