6

在发生这种情况之前,我在本机脚本项目方面取得了良好进展:

JS: EXCEPTION: Uncaught (in promise): ReferenceError: __assign is not defined

这是从这行代码冒出来的:

return [...state, { ...action.payload, success: false }];

这是我的tsconfig.json

{
    "compilerOptions": {
        "module": "commonjs",
        "target": "es5",
        "experimentalDecorators": true,
        "emitDecoratorMetadata": true,
        "noEmitHelpers": true,
        "noEmitOnError": true
    },
    "exclude": [
        "node_modules",
        "platforms",
        "**/*.aot.ts"
    ]
}

Typescript 似乎没有__assign在编译的源代码中包含它的辅助函数——这是他们实现对象扩展语法的方式。你们中的任何一个好人会碰巧知道为什么吗?

4

2 回答 2

14

我很高兴地报告我找到了解决方案。这个GitHub repo很好地解释了事情,但这里有一个快速的纲要:

tsconfig.json 中的标志告诉 Typescript在每个需要它们的文件中noEmitHelpers省略这些“助手”(例如)。__assign

{
  "compilerOptions": {
    // changing this to false does the job, but duplicates helpers across every file
    "noEmitHelpers": false
  }
}

最新的 Typescript 提供了一种更好的方法来管理这个,使用标志importHelpers(参见编译器选项):

{
  "compilerOptions": {
    "noEmitHelpers": true,
    "importHelpers": true // better
  }
}

这将使对象传播工作,并避免跨文件的代码重复。

您可能还需要npm install tslib --save停止 IDE 错误。

于 2017-02-17T18:22:57.300 回答
0

您是否尝试过添加"lib": "es6"到您的 tsconfig 中?

于 2017-02-16T03:54:36.460 回答