我看到这样的代码
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/