Skip to content

Preserve nested CommonJS bindings in declaration emit - #65720

Open
Ankit Das (ecryptoguru) wants to merge 1 commit into
microsoft:mainfrom
ecryptoguru:fix/nested-cjs-declaration
Open

Ankit Das (ecryptoguru) wants to merge 1 commit into
microsoft:mainfrom
ecryptoguru:fix/nested-cjs-declaration

Conversation

@ecryptoguru

@ecryptoguru Ankit Das (ecryptoguru) commented Nov 28, 2026 •

Copy link
Copy Markdown

Fixes #65628.

In CommonJS JavaScript, destructuring require() into a nested object or array could emit an export with no declaration. Only direct bindings can become named imports. The existing fallback now emits typed declarations for the other bindings and keeps the direct imports.

The regression case covers nested objects and arrays alongside a direct binding, plus a declaration with only a nested binding. With a compiler built from main, a separate consumer of the emitted declarations reported TS2304 for the dropped names. The consumer type checks after this change. Focused declaration baselines, hereby tidy, and hereby check:format passed.

The full hereby validate --all run passed on macOS with TMPDIR set to its resolved path under /private/tmp. This included the compiler, extension, native and WebAssembly API, code generation, smoke, lint, and formatting checks. hereby tidy left the branch clean.

Prepared with Codex assistance.

Copilot AI balanced review requested due to automatic review settings November 28, 2026 18:18
@typescript-automation typescript-automation Bot added the For Backlog Bug PRs that fix a backlog bug label Nov 28, 2026
@github-project-automation github-project-automation Bot moved this to Not started in PR Backlog Nov 28, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are reported for the change preserving nested CommonJS bindings in declaration output.

No open issues 🎉

What changed in this PR

Fixes #65628 by preserving nested CommonJS require() bindings in TypeScript declaration output, preventing exports from referencing undeclared names.

Changes:

  • Reuses the importability check to route non-importable bindings through typed declaration emission while retaining direct imports.
  • Adds regression coverage and baselines for nested objects, nested arrays, mixed direct bindings, and nested-only declarations.
File Description
tsc/​testdata/​tests/​cases/​compiler/​declarationEmitCommonjsRequireNestedBinding.ts Adds nested-binding regression cases.
tsc/​testdata/​baselines/​reference/​compiler/​declarationEmitCommonjsRequireNestedBinding.types Records inferred binding types.
tsc/​testdata/​baselines/​reference/​compiler/​declarationEmitCommonjsRequireNestedBinding.symbols Records binding and export symbols.
tsc/​testdata/​baselines/​reference/​compiler/​declarationEmitCommonjsRequireNestedBinding.js Records preserved imports and typed declarations.
tsc/​internal/​transformers/​declarations/​transform.go Routes non-importable bindings through the existing declaration fallback.

Review effort: Balanced

@ecryptoguru

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

For Backlog Bug PRs that fix a backlog bug

Projects

Status: Not started

Development

Successfully merging this pull request may close these issues.

Declaration emit drops nested destructured require bindings in JS (const { b: { c } } = require("./m"))

2 participants