typescript - 以下语法是什么意思?

标签 typescript interface

我看到这样的代码

export interface SmartType {
    type: string;
    [x: string]: any;
}
    

谁能解释最后一行定义 x 的含义?

最佳答案

这意味着 SmartType 可以用 string 索引,值是 any 类型。

interface SmartType {
    type: string;
    [x: string]: any;
}

let obj: SmartType = { type: 'Thing' };
obj['test'] = 'Hello World';
obj['test2'] = 10;
console.log(obj);

( jsFiddle )

typescript 手册在其关于 Interfaces: Indexable Types 的部分对此进行了解释.

关于typescript - 以下语法是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48213682/

相关文章:

typescript - 声明参数是类的函数,返回该类的实例?

javascript - Angular2 表单重置,表单重置或提交后取消选中 radio

具有多种实现的 Java 通用接口(interface)

java - 如何返回接口(interface)对象?

java - Java中的接口(interface)是什么?

typescript - 为什么我需要在我的声明中使用类型断言将变量分配给 null?

typescript - 是否可以在 package.json bin 中使用本地安装的 ts-node?

Angular 4 : Update Component View after Async Call to Server

interface - 在 Lazarus/freepascal 中编译缓冲文件流单元时出错

java - 接口(interface)真的没有构造函数吗?