【问题标题】:Declare a named type like a dictionary像字典一样声明一个命名类型
【发布时间】:2016-10-16 08:04:43
【问题描述】:

我可以在打字稿中创建像字典一样的类型吗?例如 bar { [id: string] : IPerson; } 这是一个字典的类型,我怎样才能给它一个名字,比如说 Dict 并使用这个名字而不是整个 { [id: string] : IPerson; }

class Foo {
  bar:{ [id: string] : IPerson; }
}

我从服务器收到的数据是这样的

{
  "15" : { name: "James", age: 45},
  "34" : { name: "Mary", age: 22},
  ...
}

【问题讨论】:

标签: dictionary typescript


【解决方案1】:

interface:

interface Dict { 
  [id: string] : IPerson
}
class Foo {
  bar: Dict
}

【讨论】:

    猜你喜欢
    • 2014-07-24
    • 1970-01-01
    • 1970-01-01
    • 2016-10-09
    • 1970-01-01
    • 2010-12-22
    • 1970-01-01
    • 2015-06-03
    • 1970-01-01
    相关资源
    最近更新 更多