Skip to content

fix: allow destructured require under module preserve + verbatimModuleSyntax - #4847

Open
Sankalp Thakur (sankalpsthakur) wants to merge 1 commit into
microsoft:mainfrom
sankalpsthakur:fix/63696-verbatim-destructured-require
Open

fix: allow destructured require under module preserve + verbatimModuleSyntax#4847
Sankalp Thakur (sankalpsthakur) wants to merge 1 commit into
microsoft:mainfrom
sankalpsthakur:fix/63696-verbatim-destructured-require

Conversation

@sankalpsthakur

Copy link
Copy Markdown

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 preserve with --verbatimModuleSyntax, CommonJS files (e.g. .cjs) correctly allow bare require assignments:

const whole = require("./mod"); // ok

but incorrectly reported TS1293 on destructured require:

const { x } = require("./mod"); // was false-positive TS1293

Cause

In checkAliasSymbol, the preserve-mode CommonJS check excludes ImportEqualsDeclaration and VariableDeclaration (the latter covers const x = require(...)), but object-destructuring aliases are modeled as BindingElement nodes (const { x } = require(...)) and fell through to the ESM-syntax error.

Fix

Also exclude KindBindingElement from that check so CJS require destructuring is treated like whole-module require. Real ESM import/export syntax in unambiguously CommonJS files still errors.

Test

New local compiler test modulePreserveDestructuredRequire.ts:

  • const { x } = require("./mod") — no error
  • const { y: renamed } = require("./mod") — no error
  • const whole = require("./mod") — no error
  • import { x as x2 } from "./mod" in .cjs — still TS1293
go test ./internal/testrunner/ -count=1 -run 'TestLocal/modulePreserveDestructuredRequire'

AI 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).

…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 AI balanced review requested due to automatic review settings August 7, 2026 20:36

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.

Pull request overview

Fixes false-positive TS1293 diagnostics for destructured CommonJS require calls under module-preserve mode.

Changes:

  • Exempts BindingElement aliases 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.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

False positive on destructured require is verbatimModuleSyntax and module is preserve

2 participants