From f7b316b9baa0839f50cd028a7bf9ce6d307dd6de Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Wed, 25 Nov 2026 21:49:52 +0000 Subject: [PATCH] Readonly and Required of a class are the class itself Mapped over a class, they made an object of its fields, without its methods (Readonly of a class with methods came out empty). A conditional on `abstract new (...args: any) => any`, which a class's own type extends in tslang, keeps the class; object types and interfaces are mapped as before. Partial and Pick are left as they were: kept as the class, they would no longer take an object literal. Co-Authored-By: Claude Opus 5.5 --- src/generics/lib.generics.ts | 7 +++- tests/readonly_required_class.ts | 69 ++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 tests/readonly_required_class.ts diff --git a/src/generics/lib.generics.ts b/src/generics/lib.generics.ts index c5c6bb5..dd23903 100644 --- a/src/generics/lib.generics.ts +++ b/src/generics/lib.generics.ts @@ -1,7 +1,10 @@ type Record = { [P in K]: T; }; type Partial = { [P in keyof T]?: T[P] | undefined; } -type Required = { [P in keyof T]-?: T[P]; } -type Readonly = { readonly [P in keyof T]: T[P]; } +// A class is kept as it is: mapped, it would become an object of its fields, a copy without its methods. +// Nothing is lost - a class's fields are always there, and readonly is only checked when compiling. +// (`extends abstract new` is true of a class itself in tslang, not only of its constructor.) +type Required = T extends abstract new (...args: any) => any ? T : { [P in keyof T]-?: T[P]; } +type Readonly = T extends abstract new (...args: any) => any ? T : { readonly [P in keyof T]: T[P]; } type Pick = { [P in K]: T[P]; } type Exclude = T extends U ? never : T; type Extract = T extends U ? T : never; diff --git a/tests/readonly_required_class.ts b/tests/readonly_required_class.ts new file mode 100644 index 0000000..87d512d --- /dev/null +++ b/tests/readonly_required_class.ts @@ -0,0 +1,69 @@ +// Readonly and Required of a class are the class itself: mapped, a class became an object of +// its fields, a copy without its methods (Readonly of a class with methods came out empty). Of +// an object type they are still mapped. +class Counter { + count = 1; + + bump() { + this.count++; + } +} + +class Opt { + y?: number; + + show() { + return "opt"; + } +} + +class Base { + constructor(public x: number) {} +} + +class Derived extends Base { + twice() { + return this.x * 2; + } +} + +class Box { + constructor(public value: T) {} + + get() { + return this.value; + } +} + +interface Point { + x: number; + y?: number; +} + +function main() { + // the same instance: a call through it reaches the original + const c = new Counter(); + const r: Readonly = c; + r.bump(); + assert(c.count == 2 && r.count == 2, "Readonly of a class is the class"); + + const o = new Opt(); + const q: Required = o; + assert(q.show() == "opt", "Required of a class is the class"); + + // a class whose constructor takes arguments, a derived one, a generic one + const d: Readonly = new Derived(3); + assert(d.twice() == 6, "Readonly of a derived class"); + + const b: Required> = new Box("v"); + assert(b.get() == "v", "Required of a generic class"); + + // object types are mapped as before + const p: Readonly<{ a: number }> = { a: 5 }; + assert(p.a == 5, "Readonly of an object type"); + + const pt: Required = { x: 1, y: 2 }; + assert(pt.x == 1 && pt.y == 2, "Required of an interface"); + + print("ALL DONE"); +}