在打字稿中映射类给出错误

时间:2016-09-13 05:29:18

标签: javascript typescript

我在打字稿中收到“无法找到名字地图”的错误。

var myMap = new Map();

var keyString = "a string",
    keyObj = {},
    keyFunc = function () {};

// setting the values
myMap.set(keyString, "value associated with 'a string'");
myMap.set(keyObj, "value associated with keyObj");
myMap.set(keyFunc, "value associated with keyFunc");

myMap.size; // 3

// getting the values
myMap.get(keyString);    // "value associated with 'a string'"
myMap.get(keyObj);       // "value associated with keyObj"
myMap.get(keyFunc);      // "value associated with keyFunc"

myMap.get("a string");   // "value associated with 'a string'"
                         // because keyString === 'a string'
myMap.get({});           // undefined, because keyObj !== {}
myMap.get(function() {}) // undefined, because keyFunc !== function () {}

我不明白为什么不考虑它

此外,我们在javascript中有Map

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:0)

Map ES6 的一部分,因此您需要“定位” ES6 才能生效。

TypeScript 2 中,您可以继续定位 ES5 ,并在 lib 选项中包含所需的apis:

"compilerOptions": {
    "lib": ["es5", "es2015.collection"]
}

更多信息here