简体   繁体   English

该语法需要一个名为 __spreadArrays 的导入助手

[英]the syntax requires an imported helper named __spreadArrays

When I'm trying to use spreading in typescript like:当我尝试在 typescript 中使用传播时,例如:

.reduce((unique, x) => {
    unique.some(element => element.machineName === x.machineName) ? unique : [...unique, x];
}

I get warning with red marking from Visual Studio Code :我从Visual Studio Code收到带有红色标记的警告:

the syntax requires an imported helper named __spreadArrays

typescript version: "2.3.4"
target: "es5"

I think in later versions this issue is fixed, but right now I can't migrate.我想在以后的版本中这个问题是固定的,但现在我不能迁移。 How can I fix it?我该如何解决?

Note: Project gets compiled fine and works.注意:项目可以正常编译并运行。 But red underlying in VS Code in annoying.但是VS Code中的红色底层很烦人。

For VS Code the notification is based on the tsLib exports.对于VS Code ,通知基于tsLib导出。 Actually nothing bad happens without doing anything, it is just lack of typing.实际上,不做任何事情都不会发生任何不好的事情,只是缺少打字而已。

Solution: Update tsLib dependency to get rid of the highlighting.解决方案:更新tsLib依赖项以摆脱突出显示。 In my case it was version 1.9.0 .就我而言,它是1.9.0版本。 Update to 1.10.0 solved the issue.更新到1.10.0解决了这个问题。

__spreadArrays is added in 1.10.0 tsLib version: https://github.com/microsoft/tslib/blob/1.10.0/tslib.es6.js __spreadArrays1.10.0 tsLib版本中添加: https://github.com/microsoft/tslib/blob/1.10.0/tslib.es6.js

Update: Since some time passed, it is better to update version to 2.2.0 (based on last comments) or the latest .更新:由于一段时间过去了,最好将版本更新到2.2.0 (基于最后的评论)或latest的 .

I had this issue with tslib version 1.14.1我的 tslib 版本 1.14.1 有这个问题

npm update tslib --save didn't really do anything npm update tslib --save并没有真正做任何事情

npm install tslib@latest --save did the job, it updated to version 2.2.0 npm install tslib@latest --save完成了这项工作,它更新到版本 2.2.0

This error might happen if you have in your tsconfig.json file:如果您的 tsconfig.json 文件中包含以下内容,则可能会发生此错误:

{ 
  "importHelpers": true,
  "target": "es5"
}

If for any reason you can't upgrade to es6, setting importHelpers to false will make the error go away...如果出于任何原因你无法升级到 es6,将 importHelpers 设置为 false 将使错误 go 消失......

In my case the problem it was target: "es5" in compilerOptions at tsconfig.json .在我的情况下,问题是target: "es5"compilerOptions中的“ tsconfig.json ”。

So I change it to es6 to get it works without adding dependency.所以我将其更改为es6以使其在不添加依赖项的情况下工作。

Assuming your tslib is installed correctly and up to date, you might still get this error in VS Code if the editor is using a different Typescript version.假设您的 tslib 安装正确且是最新的,如果编辑器使用不同的 Typescript 版本,您可能仍会在 VS Code 中收到此错误。

(taken from zok's answer on this SO question ) (取自zok 对这个 SO question 的回答

Open Command Palette (Cmd+Shift+P on Mac. Focused file must be.ts or.tsx otherwise it won't show the option to change version) Select "TypeScript: Select TypeScript Version..." It shows VSCode's TS version and Workspace's (project) one, pick the workspace one Open Command Palette (Cmd+Shift+P on Mac. Focused file must be.ts or.tsx otherwise it won't show the option to change version) Select "TypeScript: Select TypeScript Version..." It shows VSCode's TS version and工作区的(项目)一,选择工作区一

for yarn users: yarn upgrade tslib@latest对于 yarn 用户: yarn upgrade tslib@latest

from right side down hover on curly braces beside of TypeScript and then click on version it will give you few options, generally related to project just select version and it will work fine从右侧向下 hover 在 TypeScript 旁边的花括号上,然后单击版本,它会给你几个选项,通常与项目相关,只有 select 版本,它会工作正常

you may need to upgrade to tslib@2.2.0您可能需要升级到 tslib@2.2.0

Just make sure tslint is enabled in the bottom of vscode editor options只需确保在 vscode 编辑器选项的底部启用了 tslint

Ran into this issue with a dependency and resolved it by updating its tsconfig.ts with the path to the top level lastest tslib .遇到这个带有依赖关系的问题,并通过使用顶级最新tslib的路径更新其tsconfig.ts来解决它。

"importHelpers": true,
"paths": {
  "tslib": [ "../tslib/tslib.d.ts" ]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM