Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exclude properties from interface while inheriting

Tags:

typescript

I've two interfaces, X and Y. X has 2 properties, x1 and x2. Now Y wants to inherit from X but don't want x2 to be inherited.

interface X {
  x1 : string;
  x2 : string;
}

interface Y extends X{
  // x2 shouldn't be available here
}

As am new in TypeScript, I can't figure it out. Is there any extends X without x1 type built-in feature available in TypeScript ?

NOTE: In my real case, X is a built-in interface. So, I need to do this without changing the X interface. Is it possible ?

like image 818
theapache64 Avatar asked Jun 27 '18 13:06

theapache64


People also ask

How to Exclude field from interface TypeScript?

Use the Omit utility type to exclude a property from a type, e.g. type WithoutCountry = Omit<Person, 'country'> . The Omit utility type constructs a new type by removing the specified keys from the existing type.

What is omit in TypeScript?

The TypeScript Omit utility type Like the Pick type, the Omit can be used to modify an existing interface or type. However, this one works the other way around. It will remove the fields you defined. We want to remove the id field from our user object when we want to create a user.

Should I use type or interface TypeScript?

The official Typescript documentation also says: ... we recommend using an interface over a type alias when possible. This also suggests that the type is more intended for creating type aliases than creating the types themselves.


1 Answers

Typescript >3.5

TypeScript 3.5 introduced the Omit helper type, which creates a new type with some properties dropped from the original. The example from the docs:

type Person = {
    name: string;
    age: number;
    location: string;
};

type QuantumPerson = Omit<Person, "location">;

// equivalent to
type QuantumPerson = {
    name: string;
    age: number;
};

Thanks to Jeremy for the update on Typescript 3.5!

Typescript >2.8

This can be implemented using the Pick and Exclude types introduced in Typescript 2.1 and 2.8:

/**
 * From T pick a set of properties K
 */
type Pick<T, K extends keyof T> = {
    [P in K]: T[P];
};

/**
 * Exclude from T those types that are assignable to U
 */
type Exclude<T, U> = T extends U ? never : T;

With these type definitions you can construct Omit<T,K> to omit specific properties from a generic type:

/**
 * From T pick all properties except the set of properties K
 */
export type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;

To state the Typescript 2.8 Release Notes why this type is not included in Typescript:

We did not include the Omit type because it is trivially written as Pick<T, Exclude<keyof T, K>>.

Although it is not included in Typescript several libraries provide their own similar Omit type, including react-redux or Material-UI.

Here is a working example:

interface X {
  x1: string;
  x2: string;
}

type Y = Omit<X, 'x2'>;

let x: X = {
  x1: 'string1',
  x2: 'string2'
}

let y: Y = {
  x1: 'string1'
}

Example of Omit

Note that the properties to exclude will be checked, it is an error to exclude properties which are not defined in the specified type:

Example of Omit using non-existent property name

like image 53
sn42 Avatar answered Oct 23 '22 05:10

sn42