How To Exclude A Property From A Type In Typescript

Exclude a Property from a Type in TypeScript

Knowing how to exclude a property from a type in TypeScript will be very helpful if you want to extend and set that type. So how to do it? Today we will show you how to use omit utility types to exclude property. Let’s go into detail now.

Exclude a property from a type in TypeScript

Use omit utility types

Omit utility type will help you get all property from your type, then remove the specific property you indicate.

Syntax:

Omit<type, props>

Parameters:

  • type: indicate the type that you want to exclude the property
  • props: indicate a property that you want to exclude

Example:

type infor = {
    name: string,
    age: number,
    country: string,
    id: number[]
};
  
type person = Omit<infor, "id">;

const Togban: person = {
    name: "Togban",
    age: 18,
    country: "VietNam"
};

console.log(Togban);

Output:

{ name: 'Togban', age: 18, country: 'VietNam' }

Here with Omit, I pass in the first argument is infor type, the second is ‘id’ property. Then a type with all property in Omit type but ‘id’ property has been assigned to person type.

You can also use Omit with an interface in Typescript.

Example:

interface infor {
    name: string,
    age: number,
    country: string,
    id: number[]
};
  
interface person extends Omit<infor, "id"> {
    name: string,
    age: number,
    country: string,
    greeting: string
};
  
const Togban: person = {
    name: "Togban",
    age: 18,
    country: "VietNam",
    greeting: "hello"
};
  
console.log(Togban);

Output:

{ name: 'Togban', age: 18, country: 'VietNam', greeting: 'hello' }

Here with Omit, I create a new type with all property from the infor interface without ‘id’ property. Then my interface person can extend that type.

You can also apply this method to override a new type for your property.

Example:

interface infor {
    name: string,
    age: number,
    country: string,
    id: string[]
};
  
interface person extends Omit<infor, "id"> {
    name: string,
    age: number,
    country: string,
    id: number[]
};
  
const Togban: person = {
    name: "Togban",
    age: 18,
    country: "VietNam",
    id: [11, 12, 13, 14, 15, 16, 17, 18, 19, 10]
};
  
console.log(Togban);

Output:

{ "name": "Togban", "age": 18, "country": "VietNam", "id": [ 11, 12, 13, 14, 15, 16, 17, 18, 19, 10 ] }

Here I override the id property from a string array to a number array with Omit.

Omit with multiple properties

If you want to exclude multiple properties, you can separate properties with the ‘|’ symbol.

Example:

type infor = {
    name: string,
    age: number,
    country: string,
    id: string[]
};
  
type person = Omit<infor, "id" | "age">;

const Togban: person = {
    name: "Togban",
    country: "VietNam"
};
  
console.log(Togban);

Output:

{ name: 'Togban', country: 'VietNam' }

In this example I exclude the ‘id’ and ‘age’ properties at one time by using the ‘|’ symbol. You can also pass in as many properties as you like; you just need to separate that property by the ‘|’ symbol.

Summary

In this tutorial, I showed you how to exclude a property from a type in TypeScript. You can use omit utility types to remove the property that you indicated. If you have any problems, please comment below. Thank you for reading!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *