Categories
TypeScript Answers

How to override interface property type defined in Typescript d.ts file?

Spread the love

To override interface property type defined in Typescript d.ts file, we can omit properties from them and extend them.

For instance, we write

interface A {
  x: string;
}

export type B = Omit<A, "x"> & { x: number };

to create type B that removes the existing x property with Omit from interface A and add the x property with type number.

We can also create a new interface from an existing one with Omit and extends.

For instance, we write

interface A {
  x: string;
}

interface B extends Omit<A, "x"> {
  x: number;
}

to create interface B that extends interface A without the x property by extending Omit<A, "x">.

Then we add the x property again with type set to number.

Conclusion

To override interface property type defined in Typescript d.ts file, we can omit properties from them and extend them.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published.