Stéphane Piette
Stéphane Piette

Reputation: 5421

ES6 Map.forEach is not a function after typescript compilation

I am trying to use the new collection types of ES6 in my typescript/react project.

interface MyComponentProps{
  myMap: Map<String, {isAvailable?: boolean}>,
}
...
this.props.myMap.keys();

IntelliJ and Webpack can compile my code without warnings, but I got an error at runtime in Chrome 55. this.props.myMap.forEach is not a function

tsconfig.json

{
"compilerOptions": {
"moduleResolution": "node",
"outDir": "./dist/",
"sourceMap": true,
"noImplicitAny": true,
"module": "commonjs",
"target": "es6",
"jsx": "react"
  }
}

package.json

{
 "name": "cocktails-db",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
  "production": "webpack -p",
  "start": "webpack-dev-server -d",
  "test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
  "axios": "^0.15.3",
  "elasticsearch": "^12.1.0",
  "react": "^15.4.0",
  "react-bootstrap": "^0.30.7",
  "react-dom": "^15.4.0",
  "react-router": "^3.0.0",
  "react-router-bootstrap": "^0.23.1",
  "react-select2-wrapper": "^1.0.4-beta1"
},
"devDependencies": {
  "@types/axios": "^0.9.34",
  "@types/bootstrap": "^3.3.32",
  "@types/elasticsearch": "^5.0.1",
  "@types/react": "^0.14.51",
  "@types/react-bootstrap": "0.0.37",
  "@types/react-dom": "^0.14.19",
  "@types/react-router": "^2.0.41",
  "@types/react-router-bootstrap": "0.0.27",
  "html-webpack-plugin": "^2.24.1",
  "ts-loader": "^1.2.2",
  "typescript": "^2.0.10",
  "webpack": "^1.13.3",
  "webpack-dev-server": "^1.16.2"
}
}

I still can use the Map type when type in the browser's console, so I guess it's a problem of typescript compilation. I am missing a dependency somewhere ?

--- Edit --- It was actually an initialization problem, but I still don't know why I don't get Typescript feedback/type warning.

This is how I call ´MyComponent´

myMap : any;
...
this.myMap="";
...
render() {
        return (
            <div>
                <MyComponent myMap={this.myMap}> </MyComponent>
            </div>
        )

Upvotes: 1

Views: 2364

Answers (1)

St&#233;phane Piette
St&#233;phane Piette

Reputation: 5421

Problem solved; That had nothing to do with a compilation problem, but because wrong object initialisation.

Upvotes: 2

Related Questions