打字稿中的变量键作为对象成员

时间:2014-08-19 21:28:55

标签: typescript

我正在寻找一个对象的接口,该对象具有映射到字符串值的键的字符串。这似乎是一件相当简单的事情,但我还没有能够

显示错误here

我已经退房了 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);

最后一行反映了我想要完成的事情

1 个答案:

答案 0 :(得分:4)

这是设计的。因为它只有您需要使用的索引签名。

console.log(x['question1']
相关问题