Skip to content

Readonly and Required of a class are the class itself - #118

Merged
ASDAlexander77 merged 1 commit into
mainfrom
readonly-required-keep-class
Nov 25, 2026
Merged

ASDAlexander77 merged 1 commit into
mainfrom
readonly-required-keep-class

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

Readonly<T> and Required<T> mapped a class into an object of its fields, without its methods. Readonly<C> of a class with methods came out as an empty type, so r.bump() failed with Can't resolve property 'bump' of type [].

Both now keep a class as it is:

type Required<T> = T extends abstract new (...args: any) => any ? T : { [P in keyof T]-?: T[P]; }
type Readonly<T> = T extends abstract new (...args: any) => any ? T : { readonly [P in keyof T]: T[P]; }

In tslang a class's own type extends abstract new (...args: any) => any (MLIRTypeHelper::extendsType matches a class against a constructor type), while object types, interfaces, arrays and primitives don't. Standard TypeScript would answer no for a class instance, so this relies on tslang's behaviour. extends class does not parse.

Partial and Pick are unchanged: if they kept the class they would no longer accept an object literal (constructor(init: Partial<Point>) called with { x: 1 }).

Not in this change: readonly is not enforced by the compiler. A write through Readonly<{ a: number }> compiles and runs, with or without this change.

Testing

  • New tests/readonly_required_class.ts: a class with a method, an optional field, a derived class with a constructor argument, a generic class, plus an object type and an interface. Passes under --emit=jit (release, gc) against a staged copy of the library with this change; fails to compile with the current one.
  • Not run locally: the full tests.ps1, --emit=exe, debug, and the rc/own/none models.

🤖 Generated with Claude Code

Mapped over a class, they made an object of its fields, without its methods
(Readonly<C> 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 <noreply@anthropic.com>
@ASDAlexander77
ASDAlexander77 merged commit 455116f into main Nov 25, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant