如何排除字符串中的键?
就像这样:
declare interface PresetConfig {
root?: HTMLElement
tunneling?: boolean,
applets?: {
system?: SystemAppletSettings
frameworks: FrameworksAppletSettings
[key: Exclude<string, 'frameworks' | 'system'>]: AppletSettings | undefined
}
}但Exclude<string, 'frameworks' | 'system'>不起作用。

发布于 2022-05-27 03:39:46
目前似乎不可能从一般的string类型中排除特定的字符串值。在否定类型上已经完成了一些工作,但是还不清楚这是否和什么时候会使它真正进入TypeScript (参见这里,以及一些用例的解决方案这里 )。
但是,对于您的具体情况:如果SystemAppletSettings和FrameworkAppletSettings都扩展了AppletSettings,则可以使用相交类型来获取所需的内容:
interface AppletSettings {a: any};
interface SystemAppletSettings extends AppletSettings {sa: any};
interface FrameworksAppletSettings extends AppletSettings {fa: any};
declare interface PresetConfig {
root?: HTMLElement
tunneling?: boolean,
applets?: {
system?: SystemAppletSettings
frameworks: FrameworksAppletSettings
} & {
[key: string]: AppletSettings
}
}https://stackoverflow.com/questions/72399906
复制相似问题