JIYIK CN >

Current Location:Home > Learning > PROGRAM > TypeScript >

exclude attribute in TypeScript

Author:JIYIK Last Updated:2025/04/15 Views:

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.

OmitExclude properties from types in TypeScript

TypeScript allows users to exclude properties from types. Users must use Omitthe type to exclude properties from an existing type.

OmitType 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, Omita new type is introduced from an existing type using . InfoThe type has four properties, while Info1has three.

We can even Infoimport 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 Info2is Infoa modified version of type . Note Omithow several properties are excluded from the type.

OmitModify properties in TypeScript using

TypeScript allows users to modify properties. If a user tries to modify Omita 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 info1property countrywill 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 omittype 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 Info1that has a modified countrytype. Notice the syntax for updating the property.

Using Omittypes is a good habit to eliminate complexity and increase productivity.

Previous: None

Next:Checking for undefined in TypeScript

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.

Article URL:

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

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial