fix: allow destructured require under module preserve + verbatimModuleSyntax - #4847
Open
Sankalp Thakur (sankalpsthakur) wants to merge 1 commit into
Conversation
…eSyntax
checkAliasSymbol already treated bare `const x = require(...)` as valid
CommonJS under `--module preserve` by excluding VariableDeclaration from the
TS1293 ESM-syntax check, but object-destructuring aliases
(`const { x } = require(...)`) are BindingElements and still errored.
Exclude BindingElement the same way so CJS require destructuring is allowed
while real ESM import/export syntax in .cjs/.cts files remains an error.
Fixes microsoft/TypeScript#63696
Refiles microsoft/TypeScript#63732 (closed: JS repo is maintenance-only)
Copilot started reviewing on behalf of
Sankalp Thakur (sankalpsthakur)
August 7, 2026 20:37
View session
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes false-positive TS1293 diagnostics for destructured CommonJS require calls under module-preserve mode.
Changes:
- Exempts
BindingElementaliases from the ESM syntax diagnostic. - Adds compiler coverage and expected error/type/symbol baselines.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
internal/checker/checker.go |
Allows destructured CommonJS aliases. |
testdata/tests/cases/compiler/modulePreserveDestructuredRequire.ts |
Adds regression coverage. |
testdata/baselines/reference/compiler/modulePreserveDestructuredRequire.errors.txt |
Verifies only genuine ESM syntax errors. |
testdata/baselines/reference/compiler/modulePreserveDestructuredRequire.types |
Records inferred types. |
testdata/baselines/reference/compiler/modulePreserveDestructuredRequire.symbols |
Records resolved symbols. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes microsoft/TypeScript#63696.
Refile of microsoft/TypeScript#63732 (closed by Jake Bailey (@jakebailey) — JS TypeScript repo is closed for development; fix belongs here per CONTRIBUTING.md and pinned #62963).
Under
--module preservewith--verbatimModuleSyntax, CommonJS files (e.g..cjs) correctly allow barerequireassignments:but incorrectly reported TS1293 on destructured
require:Cause
In
checkAliasSymbol, the preserve-mode CommonJS check excludesImportEqualsDeclarationandVariableDeclaration(the latter coversconst x = require(...)), but object-destructuring aliases are modeled asBindingElementnodes (const { x } = require(...)) and fell through to the ESM-syntax error.Fix
Also exclude
KindBindingElementfrom that check so CJS require destructuring is treated like whole-modulerequire. Real ESM import/export syntax in unambiguously CommonJS files still errors.Test
New local compiler test
modulePreserveDestructuredRequire.ts:const { x } = require("./mod")— no errorconst { y: renamed } = require("./mod")— no errorconst whole = require("./mod")— no errorimport { x as x2 } from "./mod"in.cjs— still TS1293AI disclosure
This change was developed with assistance from an LLM (coding agent). I reviewed the root cause, the patch, and the test baselines before submitting. This is a targeted refile of a specific closed PR (not bulk/queue-driven issue hunting).