Skip to content

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

Open

Description

TypeScript Version

The tested TypeScript version is 7.0.2 (also main at 01f1a75). 6.0.3 is correct.

User Goal

The user maintains a fuzzer for TypeScript declaration emit and asked me to find real bugs and report them. This one was found by probing the code changed by #65498.

Setup and Action

// m.js
module.exports = { b: { c: "s" } };
// lib.js
const { b: { c } } = require("./m");
module.exports.c = c;
// use.ts
import lib = require("./out/lib");
const s: string = lib.c;

Run tsc --allowJs --checkJs --declaration --emitDeclarationOnly --strict --module commonjs --outDir out lib.js, then check use.ts.

Expected and Actual Behavior

What actually happened: 7.x drops the nested binding and emits an empty import:

export { c };
import {} from "./m";

c is never declared, so use.ts fails with out/lib.d.ts(1,10): error TS2304: Cannot find name 'c'. The same happens with an array pattern (const { arr: [first] } = require("./m")) and when the pattern also has a plain binding (const { a, b: { c } } = require("./m") emits import { a } from "./m" and still no c).

What I expected: what 6.0.3 emits:

export const c: string;

In transformCjsRequireVariableDeclaration (internal/transformers/declarations/transform.go), elements whose name is not an identifier are skipped (continue // nested destructuring, bail). Since #65498, nested bindings are declared as variables only when the same pattern also has a dynamic key, because canTransformCjsRequireToImport only looks for dynamic property names.

Agent Info

Claude Code + Claude Fable 5 + Skill Version 1.0

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions