“__assign 未定义”- NativeScript 对象传播问题

"__assign is not defined" - NativeScript Object Spread Woes

在发生这种情况之前,我的原生脚本项目进展顺利:

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 函数——这是他们实现对象传播语法的方式。你们中的任何好人碰巧知道为什么吗?

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

我很高兴地报告我找到了解决这个问题的方法。这个 GitHub repo 很好地解释了事情,但这里有一个简短的摘要:

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

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

最新的 Typescript 提供了一种更好的方法来管理它,使用标志 importHelpers(参见 compiler options):

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

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

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