TypeScript Omit<Type, Keys> Utility Type
Last Updated :
03 Sep, 2024
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.
Similar Reads
TypeScript Pick<Type, Keys> Utility Type
TypeScript's Pick<Type, Keys> utility type allows you to create a new type by selecting specific properties (`Keys`) from an existing type (`Type`). This is useful for narrowing down types to only the relevant properties, enhancing type safety, and reducing redundancy in complex type definitio
4 min read
Typescript Record<Keys, Type> Utility Type
In this article, we are going to learn about the Record<Keys, Type> in Typescript. TypeScript is a programming language that is a strict syntactical superset of JavaScript. It adds optional static typing and class-based object-oriented programming to JavaScript, one of the features is Record
4 min read
TypeScript ThisType<Type> Utility Type
The TypeScript ThisType<Type> utility type allows you to define the type of this within a specific object or function context, providing precise type checking for methods and properties accessed through this. The ThisType<Type> utility type is primarily used to provide type annotations f
3 min read
Typescript Partial<Type> Utility Type
The Partial<Type> utility in TypeScript creates a new type by making all properties of an existing type optional. This allows you to create flexible object types where only some properties are required, streamlining code and reducing redundancy when working with complex data structures.What is
3 min read
Typescript Required<Type> Utility Type
TypeScript's Required<Type> utility type creates a new type by making all properties of an existing type mandatory. It removes optionality from each property, ensuring that all fields must be provided, which enhances type safety and reduces potential errors in type definitions.Syntaxtype Requi
3 min read
TypeScript Readonly <Type> Utility Type
In this article, we are going to learn about Readonly<Type> Utility Type in Typescript. Typescript is a popular programming language used for building scalable and robust applications. One of the features of Typescript is Readonly<Type> Utility Type which is used to create a new type whe
2 min read
TypeScript OmitThisParameter<Type> Utility Type
In this article, we are going to learn about OmitThisParameter<Type> Utility Type in Typescript. TypeScript is a popular programming language used for building scalable and robust applications. In TypeScript, the OmitThisParameter<Type> utility type is used to create a new function type
3 min read
TypeScript InstanceType<Type> Utility Type
In this article, we are going to learn about InstanceType<Type> Utility Type in Typescript. TypeScript is a popular programming language used for building scalable and robust applications. In TypeScript, the InstanceType<Type> utility type is used to extract the instance type of a constr
3 min read
TypeScript ReturnType <Type> Utility Type
The ReturnType<Type> utility type in TypeScript extracts and infers the return type of a given function type. It enhances type safety and reusability by allowing developers to dynamically determine the type of values returned by functions without manually specifying them.Syntaxtype ResultTypeV
3 min read
TypeScript NonNullable<Type> Utility Type
In this article, we are going to learn about NonNullable<Type> Utility Type in Typescript. TypeScript is a popular programming language used for building scalable and robust applications. In TypeScript, the NonNullable<Type> utility is used to create a new type by removing null and undef
2 min read