esModuleInterop should work even when compiling to esnext modules
See original GitHub issueTypeScript 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:
- Created 5 years ago
- Reactions:10
- Comments:9 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@DanielRosenwasser Doesn’t it already? From
--esModuleInterop
docsEmit __importStar and __importDefault helpers for runtime babel ecosystem compatibility and enable --allowSyntheticDefaultImportsfor typesystem compatibility.
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 ofT