Typescript: importHelpers option causes compiler errors

Created on 5 Mar 2017  路  18Comments  路  Source: microsoft/TypeScript

TypeScript Version: 2.2.1

Environment:

  • Windows 10
  • Node 7.6.0
  • NPM 4.1.2
  • TypeScript 2.2.1
  • tslib 1.6.0

The recently added compiler option "importHelpers" causes compiler errors. There are several other closed issues where this problem was described: #12724, #12775, #12. This should be fixed, in TS 2.1.5 or 2.2.x but it still occurs.
It's a simple compiler error in the CLI. In vscode (latest version 1.10.1) the "extends" keyword causes the error.

I created a small skeleton which demonstrates this bug: https://github.com/code-chris/tslib-compile-error. This was tested on a windows machine with the specs above.

Expected behavior:
There's no compilation error when "importHelpers" option is enabled.

Actual behavior:
The compilation result is runnable and correct, but there's a compilation error like this:

src/main.ts(5,19): error TS2354: This syntax requires an imported helper but module 'tslib' cannot be found.
Bug

Most helpful comment

To summarize: The problem isn't the --importHelpers option, it's that different people have had module resolution fail for various reasons. There's no real bug here, but we could do more to help people understand why they're getting resolution failures. I've created #15845 to track that.

All 18 comments

I still see this error when use webpack with awesome-typescript-loader in case tslib resolved via webpack

It can't find that module because your module resolution is set to the default, which doesn't look in node_modules. You can fix this by adding "moduleResolution": "node" to your tsconfig.json. We could probably improve the error message here.

Espessialy for @andy-ms
There is no this module in node_modules or anywhere which can be resolved via moduleResolution.
Path to tslib module written in paths setting of tsconfig.json and at bundle resolved via webpack. All works but dianostics message still appear

@Delagen Could you provide a repro for your case? Also try using --traceResolution so see where it looks.

@andy-ms The moduleResoution fixed the bug in my skeleton from my first post. But yes, the message isn't clear.
@Delagen What do you mean with "no this module"?

@andy-ms https://github.com/Delagen/typescript-issue14468
npm run test -- success

Found 2 issues which not related to webpack or any other. So it's base behavior of TS.
npm run test.fail
When extend another tsconfig.json and have own paths its simply override base config paths section (may be extend?)
npm run test.fail2
When extend another tsconfig.json paths of base config resolved using root baseUrl and not using baseUrl from base config (relative to its path)

@code-chris I mean tslib module installed outside generic moduleResolution from base module.

@Delagen I created #14527 for your issue. Assuming it happens for all libraries and not just for tslib.

Work as intended ) - expected answer

Status on this? Workaround gives me 500+ compiler errors for other modules it cant find, so that's not really a solution.
Adding tslib.d.ts to where my other .d.ts files are doesn't help either.

@davhdavh Try using --traceResolution in both scenarios. You'll need to get the module resolution to match up with where you're actually storing your modules. If they're in node_modules you should be able to use "moduleResolution": "node". In some cases you may need to use path mapping. More info here.

It is given in the command line, and yet it still cannot find it... Only tslib has this problem. Other modules are not resolved either, according to traceResolution, but it doesn't give an error due to those.
I moved tslib.d.ts into the /App folder and no problem.

