DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • When TypeScript Types Meet Untrusted AI: Building Type-Safe LLM Pipelines With Runtime Validation
  • Beyond the Chatbot: Engineering a Real-World GitHub Auditor in TypeScript
  • Resilient API Consumption in Unreliable Enterprise Networks (TypeScript/React)
  • Supercharge AI Workflows on Azure: Remote MCP Tool Triggers + Your First TypeScript MCP Server

Trending

  • When RabbitMQ Accepts a Message but Does Not Route It to a Queue
  • OwlVigil Open-Sources Its Go SDK for Enterprise AI
  • AI Agents and Frontier Models: Insights From The Neuron
  • Moving 900+ GB of Prometheus Without a Cutover Weekend

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. JavaScript
  4. How the TypeScript Omit Type Works

How the TypeScript Omit Type Works

Let's look at how the Omit Type works in TypeScript

By 
Johnny Simpson
·
Apr. 27, 22 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
6.0K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

TypeScript provides a number of utility types that are used to solve a particular problem that using types in Javascript creates. One very useful utility type used in TypeScript is the Omit type, which lets us customize an already existing type. Let's look at how it works.

Custom Types

This article assumes you know how to create custom types in TypeScript. If you don't, read my article on custom types here.

TypeScript Omit Type

In TypeScript, we often create custom types that let us ensure data conforms to a specific format. For example, if we wanted to create a custom User type that has four fields - firstName, lastName, age and lastActive, we could do something like this:

TypeScript
 
type User = {
  firstName: string;
  lastName: string;
  age: number;
  lastActive: number;
}

Sadly, coding is not always straightforward. Sometimes, we want to use our type again, but remove certain elements from it, thus creating a new type. To do this, we can use Omit<Type, Omissions>. Omit accepts two values:

  • The Type to base our new type on
  • A Union Type listing all the fields to remove. For example, if we want to take our User type, and remove age and lastActive, we could do the following:
TypeScript
 
type User = {
  firstName: string;
  lastName: string;
  age: number;
  lastActive: number;
}

type UserNameOnly = Omit<User, "age" | "lastActive">

Now we have two types - User, which is our core user type, and UserNameOnly, which is our User type minus age and lastActive. Similarly, if we only wanted to remove age, this would suffice:

TypeScript
 
type UserNameAndActive = Omit<User, "age">

Now we can use our new types anywhere in our code. This gives us the flexibility to use a type, and transform it for specific circumstances. Here is an example of using both our new types:

TypeScript
 
type User = {
  firstName: string;
  lastName: string;
  age: number;
  lastActive: number;
}

type UserNameOnly = Omit<User, "age" | "lastActive">
type UserNameAndActive = Omit<User, "age">

const userByName:UserNameOnly = {
    firstName: "John",
    lastName: "Doe",
};
const userWithoutAge:UserNameAndActive = {
    firstName: "John",
    lastName: "Doe",
    lastActive: -16302124725
}
TypeScript

Published at DZone with permission of Johnny Simpson. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • When TypeScript Types Meet Untrusted AI: Building Type-Safe LLM Pipelines With Runtime Validation
  • Beyond the Chatbot: Engineering a Real-World GitHub Auditor in TypeScript
  • Resilient API Consumption in Unreliable Enterprise Networks (TypeScript/React)
  • Supercharge AI Workflows on Azure: Remote MCP Tool Triggers + Your First TypeScript MCP Server

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook