typescript - 如何在 typescript 界面中表示变量键名?

标签 typescript interface

interface Items {
    id: Item,
}

id 不是可选的,但它会有不同的名称

例如:

let items = {
34433ded : {name: "foo", price: 0.99},
14d433dee : {name: "bar", price: 1.99},
}

最佳答案

类似于:

interface Item {
    name: string;
    price: string;
}

type Items = { [id: string]: Item }

let items = {
    34433ded : {name: "foo", price: 0.99},
    14d433dee : {name: "bar", price: 1.99},
} as Items;

关于typescript - 如何在 typescript 界面中表示变量键名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41045924/

相关文章:

typescript - 将自定义指令绑定(bind)到 Angular2 中的 Observable

c - 这是接口(interface)还是函数原型(prototype)?

delphi - Delphi 中 Form 分发与其生命周期相关的接口(interface)对象的安全方法?

c# - 使用通用方法实现接口(interface)

java - 可以重新初始化接口(interface)变量吗?

javascript - 使用reduceRight删除数组中的最后一个元素

javascript - 带有鸭子类型对象的 typescript 字符串文字

typescript - 无法将已编译的 JS 文件映射到 Electron 的 browserWindow 中的源 TS 文件

javascript - Angular 2点击带有空格的特定单词

PHP 无法从接口(interface)扩展?