我在typescript中创建了一个类,它的属性是ES6 (ECMAscript 2016) Map,如下所示:

class Item {
  configs: ????;
  constructor () {
    this.configs = new Map();
  }
}

我如何在typescript中声明一个ES6 Map类型?


当前回答

这里有一个例子:

this.configs = new Map<string, string>();
this.configs.set("key", "value");

Demo

其他回答

我如何在typescript中声明一个ES6 Map类型?

您需要目标——模块es6。这是不幸的,你可以在这里提出你的关注:https://github.com/Microsoft/TypeScript/issues/2953#issuecomment-98514111

在tsconfig中添加"target": "ESNEXT"属性。json文件。

{
    "compilerOptions": {
        "target": "ESNEXT" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', or 'ESNEXT'. */
    }
}

这里有一个例子:

this.configs = new Map<string, string>();
this.configs.set("key", "value");

Demo

最起码:

tsconfig:

 "lib": [
      "es2015"
    ]

如果你想要IE < 11支持,安装一个polyfill如https://github.com/zloirock/core-js: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map

Typescript还不支持Map。

ES6兼容性表