You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Declaration emit drops nested destructured require bindings in JS (const { b: { c } } = require("./m")) #65628
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.jsmodule.exports={b: {c: "s"}};
// lib.jsconst{b: { c }}=require("./m");module.exports.c=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:
exportconstc: 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.
Activity