在 TypeScript 中,一个声明如何从一个基类型中省略多个方法?
In TypeScript, how can a declaration omit multiple methods from a base type?
在寻找修复 d.ts
文件中的错误的方法时,我需要从基本类型中省略一些方法,因为我正在处理的自定义 class 重新定义了它们。
我找到了 Omit
助手类型,它被用在像这样的例子中:
type Foo = {
a() : void;
b() : void;
c() : void;
toString() : string;
};
type BaseFoo = Omit<Foo, "a">;
但是,如果我需要删除 BaseFoo
中的 a
、b
和 c
怎么办?
看来我可以
type BaseFoo = Omit<Omit<Omit<Foo, "a">, "b">, "c">;
但是有更简洁的方法吗?
是,使用联合:
type BaseFoo = Omit<Foo, 'a'|'b'|'c'>;
或者直接使用Pick<Foo,'toString'>
在寻找修复 d.ts
文件中的错误的方法时,我需要从基本类型中省略一些方法,因为我正在处理的自定义 class 重新定义了它们。
我找到了 Omit
助手类型,它被用在像这样的例子中:
type Foo = {
a() : void;
b() : void;
c() : void;
toString() : string;
};
type BaseFoo = Omit<Foo, "a">;
但是,如果我需要删除 BaseFoo
中的 a
、b
和 c
怎么办?
看来我可以
type BaseFoo = Omit<Omit<Omit<Foo, "a">, "b">, "c">;
但是有更简洁的方法吗?
是,使用联合:
type BaseFoo = Omit<Foo, 'a'|'b'|'c'>;
或者直接使用Pick<Foo,'toString'>