typescript - "__assign is not defined"- NativeScript 对象传播问题

标签 typescript nativescript spread-syntax

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

JS: EXCEPTION: Uncaught (in promise): ReferenceError: __assign 未定义

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

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

最佳答案

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

tsconfig.json 中的标志 noEmitHelpers 告诉 Typescript 在每个需要它们的文件中省略这些“助手”(例如 __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 错误。

关于typescript - "__assign is not defined"- NativeScript 对象传播问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42264304/

相关文章:

javascript - '类型错误: Cannot set properties of undefined (setting '0' )'----Array assignment

javascript - typescript : Argument of type 'any[]' is not assignable to parameter of type '[]' . ts(2345)

android - 如何在 NativeScript 中创建自定义导航栏?

nativescript - 我可以在 nativescript 中运行 http 服务器吗?

javascript - 自定义类扩展 Array 而不使用扩展语法

javascript - 正确实例化复杂的 JS 对象

angular - 如何在http调用 Angular 2的catch block 中读取响应主体

Nativescript 选项卡布局方法

Javascript 传播与继承

typescript - 在 TypeScript 中键入 rest 运算符对象