【问题标题】:Variable keys in typescript as object members打字稿中的变量键作为对象成员
【发布时间】:2014-10-13 03:16:17
【问题描述】:

我正在为一个对象创建一个接口,该对象具有用于将键映射到字符串值的字符串。这似乎是一件相当简单的事情,但我一直无法

错误显示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);

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

【问题讨论】:

    标签: typescript


    【解决方案1】:

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

    console.log(x['question1']
    

    【讨论】:

      猜你喜欢
      • 2017-06-19
      • 1970-01-01
      • 2019-01-02
      • 1970-01-01
      • 2021-03-05
      • 2016-11-24
      • 1970-01-01
      • 1970-01-01
      • 2020-06-29
      相关资源
      最近更新 更多