microsoft / microsoft/TypeScript
Suggestion: disallow synthetic imports for ES modules
Nobody has claimed this yet.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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