Can't find 'Map' in TypeScript

I am very new to WebStorm, on a trial.  I create a trivial TypeScript file which runs fine, but the word Map is in red and says TS2304: Cannot find name 'Map'.  Since Map is available in all browsers, should not the default project (node.js) include this?  What do I have to do to get Map to be recognized?

10 comments
Comment actions Permalink

>Since Map is available in all browsers, should not the default project (node.js) include this?

Why should it? default Node.js Express project doesn't include any typescript stuff, the fact that certain types are known to all browsers doesn't mean that they are implemented by Node.js/V8/TypeScript compiler - all these are different things.

Do you have the most recent node.d.ts available in your project? What Typescript compiler version do you use?

-2
Comment actions Permalink

My apologies; I should not have said "browsers".  I was just remarking that something that runs just fine (when compiled to js) should probably have the definitions pre-populated.  I was assuming that it would set it up for me.

I understand I need a node.d.ts.  Where does it come from and where does it go?  Specifically, where does it go in the project?  Do I need a reference?

Compiler version is showing v1.7.3.

 

0
Comment actions Permalink

Well, TypeScript compiler produces the javascript code unless the errors are severe. In your case, it can't find the d.ts files imported by your module - such errors don't normally prevent javascript from being emitted, so your code runs, but compiler errors are still thrown.

node.d.ts can be downloaded from https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/node/node.d.ts. You need to place it in your project and either include in your file using ///<reference path> comments, or set up tsconfig.json accordingly

 

-1
Comment actions Permalink

Well, I have experimented at length, and am apparently still missing something, since the "Map" type is still unresolved.  I have tried with tsconfig.json and without, with all logical compiler settings I could imagine, and still no luck.  I have node.d.ts in the path.  I have a line 

/// <reference path="node.d.ts" />

at the top of my ts file, which definitely reacts to the presence of the node.d.ts file (it highlights if I take the file away).  But no matter what I do, I can't seem to get intellisense style type support for "Map".

tsconfig.json looks like this:

{
"compilerOptions": {
"module": "commonjs",
"target": "es5",
"sourceMap": true
},
"exclude": [
"node_modules"
]
}

Does it need something else?

 

0
Comment actions Permalink

>am apparently still missing something, since the "Map" type is still unresolved

 

Do you mean that you still get compiler errors for it? Can you attach your .ts file?

-1
Comment actions Permalink

Here is the code.

/**
* Created by jgg on 3/2/2016.
*/
/// <reference path="node.d.ts" />

class MapUser {
internal_map: Map;
constructor() {
this.internal_map = new Map();
}
add(key: string, val: any) : number {
this.internal_map.set(key, val);
return this.internal_map.size;
}
}

var mu: MapUser = new MapUser();
mu.add("one", 1);
mu.add("two", 2);
mu.add("one", "uno");
console.log(mu);

The two inclusions of "Map" appear in red, and the tool tip says "TS2304: Cannot find the name 'Map'."

The code runs just fine, and outputs:
"C:\Program Files (x86)\JetBrains\WebStorm 11.0.3\bin\runnerw.exe" "C:\Program Files\nodejs\node.exe" test2.js
MapUser { internal_map: Map { 'one' => 'uno', 'two' => 2 } }

Process finished with exit code 0



0
Comment actions Permalink

Thanks! Indeed, recent node.d.ts doesn't include ES6 types definitions. You need using other d.ts. files. There are several libraries you can try - https://github.com/Microsoft/TypeScript/blob/master/lib/lib.core.es6.d.ts, or https://github.com/WebReflection/es6-collections, or https://github.com/es-shims/es6-shim - whatever you like. All the Typescript compiler needs is a declaration for the ES6 constructs you want to use.

Note that almost all them declare map as a generic type, so you will likely face errors like 'Error:(7, 19) TS2314: Generic type 'Map<K, V>' requires 2 type argument(s).'

 

See also http://stackoverflow.com/questions/33989459/typescript-use-lib-core-es6-d-ts

-1
Comment actions Permalink

Had this same problem just now. Thing that fixed it for me was to go to Settings > Languages & Frameworks > Javascript then switched the JavaScript language version ECMAScript 6 (it was at ECMAScript 5.1).

Best I can figure, even for a .ts file, WebStorm is using the Javascript settings, explaining why Maps/Sets/other new ES6 features show as errors in the text editor unless you set Javascript to ECMAScript 6.

0
Comment actions Permalink

>Best I can figure, even for a .ts file, WebStorm is using the Javascript settings, explaining why Maps/Sets/other new ES6 features show as errors in the text editor unless you set Javascript to ECMAScript 6.

 

no, nothing of the kind.

What errors do you see namely? Please provide screenshot of the error message and screenshot of your TypeScript preferences (Preferences | Languages & Frameworks | TypeScript)

0
Comment actions Permalink

I encountered this unexpectedly today with `Object` (a global in the ES spec for eons). Not sure why, but when I changed Settings -> Languages & Frameworks -> TypeScript -> Node interpreter (from 8.x) to the version I was using in my nvm shell (10.11.0) it started working again.

0

Please sign in to leave a comment.