Excluding Fields from a Type in TypeScript: A Simple Guide

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

How to Exclude a Property from a Type in TypeScript: Saving Your Code from Unnecessary Baggage

\n\n

Have you ever encountered a situation where you have a large interface or type, and you need to create a new type that is almost the same, but without one or two fields? Duplicate all the code? Copy 15 lines and delete one? There is a more elegant way. Today we'll break down how to exclude a property from a type in TypeScript — this is one of the most popular utilities that saves you from duplication and makes your code cleaner.

\n\n

This topic is a real hit on Stack Overflow. The question "Exclude property from type" gets thousands of views because sooner or later every developer faces the need to remove a field from a type or exclude a property from an interface. Let's break it down step by step, starting with simple examples and moving on to advanced techniques.

\n\n

The Problem: Why Exclude Fields from a Type?

\n\n

Imagine you have a User interface:

\n\n
interface User {\n  id: number;\n  name: string;\n  email: string;\n  password: string;\n  createdAt: Date;\n}
\n\n

You want to create a registration form where the user enters their name, email, and password. But id is generated on the server, and createdAt is automatic. You need a NewUser type that only contains name, email, and password. How do you do this?

\n\n

A bad solution is to declare a new interface from scratch:

\n\n
interface NewUser {\n  name: string;\n  email: string;\n  password: string;\n}
\n\n

This works, but it violates the DRY (Don't Repeat Yourself) principle. If we change the type of name in User, we'll have to change it in NewUser as well. This is exactly where excluding properties from a type comes to the rescue.

\n\n

The Utility Type Omit: The Main Tool

\n\n

TypeScript provides a built-in utility type Omit<Type, Keys>. It creates a new type by excluding the specified keys from the original. The syntax is simple:

\n\n
type NewUser = Omit<User, 'id' | 'createdAt'>;
\n\n

Now NewUser only contains name, email, and password. If we change email to Email in User, NewUser will pick it up automatically.

\n\n

Let's test it in practice:

\n\n
const newUser: NewUser = {\n  name: 'Anna',\n  email: 'anna@example.com',\n  password: 'secure123'\n}; // ✅ All good\n\nconst invalidUser: NewUser = {\n  name: 'Anna',\n  email: 'anna@example.com',\n  password: 'secure123',\n  id: 1 // ❌ Error: id does not exist in type NewUser\n};
\n\n

Note: Omit works with both interfaces and types. It's a universal tool for excluding fields from a type.

\n\n

How Does Omit Work Under the Hood?

\n\n

To better understand how TypeScript excludes properties from a type, let's look under the hood. Omit is a combination of two other utilities: Pick and Exclude.

\n\n

Here's how it's defined in the TypeScript standard library:

\n\n
type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
\n\n

Let's break it down step by step:

\n
    \n
  • keyof T — gets all keys of type T (e.g., 'id' | 'name' | 'email' | 'password' | 'createdAt')
  • \n
  • Exclude<keyof T, K> — excludes from these keys those specified in K (resulting in 'name' | 'email' | 'password')
  • \n
  • Pick<T, ...> — picks only the remaining keys from T
  • \n
\n\n

Thus, excluding a property from an interface boils down to filtering keys. Elegant, isn't it?

\n\n

Pick: The Flip Side of Omit

\n\n

Sometimes it's easier not to exclude, but the oppo

Blogs

Book Recommendations