TypeScript: TypeScript error “TS2354: This syntax requires an imported helper but module 'tslib' cannot be found”
TypeScript Version: 3.8.3
Search Terms: TS2354
Code
package.json
{
"name": "test",
"version": "1.0.0",
"description": "",
"main": "index.ts",
"scripts": {
"test": "tsc --noEmit --project ./tsconfig.json"
},
"author": "",
"license": "ISC",
"devDependencies": {
"@types/argparse": "1.0.38",
"argparse": "1.0.10",
"tslib": "1.11.1",
"typescript": "3.8.3"
}
}
tsconfig.json
{
"compilerOptions": {
"checkJs": true,
"allowJs": true,
"moduleResolution": "node",
"target": "es2018",
"module": "commonjs",
"importHelpers": true,
"lib": [
"es2018"
]
},
"include": [
"*.js"
],
"exclude": [
"node_modules"
]
}
index.js
'use strict';
const {ArgumentParser} = require('argparse');
Expected behavior:
Actual behavior:
I have reduced a problem with TypeScript to a simple example. When trying to run tsc, I get the following error message but tslib should be available.
$ tsc --noEmit --project ./tsconfig.json
index.js:3:8 - error TS2354: This syntax requires an imported helper but module 'tslib' cannot be found.
3 const {ArgumentParser} = require('argparse');
~~~~~~~~~~~~~~
Found 1 error.
Playground Link: https://codesandbox.io/s/quizzical-mclean-n9vvi?fontsize=14&hidenavigation=1&theme=dark
Related Issues:
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 16 (2 by maintainers)
Commits related to this issue
- Fix for tslib error in vscode, add tslib in tsconfig see https://github.com/microsoft/TypeScript/issues/37991#issuecomment-629944853 — committed to lostboy139/DatingApp by lostboy139 2 years ago
- Fix for https://github.com/microsoft/TypeScript/issues/37991 — committed to UncleBench/TourOfHeroesAspNetCore by UncleBench 5 months ago
I was having this exact same issue and I figured it out!
Add to paths in tsconfig.json. And in your case, you will need to add “baseUrl” too.
Thank you.
Seeing this error when opening Yarn PnP project in VIM.
yarn add -D tslib@latest
does not solves this.npm i tslib@latest
does solves this, but it brings innode_modules
directory to the project which is not what i want. In VS Code everything works fine because it handles PnP.https://stackoverflow.com/questions/58329178/the-syntax-requires-an-imported-helper-named-spreadarrays
This error might happen if you have in your tsconfig.json file :
If for any reason you can’t upgrade to es6, setting importHelpers to false will make the error go away …
I got the same __spreadArray error in my project. I npm i tslib to ^2.2.0 and the error is gone.
No matter what I do in the internet I still get the error.
This syntax requires an imported helper named ‘__spreadArray’ which does not exist in ‘tslib’. Consider upgrading your version of ‘tslib’.ts(2343)
Has anyone found a solution for this issue (with Yarn v2)?
For those using pnp, you need to resolve the pnp path within tsconfig in your “compilerOptions”:
I have a fix up for the commonjs
require
scenario, but I’m not disabling the checks when--noEmit
is enabled since there is value in reporting errors that would occur when you emit, and you can pass--noEmit --importHelpers false
on the command line if you don’t want the errors reported.The reason for the issue in the OP is twofold. First, we were detecting the file is a module due to the
require
call (which is a signal to TypeScript the file is a module), and we only check for a validtslib
for modules. Second, we had a very weak check as to whether the__rest
helper should be checked due to the destructuring assignment. #43166 addresses this weak check, which should solve the root cause of this issue.Using yarn pnp this can be overcome by executing tsc with yarn pnpify.
I’m also seeing this error in JavaScript files when running with
--checkJs
and using object destructuring, even when targeting ES2020.