对象类型在确定类型文件中的应用

时间:2019-05-07 08:04:28

标签: javascript types intellisense jsdoc .d.ts

是否可以在绝对类型的文件中创建对象类型的应用程序表达式?

在普通的JSDoc中,您可以使用以下所示的方式对其进行定义:

/* @type {Object.<string, number>} */
var x = {
  "0": 0,
  "1": 1
};

但是在.d.ts文件中不会解析相同的东西(变量的类型为any)。

const x: Object.<string, number>; 

1 个答案:

答案 0 :(得分:1)

您可以使用索引器或内置的Record类型:

const x: {[key: string]: number};
const x: Record<string, number>;

他们都会做同样的事情。

有关“记录”类型的更多信息:What is the Record type in typescript?