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

Sometimes, we want to override interface property type defined in Typescript d.ts file.

In this article, we’ll look at how to override interface property type defined in Typescript d.ts file.

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

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.