Holden1515
Holden1515

Reputation: 709

Unable to use Map.Keys() function

I'm able to create and manipulate a Map object, but I do not have access to the .keys() or .values() functions.

How can I figure why this is?

let a = new Map([['a', 1], ['b', 2]]);
a.set('c', 3);

let myArray = a.keys();

I get red underline under keys().

Error:

[15:52:12] Error - typescript - src\...TruckDeliverySchedule.tsx(102,21): error TS2339: Property 'keys' does not exist on type 'Map<string, number>'.

I'm not sure what else would be relevant. Very new javascript programming.

EDIT:

  "compilerOptions": {
    "target": "es5",
    "forceConsistentCasingInFileNames": true,
    "module": "esnext",
    "moduleResolution": "node",
    "jsx": "react",
    "declaration": true,
    "sourceMap": true,
    "experimentalDecorators": true,
    "skipLibCheck": true,
    "typeRoots": [
      "./node_modules/@types",
      "./node_modules/@microsoft"
    ],
    "types": [
      "es6-promise",
      "webpack-env"
    ],
    "lib": [
      "es5",
      "dom",
      //"es2015.collection"  // removed
      "es2015"
    ]

EDIT 2:

Project was built for a Sharepoint Framework and using Visual Studio Code.

When I try to look at the definition for Map, I takes me to:

// Backward compatibility with --target es5
declare global {
    interface Set<T> { }
    interface Map<K, V> { }
    interface WeakSet<T> { }
    interface WeakMap<K extends object, V> { }
}

Upvotes: 0

Views: 1186

Answers (1)

jcalz
jcalz

Reputation: 329198

In the compilerOptions for your tsconfig.json file, you have:

"lib": [
  "es5",
  "dom",
  "es2015.collection"
]

So Map was introduced in ECMAScript 2015, but you are explicitly including only the es2015.collection TypeScript library, instead of the library for all of ECMAScript 2015. The particular methods you are missing are present in the es2015.iterable library. I'd suggest that, if you are targeting an environment that supports all of ES2015, you should change es2015.collection to es2015 so you get the whole thing instead of just pieces of it.

Hope that helps. Good luck!

Upvotes: 2

Related Questions