Files
scriptc/tests/diagnostics/constructor-returns-object.ts
T
Malte UblandClaude Opus 5.5 11c97074e2 Answer the object a constructor returns from new
A class constructor's `return expr` was evaluated and dropped, so
`new C()` answered the constructed instance even where Node answers the
returned object (`if (C.instance) return C.instance;` built a second
instance).

A return that yields an instance of the class itself stores it in a
per-class result slot, read and cleared by one helper at every
construction site (direct new, class values, the checked dynamic
construction dispatchers, record-to-class coercion). A return of any
other object refuses with SC1090 (compile time in TypeScript, a
deferred fence in JavaScript); a JavaScript value typed only at run time
is tested when it returns. super() into a class that can return an
existing instance refuses, since Node would rebind the subclass's this.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-18 14:42:18 -07:00

11 lines
295 B
TypeScript

// Node's `new Point()` answers the object a constructor returns. A plain
// object has no representation as a Point instance, so the return refuses
// instead of answering the constructed instance.
class Point {
x = 1;
constructor() {
return { x: 2 };
}
}
console.log(new Point().x);