Saturday, December 28, 2024
Google search engine
HomeLanguagesJavascriptTypeScript Omit Utility Type

TypeScript Omit Utility Type

In this article, we are going to learn about the Omit<Type, Keys> utility type in Typescript. TypeScript is a powerful language that provides many utility types to make development easier and more efficient. One of the Typescript features is Omit utility type. We can use it when, let’s say there exists a type already but we want to create a new type that must contain only a few properties, and with the help of Omit, we will be able to exclude the unnecessary properties.

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, first of all, we will create an interface called Person with three properties called name, age, and address, and now let’s say that we only want the name and age properties in the new type and we want to exclude the address, so we can do it with the Omit. We have created a new type called nameAndAge with Omit and we have excluded the address, as we can see in the example below. 

Javascript




/* Define an interface Person with three 
properties: name, age, and address */
interface Person {
    name: string;
    age: number;
    address: string;
}
  
/* Define a new type NameAndAge using the Omit 
utility type to exclude the address property 
from the Person interface */
type NameAndAge = Omit<Person, 'address'>;
  
/* Declare a constant called person with
the NameAndAge type and assign it a 
value of { name: 'John', age: 30 } */
const person: NameAndAge = { 
    name: 'John'
    age: 30 
};
  
/* Log the person constant to the 
console to see the output */
console.log(person);


Output:

 

Example 2: In this example, we will use the Omit with function, so first of all we have defined an interface called Book with four properties named id, title, author, and price. Now we have also created a function called getBookDetails that will take the Object of Book as its argument and simply return a string with the title and author since we have excluded the id and price, as we can see in the below example.

Javascript




/* Define an interface called Book with four 
properties: id, title, author, and price */
interface MyBook {
    id: number;
    title: string;
    author: string;
    price: number;
}
  
/* Define a function called getBookDetails
that accepts a book of type Omit<Book, 'id' | 'price'> 
and returns a string */
function getBookDetails(book: Omit<MyBook, 'id' | 'price'>): string {
    return `Title: ${book.title}, Author: ${book.author};`
}
  
/* Declare a constant called book of type 
Book and assign it an object with values 
for id, title, author, and price */
const book: MyBook = {
    id: 123,
    title: 'The Alchemist',
    author: 'Paulo Coelho',
    price: 15.99,
};
  
/* Call the getBookDetails function with the book 
constant as an argument and store the result in 
a constant called bookDetails */
const bookDetails = getBookDetails(book);
  
/* Log the bookDetails constant to the console, 
which will output "Title: The Alchemist, 
Author: Paulo Coelho"  */
console.log(bookDetails);


Output:

 

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.

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments