Open In App

TypeScript Omit<Type, Keys> Utility Type

Last Updated : 03 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

TypeScript's Omit<Type, Keys> utility type creates a new type by excluding specific properties (Keys) from an existing type (Type). It is useful for refining types by removing unnecessary properties, enhancing type safety, and simplifying complex type definitions in TypeScript applications.

Syntax: 

type NewType = Omit<OriginalType, 'Key1' | 'Key2'>
  • OriginalType is the original type from which we want to create a new type.
  • Key1 and Key2 are the properties that we want to remove from the new type.

Omit Vs Pick Vs Partial: Note that we have some Typescript utility types with similar kinds of operation, but have slight differences between them, for example, Pick allows us to select properties, Partial allows us to make properties optional and Omit allows us to exclude properties. These utility types can be used together to create more complex types that suit our needs.

Approach: We can see how we can use Omit through this step-by-step approach including code snippets.

Step 1: First of all we need to define the original type that we want to create a new type from.

interface Person {
      name: string;
      age: number;
      gender: string;
      address: string;
}

Step 2: Now we need to define the new type that we want to create by using the Omit utility type. In this case, the new type is NewPerson and Person is the original Type, and we want to exclude the address property from the new type using the Omit.

type NewPerson = Omit<Person, 'address'>;

Step 3: Now we can now use the new type NewPerson in your code.

const person: NewPerson = {
      name: 'John',
      age: 30,
      gender: 'male'
};

Step 4: And finally we can use the output as our requirement.

Example 1:  In this example, we define a Person interface and create a new type NameAndAge using Omit to exclude the address property, keeping only name and age properties.

JavaScript
// Define "Person" interface with name, age, and address
interface Person {
    name: string;
    age: number;
    address: string;
}

// Create "NameAndAge" type using Omit to exclude address
type NameAndAge = Omit<Person, 'address'>;

// Declare "person" with type NameAndAge
const person: NameAndAge = { 
    name: 'John', 
    age: 30 
};

// Log the person object
console.log(person);

Output:

{ name: 'John', age: 30 }

Example 2: In this example, we define a Book interface and use Omit in the getBookDetails function, which takes a Book object excluding id and price, returning a string with only title and author.

JavaScript
// Define "MyBook" interface
interface MyBook {
    id: number;
    title: string;
    author: string;
    price: number;
}

// Function accepts book without id and price
function getBookDetails(book: Omit<MyBook, 'id' | 'price'>): string {
    return `Title: ${book.title}, Author: ${book.author}`;
}

// Create a "book" object
const book: MyBook = {
    id: 123,
    title: 'The Alchemist',
    author: 'Paulo Coelho',
    price: 15.99,
};

// Get book details
const bookDetails = getBookDetails(book);

console.log(bookDetails);

Output:

Title: The Alchemist, Author: Paulo Coelho

Conclusion: So in this article, we have covered what is Omit utility type with its syntax and two working examples. we have also discussed what are other similar types that do the same job but with a different approach, and we can use different types based on user requirements.


Next Article

Similar Reads