在发生这种情况之前,我的原生脚本项目进展顺利:
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/