我想为一个对象创建一个接口(interface),该对象具有用于将键映射到字符串值的字符串。这似乎是一件相当简单的事情,但我没能做到
显示错误 here
我已经 checkout Is it possible to define an object containing objects?和 Enforcing the type of the indexed members of a Typescript object?但我无法正确访问这些值。
这是代码
interface ISomeQuestions {
[key:string] : string;
}
var x:ISomeQuestions = {
"question1": "",
"question2": "",
"a": ""
};
console.log(x.question1);
最后一行反射(reflect)了我正在努力完成的事情
最佳答案
这是设计使然。因为它只有您需要使用的索引签名。
console.log(x['question1']
关于typescript - typescript 中的变量键作为对象成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25393288/