【问题标题】:Cannot find name 'PropertyKey'找不到名称“PropertyKey”
【发布时间】:2017-08-01 23:49:45
【问题描述】:

我正在尝试在一个项目上运行 Webpack,但出现多个错误

node_modules/@types/core-js/index.d.ts
error TS2304: Cannot find name 'PropertyKey'.
...
node_modules/@types/core-js/index.d.ts
error TS2339: Property 'for' does not exist on type 'SymbolConstructor'.

我应该安装我所有的打字,所以我不确定它们来自哪里。我尝试从可编译的项目中复制 package.json,但没有帮助。我错过了什么?

我的 tsconfig 看起来像这样

{
  "compilerOptions": {
    "target": "es5",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "sourceMap": true,
    "noImplicitAny": false,
    "suppressImplicitAnyIndexErrors": true
  },
  "include": [
    "src/**/*"
  ],
  "exclude": [
    "node_modules",
    "**/*.spec.ts"
  ]
}

【问题讨论】:

    标签: angular webpack


    【解决方案1】:

    我在 0.9.35 遇到了与 @types/core-js 相同的错误

    可能是别的东西..

    编辑

    上周回购发生了一些变化。您可以阅读 github 上的问题并通过以下链接查看更改:

    https://github.com/DefinitelyTyped/DefinitelyTyped/issues/15104

    https://github.com/DefinitelyTyped/DefinitelyTyped/pull/15108/commits/f2c5c990e448550fcebec071c25e6a1e5766dde7

    我的解决办法是改变

    "lib": ["es5", "dom"]"lib": ["es6", "dom"] 在我的 tsconfig 文件中的 compilerOptions 对象中。

    通过这样做,我在没有降级到 0.9.35 的情况下使错误消失

    注意:你不需要改变目标(我的还是es5)

    【讨论】:

    • 它对我有用。但是,我有两台计算机都运行带有最新更新和软件包的 VS2015 Pro,一台有问题,可以通过这个答案解决,另一台不需要。有点奇怪。
    • 也许有人没有在正确的地方寻找 tsconfig?或者,也许一个拥有类型的升级版本,而另一个拥有另一个。
    • 改成 ES6 不会破坏 IE 11 操作吗?换句话说,对于 ES6 和浏览器合规性有什么影响?
    • 只要目标还是es5,你的代码应该可以在IE中运行。
    【解决方案2】:

    对我来说,答案是……

    "compilerOptions": {
      ...
      "lib": [
        "es2016",
        "dom"
      ]
    },
    

    【讨论】:

      【解决方案3】:

      将您的 @types/core-js 降级到 0.9.35。

      升级到 0.9.37 后我也遇到了同样的问题。

      【讨论】:

      • 我将我的版本降级到 0.9.36 并且它可以工作。谢谢:D
      【解决方案4】:

      可以确认加载版本 0.9.35 已为我修复此问题。

      "devDependencies": {
          ...
          "@types/core-js": "0.9.35",
          ...
      }
      

      【讨论】:

        【解决方案5】:

        降级为 "@types/core-js": "^0.9.35" 并将 lib 添加到我的 compilerOptions。

        "devDependencies": {
            "@types/core-js": "^0.9.35",
            ......
        }
        

        tsconfig.json

        {
        "compilerOptions": {
            ........
            "lib": [
                "es2016",
                "dom"
            ]
        },
        "exclude": [
            "node_modules",
            ...
        ]
        }
        

        【讨论】:

        • 添加“@types/core-js”:“^0.9.35”后,需要运行npm命令吗?像 npm update 什么的?这仍然不适合我。
        • 我使用link 删除了“npm_modules”文件夹,然后再次运行命令“npm install”
        【解决方案6】:

        我通过在 tsconfig.json 中添加此代码解决了我的问题

        { “编译器选项”:{

        "experimentalDecorators": true,
        "moduleResolution": "node",
          "lib": ["es2017", "dom"] //Please add this code in your tsconfig.json
        

        }, “排除”: [ “节点模块” ] }

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-01-24
          • 1970-01-01
          • 2021-10-29
          • 2019-11-17
          • 2018-03-27
          • 2018-06-28
          • 2021-06-23
          相关资源
          最近更新 更多