TypeScript Omit<Type, Keys> Utility Type
Last Updated :
10 May, 2023
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
interface Person {
name: string;
age: number;
address: string;
}
type NameAndAge = Omit<Person, 'address' >;
const person: NameAndAge = {
name: 'John' ,
age: 30
};
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
interface MyBook {
id: number;
title: string;
author: string;
price: number;
}
function getBookDetails(book: Omit<MyBook, 'id' | 'price' >): string {
return `Title: ${book.title}, Author: ${book.author};`
}
const book: MyBook = {
id: 123,
title: 'The Alchemist' ,
author: 'Paulo Coelho' ,
price: 15.99,
};
const bookDetails = getBookDetails(book);
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.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...