exclude attribute in TypeScript
This article will discuss excluding properties from a TypeScript type. The exclude property is used to create another type from an existing type with fewer or modified properties.
Omit
Exclude properties from types
in TypeScript
TypeScript allows users to exclude properties from types. Users must use Omit
the type to exclude properties from an existing type.
Omit
Type creates a new type from an existing type.
grammar:
Omit<Type_Name, 'property'>
example:
type Info = {
firstName: string,
lastName: string,
age: number,
country: string,
};
type Info1 = Omit<Info, 'country'>;
const info1: Info1 = {
firstName: 'John',
lastName: 'Nash',
age: 27,
};
console.log(info1);
Output:
{
"firstName": "John",
"lastName": "Nash",
"age": 27
}
In the example above, Omit
a new type is introduced from an existing type using . Info
The type has four properties, while Info1
has three.
We can even Info
import a new type from an existing type and exclude multiple properties.
example:
type Info = {
firstName: string,
lastName: string,
age: number,
country: string,
};
type Info1 = Omit<Info, 'country'>;
const info1: Info1 = {
firstName: 'John',
lastName: 'Nash',
age: 27,
};
type Info2 = Omit<Info, 'country' | 'age'>;
const info2: Info2 = {
firstName: 'Stewen',
lastName: 'Gold',
};
console.log(info1);
console.log(info2);
Output:
{
"firstName": "John",
"lastName": "Nash",
"age": 27
}
{
"firstName": "Stewen",
"lastName": "Gold"
}
As shown above, type Info2
is Info
a modified version of type . Note Omit
how several properties are excluded from the type.
Omit
Modify properties in TypeScript
using
TypeScript allows users to modify properties. If a user tries to modify Omit
a type without a type, an error is thrown.
example:
type Info = {
firstName: string,
lastName: string,
age: number,
country: string,
};
type Info1 = Info & {
country: {
countryName: string,
cityName: string,
};
};
const info1: Info1 = {
firstName: 'John',
lastName: 'Nash',
age: 27,
country: {
countryName: 'China',
cityName: 'Wuhan',
},
}
This code has no output because the object info1
property country
will throw an error. The error message is: Type countryName and cityName is not assignable to type string
.
To overcome this error, the user must use omit
type to modify the type.
example:
type Info = {
firstName: string,
lastName: string,
age: number,
country: string,
};
type Info1 = Omit<Info, 'country'> & {
country: {
countryName: string,
cityName: string,
};
};
const info1: Info1 = {
firstName: 'John',
lastName: 'Nash',
age: 27,
country: {
countryName: 'China',
cityName: 'Wuhan'
},
}
console.log(info1);
Output:
{
"firstName": "John",
"lastName": "Nash",
"age": 27,
"country": {
"countryName": "China",
"cityName": "Wuhan"
}
}
In the example above, notice Info1
that has a modified country
type. Notice the syntax for updating the property.
Using Omit
types is a good habit to eliminate complexity and increase productivity.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Handling exceptions with try..catch..finally in TypeScript
Publish Date:2025/04/15 Views:170 Category:TypeScript
-
This article will discuss try..catch..finally handling exceptions in TypeScript using the statement. Handling exceptions in TypeScript In TypeScript, try..catch..finally a block handles exceptions that occur during program runtime. It allow
Convert JSON object to a class in TypeScript
Publish Date:2025/04/15 Views:80 Category:TypeScript
-
Data on the internet flows in the form of strings, which can be converted into a very popular format called JSON. The JSON representation of this data usually represents an object or even a class in TypeScript. TypeScript provides the abili
Update TypeScript to the latest version using NPM
Publish Date:2025/04/15 Views:119 Category:TypeScript
-
This article provides a guide to update to the latest version of TypeScript using npm, which is the best and most popular method used by developers. This also gives us an in-depth look at what npm is and why to use it. Node Package Manager
Using jQuery and TypeScript
Publish Date:2025/04/15 Views:148 Category:TypeScript
-
This article provides the basic understanding and concepts of using jQuery with TypeScript. It guides on how to use jQuery in TypeScript through a coding example and outputs using various methods in TypeScript and provides knowledge about w
Checking for undefined in TypeScript
Publish Date:2025/04/15 Views:196 Category:TypeScript
-
This article will demonstrate how programmers can check for undefined in TypeScript using various coding examples and situations. It not only gives you an understanding of checking for undefined in TypeScript, but also helps in differentiat
在 TypeScript 中使用 try..catch..finally 处理异常
Publish Date:2023/03/19 Views:396 Category:TypeScript
-
本文详细介绍了如何在 TypeScript 中使用 try..catch..finally 进行异常处理,并附有示例。
在 TypeScript 中使用 declare 关键字
Publish Date:2023/03/19 Views:257 Category:TypeScript
-
本教程指南通过特定的实现和编码示例深入了解了 TypeScript 中 declare 关键字的用途。
在 TypeScript 中 get 和 set
Publish Date:2023/03/19 Views:971 Category:TypeScript
-
本篇文章演示了类的 get 和 set 属性以及如何在 TypeScript 中实现它。
在 TypeScript 中格式化日期和时间
Publish Date:2023/03/19 Views:276 Category:TypeScript
-
本教程介绍内置对象 Date() 并讨论在 Typescript 中获取、设置和格式化日期和时间的各种方法。