我有一个系统,它接受 key:object 对,然后让您稍后在应用程序中获取它们。
问题是这个索引签名会中断输入,因为它允许任何键存在,所以你不能输入 get() 请求。
下面是应用程序和问题的摘要片段。
我试过删除这个索引签名,这是一个问题,因为那时setting[key].value
inclass Configuration
不存在。我曾尝试删除基本设置界面,但这基本上会破坏您对输入内容所做的任何更改。我已经尝试了很多辅助函数和守卫来试图强制应用程序意识到它是一个设置。我还没有到我必须接受在 get 方法上不输入的地步。
class Setting {
constructor(
public value: number,
private locked: boolean
) { }
}
class Settings {
[key: string]: Setting; // Comment me out: errors (but typing), leave me in: no errors (no typing)
}
interface SettingsCountryRank extends Settings {
taiwan: Setting;
china: Setting;
}
class Configuration<TSettings extends Settings> {
settings: TSettings;
public get<U extends keyof TSettings>(key: U): TSettings[U]['value'] {
return this.settings[key].value;
}
constructor(settings: TSettings) {
this.settings = settings;
}
}
function main(){
const test = new Configuration<SettingsCountryRank>({ taiwan: new Setting(1, true), china: new Setting(2, false) });
test.get('china')
test.get('notathing');
}
当您打开和关闭线路时,Settings
您可以看到问题。