microsoft / microsoft/TypeScript

esModuleInterop should work even when compiling to esnext modules

Open
#22,851 9 comments 11 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Domain: ES Modules In Discussion Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the reported imports with tsc using --esModuleInterop and --module esnext, then compare the diagnostics with the commonjs case. Trace the compiler's handling of default imports and import assignments for export = declarations; done means the esnext case follows the requested interop behavior without regressing the commonjs behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.