【问题标题】:ES6 Map.forEach is not a function after typescript compilationES6 Map.forEach 不是打字稿编译后的函数
【发布时间】:2017-04-19 21:43:08
【问题描述】:

我正在尝试在我的 typescript/react 项目中使用新的 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 编译的问题。 我在某处缺少依赖项?

--- 编辑 --- 这实际上是一个初始化问题,但我仍然不知道为什么我没有收到 Typescript 反馈/类型警告。

这就是我所说的“我的组件”

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

【问题讨论】:

  • 请添加更多相关代码。你确定你在传递地图吗?
  • 预计 ES6 的 polyfill 特性将被 polyfill。很明显,您的情况并非如此。
  • @estus 不需要 pollyfill,chrome 支持Map 对象
  • @NitzanTomer 该问题没有指定使用哪个 Chrome 版本,但通常网络应用程序客户端不限于 Chrome。虽然它不太可能有 Map 但没有 Map.prototype.forEach。
  • @estus 它确实说 chrome 55,它支持 Map。这可能是另一个问题,OP 可能没有传递地图实例...

标签: javascript typescript ecmascript-6 ts-loader


【解决方案1】:

问题解决了;这与编译问题无关,而是因为错误的对象初始化。

【讨论】:

    猜你喜欢
    • 2021-09-07
    • 2017-02-16
    • 2013-08-19
    • 1970-01-01
    • 2019-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多