我正在尝试在我的打字稿/反应项目中使用新的collection types of ES6。
interface MyComponentProps{
myMap: Map<String, {isAvailable?: boolean}>,
}
...
this.props.myMap.keys();
IntelliJ和Webpack可以在没有警告的情况下编译我的代码,但是我在运行时在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"
}
}
在浏览器控制台中键入时我仍然可以使用Map类型,所以我猜这是打字稿编译的问题。 我在某个地方错过了依赖?
---编辑--- 这实际上是一个初始化问题,但我仍然不知道为什么我没有得到Typescript反馈/类型警告。
这就是我称之为'MyComponent'的方式
myMap : any;
...
this.myMap="";
...
render() {
return (
<div>
<MyComponent myMap={this.myMap}> </MyComponent>
</div>
)