Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

the syntax requires an imported helper named __spreadArrays

When I'm trying to use spreading in typescript like:

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

I get warning with red marking from 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.

like image 446
Alex Vovchuk Avatar asked Oct 10 '19 18:10

Alex Vovchuk


4 Answers

In my case the problem it was target: "es5" in compilerOptions at tsconfig.json.

So I change it to es6 to get it works without adding dependency.

like image 69
yacth_Mon Avatar answered Oct 16 '22 18:10

yacth_Mon


Quick fix, short answer

npm install tslib@latest --save


Update

I got this issue again (now with tslib version 2.2.0) after updating VS Code, again updating fixed the issue.

Original

I had this issue with tslib version 1.14.1

npm update tslib --save didn't really do anything

npm install tslib@latest --save did the job, it updated to version 2.2.0

like image 25
Jacob-Jan Mosselman Avatar answered Oct 16 '22 18:10

Jacob-Jan Mosselman


For VS Code the notification is based on the tsLib exports. Actually nothing bad happens without doing anything, it is just lack of typing.

Solution: Update tsLib dependency to get rid of the highlighting. In my case it was version 1.9.0. Update to 1.10.0 solved the issue.

__spreadArrays is added in 1.10.0 tsLib version: 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.

like image 30
Alex Vovchuk Avatar answered Oct 16 '22 18:10

Alex Vovchuk


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.

(taken from zok's answer on this 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

like image 23
everythinginplace Avatar answered Oct 16 '22 19:10

everythinginplace