Files
scriptc/tests/corpus/6430-npm-constructor-function-class/main.ts
T
Malte UblandClaude Opus 5.5 f176de2124 Construct CommonJS constructor functions imported through a member alias
A package that exports a constructor function whole and also as a named member (`exports = module.exports = Client; exports.StatsD = Client`, the statsd-client shape) could not be constructed from TypeScript: `new StatsD(...)` was refused as "constructing values other than classes declared in the program", and a bare reference had "no lowering".

- `exports = module.exports = value` is now the module's whole export, and it rebinds `exports` too, so members attached through `exports` afterwards are exported. The fresh-table reset `exports = module.exports = {}` keeps its own handling.
- An `import { name }` or a top-level `const { name } = require(...)` of a member written once as `exports.<name> = <binding or primitive literal>` on such a module reads that value, following `module.exports = require("./x")` re-exports. The checker gives these bindings a synthesized property symbol, so the module's statements answer.
- Construction then takes the existing checked path for JavaScript functions: prototype methods, `util.inherits` between two constructor functions, and `Parent.call(this, ...)` behave as in Node.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-19 19:58:56 -07:00

20 lines
924 B
TypeScript

// Constructing a CommonJS constructor-function "class" imported by a named
// member alias (`exports = module.exports = Client; exports.StatsD =
// Client`) through a re-exporting package index: prototype methods, a child
// client linked with util.inherits, and a callback-style close.
import { StatsD } from "metrics-client";
const client = new StatsD({ prefix: "app." });
client.onSent((line: string) => console.log("event", line));
console.log(client.increment("boot"));
console.log(client.gauge("heap", 42));
console.log(typeof client.increment, typeof client.onSent);
const child = client.childClient({ prefix: "db." });
console.log(child.increment("query", 3));
console.log(child.gauge("pool", 1), child.sent.length, client.sent.length);
const bare = new StatsD();
console.log(bare.increment("x"), bare.sent.length);
client.close((error: Error | null, count: number) => {
console.log("closed", error, count);
});