C:\Program Files (x86)\Microsoft SDKs\TypeScript\2.1\tsc.exe  --noImplicitAny --module AMD --target ES5 --importHelpers --traceResolution --inlineSourceMap --forceConsistentCasingInFileNames --noEmitOnError --locale en-US --listEmittedFiles "<base>\Scripts\typings\tslib\tslib.d.ts" "<base>\App\main.ts" ...
1>  Unknown output: ======== Resolving module 'tslib' from '<base>/App/Admin/viewmodels/admin.ts'. ========
1>  Unknown output: Module resolution kind is not specified, using 'Classic'.
1>  Unknown output: File '<base>/App/Admin/viewmodels/tslib.ts' does not exist.
1>  Unknown output: File '<base>/App/Admin/viewmodels/tslib.tsx' does not exist.
1>  Unknown output: File '<base>/App/Admin/viewmodels/tslib.d.ts' does not exist.
1>  Unknown output: File '<base>/App/Admin/tslib.ts' does not exist.
1>  Unknown output: File '<base>/App/Admin/tslib.tsx' does not exist.
1>  Unknown output: File '<base>/App/Admin/tslib.d.ts' does not exist.
1>  Unknown output: File '<base>/App/tslib.ts' does not exist.
1>  Unknown output: File '<base>/App/tslib.tsx' does not exist.
1>  Unknown output: File '<base>/App/tslib.d.ts' does not exist.
1>  Unknown output: File '<base>/tslib.ts' does not exist.
1>  Unknown output: File '<base>/tslib.tsx' does not exist.
1>  Unknown output: File '<base>/tslib.d.ts' does not exist.
1>  Unknown output: File '<base>/../tslib.ts' does not exist.
1>  Unknown output: File '<base>/../tslib.tsx' does not exist.
1>  Unknown output: File '<base>/../tslib.d.ts' does not exist.
1>  Unknown output: File 'C:/src/cghg/tslib.ts' does not exist.
1>  Unknown output: File 'C:/src/cghg/tslib.tsx' does not exist.
1>  Unknown output: File 'C:/src/cghg/tslib.d.ts' does not exist.
1>  Unknown output: File 'C:/src/tslib.ts' does not exist.
1>  Unknown output: File 'C:/src/tslib.tsx' does not exist.
1>  Unknown output: File 'C:/src/tslib.d.ts' does not exist.
1>  Unknown output: File 'C:/tslib.ts' does not exist.
1>  Unknown output: File 'C:/tslib.tsx' does not exist.
1>  Unknown output: File 'C:/tslib.d.ts' does not exist.
1>  Unknown output: File '<base>/App/Admin/viewmodels/node_modules/@types/tslib.d.ts' does not exist.
1>  Unknown output: File '<base>/App/Admin/viewmodels/node_modules/@types/tslib/package.json' does not exist.
1>  Unknown output: File '<base>/App/Admin/viewmodels/node_modules/@types/tslib/index.d.ts' does not exist.
1>  Unknown output: File '<base>/App/Admin/node_modules/@types/tslib.d.ts' does not exist.
1>  Unknown output: File '<base>/App/Admin/node_modules/@types/tslib/package.json' does not exist.
1>  Unknown output: File '<base>/App/Admin/node_modules/@types/tslib/index.d.ts' does not exist.
1>  Unknown output: File '<base>/App/node_modules/@types/tslib.d.ts' does not exist.
1>  Unknown output: File '<base>/App/node_modules/@types/tslib/package.json' does not exist.
1>  Unknown output: File '<base>/App/node_modules/@types/tslib/index.d.ts' does not exist.
1>  Unknown output: File '<base>/node_modules/@types/tslib.d.ts' does not exist.
1>  Unknown output: File '<base>/node_modules/@types/tslib/package.json' does not exist.
1>  Unknown output: File '<base>/node_modules/@types/tslib/index.d.ts' does not exist.
1>  Unknown output: File '<base>/../node_modules/@types/tslib.d.ts' does not exist.
1>  Unknown output: File '<base>/../node_modules/@types/tslib/package.json' does not exist.
1>  Unknown output: File '<base>/../node_modules/@types/tslib/index.d.ts' does not exist.
1>  Unknown output: File 'C:/src/cghg/node_modules/@types/tslib.d.ts' does not exist.
1>  Unknown output: File 'C:/src/cghg/node_modules/@types/tslib/package.json' does not exist.
1>  Unknown output: File 'C:/src/cghg/node_modules/@types/tslib/index.d.ts' does not exist.
1>  Unknown output: File 'C:/src/node_modules/@types/tslib.d.ts' does not exist.
1>  Unknown output: File 'C:/src/node_modules/@types/tslib/package.json' does not exist.
1>  Unknown output: File 'C:/src/node_modules/@types/tslib/index.d.ts' does not exist.
1>  Unknown output: File 'C:/node_modules/@types/tslib.d.ts' does not exist.
1>  Unknown output: File 'C:/node_modules/@types/tslib/package.json' does not exist.
1>  Unknown output: File 'C:/node_modules/@types/tslib/index.d.ts' does not exist.
1>  Unknown output: File '<base>/App/Admin/viewmodels/tslib.js' does not exist.
1>  Unknown output: File '<base>/App/Admin/viewmodels/tslib.jsx' does not exist.
1>  Unknown output: File '<base>/App/Admin/tslib.js' does not exist.
1>  Unknown output: File '<base>/App/Admin/tslib.jsx' does not exist.
1>  Unknown output: File '<base>/App/tslib.js' does not exist.
1>  Unknown output: File '<base>/App/tslib.jsx' does not exist.
1>  Unknown output: File '<base>/tslib.js' does not exist.
1>  Unknown output: File '<base>/tslib.jsx' does not exist.
1>  Unknown output: File '<base>/../tslib.js' does not exist.
1>  Unknown output: File '<base>/../tslib.jsx' does not exist.
1>  Unknown output: File 'C:/src/cghg/tslib.js' does not exist.
1>  Unknown output: File 'C:/src/cghg/tslib.jsx' does not exist.
1>  Unknown output: File 'C:/src/tslib.js' does not exist.
1>  Unknown output: File 'C:/src/tslib.jsx' does not exist.
1>  Unknown output: File 'C:/tslib.js' does not exist.
1>  Unknown output: File 'C:/tslib.jsx' does not exist.
1>  Unknown output: ======== Module name 'tslib' was not resolved. ========

That shows that you're using classic resolution. That is usually bad if you are using NPM, so you might want to try "moduleResolution": "node".

@andy-ms @davhdavh see also #11434

I didn't say I was using npm, although I do use it for the build setup. But for development, it is Visual Studio and for typings, I use NuGet. (Although there is no nuget package for tslib)

@davhdavh Wherever your modules are, you may need a typeRoots, baseUrl, or path mapping to help find them. You can use --traceResolution to help debug module-not-found errors.
FYI, nuget-published typings are deprecated.

Same here.
Resolved by: "moduleResolution": "node"

As I'm using jspm with its folder structure and moduleResolution: "classic" I had to setup the right path mapping in tsconfig.json

"baseUrl": "./jspm_packages",
"paths": {
      "tslib": [
        "npm/[email protected]/tslib.d.ts"
      ],
}

Maybe this example helps someone else to sort out the issue.

To summarize: The problem isn't the --importHelpers option, it's that different people have had module resolution fail for various reasons. There's no real bug here, but we could do more to help people understand why they're getting resolution failures. I've created #15845 to track that.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dlaberge picture dlaberge  路  3Comments

DanielRosenwasser picture DanielRosenwasser  路  3Comments

Antony-Jones picture Antony-Jones  路  3Comments

siddjain picture siddjain  路  3Comments

zhuravlikjb picture zhuravlikjb  路  3Comments