How to remove fields from a TypeScript interface via extension
TypescriptTypescript Problem Overview
Let's say there's an interface that I don't have control over:
interface Original {
name: string;
size: number;
link: SomeType;
}
I want to extend this interface, but actually remove link
so I end up with a new interface that is effectively:
interface OriginalLite {
name: string;
size: number;
}
How can I do this?
Typescript Solutions
Solution 1 - Typescript
You can use mapped and conditional types to achieve this. Usually we call what you're trying to do Omit
:
type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>
(for TS3.5+, the Omit
utility type is included in the standard library, so you don't have to define it yourself anymore)
So Omit<T, K>
is basically the same as T
but with none of the properties in K
. You can do the following to make a new interface
called OriginalLite
which is missing only link
:
interface OriginalLite extends Omit<Original, 'link'> {}
Note that you are not extending Original
. That's because to "extend" in TypeScript means to make a type more specific (a.k.a. "narrowing"), but in your case what you are doing is making it less specific (a.k.a. "widening"). So that's why extends Original
does not appear in that definition. I assume that's okay with you.
Let's test it:
declare const originalLite: OriginalLite;
originalLite.name; // string
originalLite.size; // number
originalLite.link; // error, property 'link' does not exist on type 'OriginalLite'
Looks good. Hope that helps. Good luck!