Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-exporting default from a dependency in JavaScript produces an invalid declaration file #55082

Closed
esdmr opened this issue Jul 19, 2023 · 2 comments · Fixed by #56340
Closed
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue Has Repro This issue has compiler-backed repros: https://aka.ms/ts-repros

Comments

@esdmr
Copy link

esdmr commented Jul 19, 2023

Bug Report

🔎 Search Terms

Re-export, default, declaration, module.exports, Identifier expected ts(1003), js, JavaScript.

🕗 Version & Regression Information

  • This affects Bundler since version 5.0.
  • This affects Node16/NodeNext since version 4.7.
  • This affects System since version 3.7.

⏯ Playground Link

Workbench Repro

Switch to the “Debug” tab and look at /index.d.ts. @showEmittedFile did not work.

💻 Code

// @checkJs
// @module: Node16
// @moduleResolution: Node16
// @declaration
// @emitDeclarationOnly

// @filename: index.js
export {default as mod} from 'a';
// @filename: package.json
{"private": true, "type": "module"}
// @filename: node_modules/a/package.json
{"name": "a", "version": "0.0.0"}
// @filename: node_modules/a/index.js
exports.a = 123;
// @filename: node_modules/a/index.d.ts
export const a = 123;

Other cases like this:

  • Indirect re-export
    (Only the declaration file that re-exports a dependency will be invalid.)
  • index.js: export {default} from 'a';
    (Declaration file will have as default)
  • node_modules/a/index.d.ts may be provided separately (e.g., by a @types/ package).
  • My comment below

🙁 Actual behavior

