Home > database >  In TypeScript, how can a declaration omit multiple methods from a base type?
In TypeScript, how can a declaration omit multiple methods from a base type?

Time:02-09

While searching for ways to fix errors in a d.ts file, I need to omit a few methods from the base type because the custom class I'm working on redefines them.

I found the Omit helper type, which is used in examples like this one :

type Foo = {
  a() : void;
  b() : void;
  c() : void;
  toString() : string;
};
type BaseFoo = Omit<Foo, "a">;

However, what if I need to remove both a, b, and c in BaseFoo?

It seems that I can do

type BaseFoo = Omit<Omit<Omit<Foo, "a">, "b">, "c">; 

But is there a cleaner way to do this?

CodePudding user response:

Yes, use union:

type BaseFoo = Omit<Foo, 'a'|'b'|'c'>; 

Or just use Pick<Foo,'toString'>

  •  Tags:  
  • Related