question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

esModuleInterop should work even when compiling to esnext modules

See original GitHub issue

TypeScript Version: 2.7.2 Search Terms: esModuleInterop, esnext, modules, import, export, default

Code

With this type definition:

declare function fn(): void;
declare module "external" {
  export = fn;
}

Running with:

tsc --esModuleInterop --module esnext

Produces these errors when importing:

import fn1 from 'external';       // error TS1192: Module '"external"' has no default export.
import fn2 = require('external'); // error TS1202: Import assignment cannot be used when targeting ECMAScript modules.

But, if you use commonjs modules:

tsc --esModuleInterop --module commonjs

It works as expected (because of --esModuleInterop)

import fn1 from 'external';       // works
import fn2 = require('external'); // works

Expected behavior:

It is understandable that the type checker doesn’t want to pretend the import is interop’d when it’s not compiling in the helpers.

But if you’ve specified --esModuleInterop and --module esnext the assumption from the type checker should be that an external system is applying the interop. Otherwise why would you specify --esModuleInterop?

Playground Link: https://github.com/jamiebuilds/ts-bug

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:10
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

6reactions
aryzingcommented, Apr 8, 2018

I guess you could make the argument that esModuleInterop should imply allowSyntheticDefaultImports

@DanielRosenwasser Doesn’t it already? From --esModuleInterop docs

Emit __importStar and __importDefault helpers for runtime babel ecosystem compatibility and enable --allowSyntheticDefaultImportsfor typesystem compatibility.

1reaction
jamiebuildscommented, Apr 9, 2018

allowSyntheticDefaultImports does not fix the issue. It also has broken behaviour when you re-export something that was imported with it: You get an object with the shape { default: T } instead of T

Read more comments on GitHub >

github_iconTop Results From Across the Web

Understanding esModuleInterop in tsconfig file - Stack Overflow
It works and it's perfectly valid with es6 modules spec, because moment is not namespace from star import, it's default import.
Read more >
TSConfig Option: esModuleInterop - TypeScript
the ES6 modules spec states that a namespace import ( import * as x ) can only be an object, by having TypeScript...
Read more >
Module Compiler Option in TypeScript - tsmean
So all in all this can be summarized as: Using TypeScript, node. js and the TypeScript compiler option "module": "esnext" together is next...
Read more >
Avoid these issues when using new ECMAScript modules in ...
Es6 modules now have full support in Node.js 12 and above so it's time to ... modules should turn on esModuleInterop in their...
Read more >
Configuration | Manual - Deno
Using tsconfig.json as a file name will still work, but we recommend to use ... and even then it only considers certain compiler...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found