Files
scriptc/tests/diagnostics/new-target-value-generic-subclass.ts
T
Chris Tate 6115135d96 Dispatch new.target over generic subclasses and own static names
A constructor's new.target dispatch skipped generic class families, so a
generic subclass instantiated only in a body lowered later answered with
the base's name, and `new.target === Base` held for its instances. A
family now has its own arm, tested by its instanceof interval, which holds
every instantiation: `new.target.name` reads the family's name and
comparisons with a program class are false. Under an instantiation, the
generic subclasses of its family are candidates too. `new.target` as a
value refuses generic subclasses, which have no common class value.

`new.target.name` reads a class's own static `name` field, which replaces
its name (a subclass has its own name); a static method `name` or a
non-string field is refused.
2026-10-22 21:18:48 -05:00

6 lines
284 B
TypeScript

// new.target as a class value where a subclass is generic: its
// instantiations have no common class value.
class A { readonly t: typeof A; constructor() { this.t = new.target; } }
class B<T> extends A { readonly v: T | undefined = undefined; }
console.log(new B<number>().t.name);