typescriptinterface

How to Omit an interface from an interface


I would want to know if it is possible to omit several keys at once without having to specify them all in case they are already known from a specific interface.

Let's imagine I have the following interfaces :

interface A {
  a: number;
  b: number;
  c: number;
  d: number;
}

interface B extends A {
  e: string;
  f: string;
}

I would want to have a type referring to the following object :

const a: SomeType = {
  e: 'foo',
  f: 'bar',
};

I could do something like this but it would be very redundant :

type OmitWithKeys = Omit<B, 'a' | 'b' | 'c' | 'd'> 

And it would be the same problem for this solution if B has many keys :

type BWithoutExtend = {
  e: string;
  f: string;
};

Is there any trick to achieve this ?


Solution

  • type BWithoutExtend = Omit<B, keyof A>;
    

    enter image description here