microsoft / microsoft/TypeScript

Suggestion: disallow synthetic imports for ES modules

Open
#33,954 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

TypeScript Version: 3.6.3

Search Terms: allowSyntheticDefaultImports esModuleInterop babel synthetic es modules default exports imports commonjs

Code

allowSyntheticDefaultImports is great when you're importing a CommonJS module, where/when Babel/TS will make sure there is a default export/import via their "interop" layers (e.g. esModuleInterop).

// foo.js
exports.foo = () => {}
// foo.d.ts
declare const _default: { foo: () => {} }
export = _default
// main.ts
import Foo from './foo';
Foo.foo(); // all good!

However, it's possible to shoot yourself in the foot by trying to import a non-existent default from an ES module.

// foo.js
export const foo = () => {}
// foo.d.ts
export declare const foo: () => {}
// main.ts
import Foo from './foo'; // no error, but should be `* as Foo`!
// runtime error!!
// TypeError: Cannot read property 'foo' of undefined
Foo.foo();

Ideally it would not be possible to use synthetic default imports with an ES module, so runtime errors such as the one above would not happen.

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

Start with the TypeScript 3.6.3 repro showing allowSyntheticDefaultImports accepting a default import from an ES module, and compare it with the CommonJS example. Done means the invalid ES-module default import is rejected while the intended CommonJS synthetic-default case remains supported.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.