the syntax requires an imported helper named __spreadArrays
Asked Answered
O

10

82

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.

Odom answered 10/10, 2019 at 18:39 Comment(1)
It looks like VS Code is set to target es5 instead of es6? Haven't seen that before, maybe check out this thread: #29953793Anarch
O
31

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.

Odom answered 19/12, 2019 at 21:0 Comment(2)
What does update tslib dependency mean? Add it to package.json, to development dependencies or to devDependencies?Hypogeous
npm install tslib --save did the job for meSouthernly
F
93

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.

Fibroid answered 30/6, 2021 at 5:14 Comment(1)
I really think this is a more relatable answer. tsLib is rarely directly installed in projects.Alfredoalfresco
S
47

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

Soursop answered 7/5, 2021 at 13:19 Comment(2)
Had the same issue with 1.14.1. I don't know if this is an issue with that specific version (since _spreadArrays is meant to be there since 1.10.0), but installing 2.2.0 seems to be the fixWarehouseman
this works for me.. i had to restart my vscode..Fawkes
O
31

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.

Odom answered 19/12, 2019 at 21:0 Comment(2)
What does update tslib dependency mean? Add it to package.json, to development dependencies or to devDependencies?Hypogeous
npm install tslib --save did the job for meSouthernly
W
30

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

Wigging answered 28/6, 2021 at 2:8 Comment(1)
This is the only valid answer, since the question refers to a problem with vs code, supposing that the build would succeedHerculean
N
8

This error might happen if you have in your tsconfig.json file :

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

If for any reason you can't upgrade to es6, setting importHelpers to false will make the error go away ...

Newspaper answered 29/9, 2021 at 14:18 Comment(1)
One reason in my case is that the third party API I use instantiates objects without the word "new".Himmler
L
2

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

Lafreniere answered 13/4, 2022 at 4:24 Comment(1)
💡 Brilliant. The simplest solution of all. Thanks!Hensley
F
1

for yarn users: yarn upgrade tslib@latest

Faunia answered 20/9, 2021 at 12:44 Comment(0)
L
0

you may need to upgrade to [email protected]

Latoyialatreece answered 7/5, 2021 at 9:33 Comment(0)
T
0

Just make sure tslint is enabled in the bottom of vscode editor options

Tillietillinger answered 19/6, 2021 at 19:3 Comment(0)
B
0

Ran into this issue with a dependency and resolved it by updating its tsconfig.ts with the path to the top level lastest tslib.

"importHelpers": true,
"paths": {
  "tslib": [ "../tslib/tslib.d.ts" ]
}
Blabber answered 11/5, 2022 at 11:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.