Generated index.d.ts is invalid because, instead of re-exporting default as mod, it re-exports "/path/to/node_modules/a/index" as mod. Firstly, there is no export on a/index.js with that identifier. Secondly, TypeScript does not even support “arbitrary module namespace identifier names” (#40594), so the resulting declaration file cannot be subsequently parsed anyway. (Errs Identifier expected. ts(1003).)

🙂 Expected behavior

Generated index.d.ts should simply re-export default as mod, just like the JavaScript file. If node_modules/a is ESM, it should err that there is no export named default.

@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label Jul 20, 2023
@typescript-bot typescript-bot added the Has Repro This issue has compiler-backed repros: https://aka.ms/ts-repros label Jul 20, 2023
@typescript-bot
Copy link
Collaborator

typescript-bot commented Jul 21, 2023

👋 Hi, I'm the Repro bot. I can help narrow down and track compiler bugs across releases! This comment reflects the current state of the repro in the issue body running against the nightly TypeScript.


Issue body code block by @esdmr

👍 Compiled

Historical Information
Version Reproduction Outputs
4.7.2, 4.8.2, 4.9.3, 5.0.2, 5.1.3

👍 Compiled

@esdmr
Copy link
Author

esdmr commented Jul 21, 2023

Update on which @module, @moduleResolution, and "type" cause the bug. Columns represent @moduleResolution and rows represent @module. “CJS from ESM” means that node_modules/a/package.json is “CJS” and package.json is “ESM”.

Symbols:

  • Empty: Compiler error,
  • Dash: Invalid cases (incompatible options or importing ESM from CJS),
  • ‘B’: Bug.
version 5.1.6
CJS from CJS node node16 nodenext bundler
AMD -
CommonJS -
ES2015 B
ES2020 B
ES2022 B
ESNext B
Node16 B B B -
NodeNext B B B -
System B B B -
UMD -
CJS from ESM node node16 nodenext bundler
AMD B B -
CommonJS B B -
ES2015 B B B
ES2020 B B B
ES2022 B B B
ESNext B B B
Node16 B B B -
NodeNext B B B -
System B B B -
UMD B B -
ESM from CJS node node16 nodenext bundler
AMD - - -
CommonJS - - -
ES2015 - - B
ES2020 - - B
ES2022 - - B
ESNext - - B
Node16 B - - -
NodeNext B - - -
System B - - -
UMD - - -
ESM from ESM node node16 nodenext bundler
AMD -
CommonJS -
ES2015 B
ES2020 B
ES2022 B
ESNext B
Node16 B -
NodeNext B -
System B -
UMD -

By comparison, here is the tables for version 4.6.

version 4.6.4
CJS from CJS node node16 nodenext bundler
AMD - - -
CommonJS - - -
ES2015 - - -
ES2020 - - -
ES2022 - - -
ESNext - - -
Node16 - - - -
NodeNext - - - -
System B - - -
UMD - - -
CJS from ESM node node16 nodenext bundler
AMD - - -
CommonJS - - -
ES2015 - - -
ES2020 - - -
ES2022 - - -
ESNext - - -
Node16 - - - -
NodeNext - - - -
System B - - -
UMD - - -
ESM from CJS node node16 nodenext bundler
AMD - - -
CommonJS - - -
ES2015 - - -
ES2020 - - -
ES2022 - - -
ESNext - - -
Node16 - - - -
NodeNext - - - -
System B - - -
UMD - - -
ESM from ESM node node16 nodenext bundler
AMD - - -
CommonJS - - -
ES2015 - - -
ES2020 - - -
ES2022 - - -
ESNext - - -
Node16 - - - -
NodeNext - - - -
System B - - -
UMD - - -

(script to run these test cases)

Edit: @allowSyntheticDefaultImports also seems to be related.

version 5.1.6, "esModuleInterop": true or "allowSyntheticDefaultImports": true
CJS from CJS node node16 nodenext bundler
AMD B B B -
CommonJS B B B -
ES2015 B B B B
ES2020 B B B B
ES2022 B B B B
ESNext B B B B
Node16 B B B -
NodeNext B B B -
System B B B -
UMD B B B -
CJS from ESM node node16 nodenext bundler
AMD B B B -
CommonJS B B B -
ES2015 B B B B
ES2020 B B B B
ES2022 B B B B
ESNext B B B B
Node16 B B B -
NodeNext B B B -
System B B B -
UMD B B B -
ESM from CJS node node16 nodenext bundler
AMD B - - -
CommonJS B - - -
ES2015 B - - B
ES2020 B - - B
ES2022 B - - B
ESNext B - - B
Node16 B - - -
NodeNext B - - -
System B - - -
UMD B - - -
ESM from ESM node node16 nodenext bundler
AMD B -
CommonJS B -
ES2015 B B
ES2020 B B
ES2022 B B
ESNext B B
Node16 B -
NodeNext B -
System B -
UMD B -
version 5.1.6, "allowSyntheticDefaultImports": false
CJS from CJS node node16 nodenext bundler
AMD -
CommonJS -
ES2015
ES2020
ES2022
ESNext
Node16 -
NodeNext -
System -
UMD -
CJS from ESM node node16 nodenext bundler
AMD B B -
CommonJS B B -
ES2015 B B
ES2020 B B
ES2022 B B
ESNext B B
Node16 B B -
NodeNext B B -
System B B -
UMD B B -
ESM from CJS node node16 nodenext bundler
AMD - - -
CommonJS - - -
ES2015 - -
ES2020 - -
ES2022 - -
ESNext - -
Node16 - - -
NodeNext - - -
System - - -
UMD - - -
ESM from ESM node node16 nodenext bundler
AMD -
CommonJS -
ES2015
ES2020
ES2022
ESNext
Node16 -
NodeNext -
System -
UMD -
version 5.1.6, "esModuleInterop": false and "allowSyntheticDefaultImports" unset
CJS from CJS node node16 nodenext bundler
AMD -
CommonJS -
ES2015 B
ES2020 B
ES2022 B
ESNext B
Node16 -
NodeNext -
System B B B -
UMD -
CJS from ESM node node16 nodenext bundler
AMD B B -
CommonJS B B -
ES2015 B B B
ES2020 B B B
ES2022 B B B
ESNext B B B
Node16 B B -
NodeNext B B -
System B B B -
UMD B B -
ESM from CJS node node16 nodenext bundler
AMD - - -
CommonJS - - -
ES2015 - - B
ES2020 - - B
ES2022 - - B
ESNext - - B
Node16 - - -
NodeNext - - -
System B - - -
UMD - - -
ESM from ESM node node16 nodenext bundler
AMD -
CommonJS -
ES2015 B
ES2020 B
ES2022 B
ESNext B
Node16 -
NodeNext -
System B -
UMD -

@andrewbranch andrewbranch added the Bug A bug in TypeScript label Jul 24, 2023
@andrewbranch andrewbranch added this to the TypeScript 5.2.1 milestone Jul 24, 2023
@andrewbranch andrewbranch removed the Needs Investigation This issue needs a team member to investigate its status. label Nov 7, 2023
@typescript-bot typescript-bot added Fix Available A PR has been opened for this issue labels Nov 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue Has Repro This issue has compiler-backed repros: https://aka.ms/ts-repros
Projects
None yet
4 participants