我有一个像这样的枚举(只是大一点):
export enum MyTypeEnum {
one = 'one',
two = 'two',
three = 'three',
four = 'four'
}
Run Code Online (Sandbox Code Playgroud)
我用它来定义需要以这种方式拥有这些键的类型:
export type MyTypeKeyFunctionValue = { [key in MyTypeEnum ]?: Function };
export type MyTypeKeyStringValue = { [key in MyTypeEnum ]?: string };
Run Code Online (Sandbox Code Playgroud)
所以在我的课堂上我可以做这样的事情:
private memberWithFunctions: MyTypeKeyFunctionValue;
Run Code Online (Sandbox Code Playgroud)
MyTypeEnum现在,我遇到了一种特殊情况,当某些成员需要拥有除一个之外的所有键(可以说two),并且我不知道如何定义一种排除该键但保留所有其他键的类型。
有没有办法做到这一点?
您可以只使用Exclude条件类型从enum
export type MyTypeKeyStringValue = {
[key in Exclude<MyTypeEnum, MyTypeEnum.two> ]?: string
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5850 次 |
| 最近记录: |