【发布时间】:2019-03-13 00:42:49
【问题描述】:
考虑以下示例。
enum DialogType {
Options,
Help
}
class Dialog {
test() : string {
return "";
}
}
class Greeter {
openDialogs: { [key in DialogType]: Dialog | undefined } = {
0: undefined,
1: undefined
};
getDialog(t: DialogType) {
return this.openDialogs[t];
}
}
const greeter = new Greeter();
const d = greeter.getDialog(DialogType.Help);
if (d) document.write(d.test());
它有 3 个问题/问题:
- 为什么我不能在我的初始值设定项文字中省略属性,即使我将属性声明为 '|未定义'
- 为什么我不能使用 'DialogType.Options' 作为类型键,而必须使用硬编码数字?
- 为什么我必须使用 'key in DialogType' 而不是 'key: DialogType'? (或者我可以吗?)
【问题讨论】:
标签: typescript dictionary indexing enums