Skip to content

feat: model class-body index signatures (#123)#127

Merged
nickna merged 1 commit into
mainfrom
feature/issue-123-class-index-signatures
Jun 6, 2026
Merged

feat: model class-body index signatures (#123)#127
nickna merged 1 commit into
mainfrom
feature/issue-123-class-index-signatures

Conversation

@nickna
Copy link
Copy Markdown
Owner

@nickna nickna commented Jun 6, 2026

Part of #80. Implements #123.

class A { [k: string]: T } parsed since #121, but its value type was discarded. This models the index type on the class and consults it when indexing a class instance — mirroring index signatures on interfaces/object types.

Changes

  • Parser: TryParseClassIndexSignature returns the parsed Stmt.IndexSignature (key name, key-type token, value type) instead of discarding; ClassDeclaration collects them into a new Stmt.Class.IndexSignatures slot.
  • Types: ClassMetadataCore, MutableClass, and TypeInfo.Class gain String/Number/SymbolIndexType (optional defaulted — existing construction sites unaffected). CheckClassDeclaration / CheckDeclareClass resolve the signatures inside the class type-environment scope (so value types can reference the class's own type parameters).
  • Index access: indexing a class instance (TypeInfo.Class or Instance<Class>) by string/number/symbol returns the matching index value type, including the TS number-key-uses-string-index fallback. Excess-property checking treats a class index signature like an interface/record one.

Verification

  • New ClassIndexSignatureTests (7): parsing, string/number index access, type-mismatch errors, numeric-key fallback, ambient declare class, and computed-property-name disambiguation.
  • Full xUnit suite green except the 2 known pre-existing packaging failures.
  • TS conformance baseline unchanged — zero regressions.

Scope notes

`class A { [k: string]: T }` was parsed (since #121) but its value type was
discarded. The index type is now modelled on the class and consulted when
indexing a class instance.

- Parser: TryParseClassIndexSignature returns the parsed Stmt.IndexSignature
  (key name, key-type token, value type) instead of discarding; ClassDeclaration
  collects them into the new Stmt.Class.IndexSignatures slot.
- Types: ClassMetadataCore + MutableClass + TypeInfo.Class gain
  String/Number/SymbolIndexType (optional defaulted; existing construction sites
  unaffected). CheckClassDeclaration / CheckDeclareClass resolve the signatures
  inside the class type-environment scope (so value types can reference the
  class's own type parameters).
- Index access: indexing a class instance (TypeInfo.Class or Instance<Class>)
  by string/number/symbol returns the matching index value type, with the
  TypeScript number-key-uses-string-index fallback. Excess-property checking
  treats a class index signature like an interface/record one.

Scope: index-signature *value-type* assignability (e.g. a number-index not
assignable to a string-index target) is intentionally not implemented here — it
is a pre-existing general gap (not enforced for Record<->Record either) and is
orthogonal to this feature. As with #122, conformance baseline movement awaits
#125 (recovery-path TsCode). New ClassIndexSignatureTests (7) cover access and
modelling. Full xUnit suite green (only pre-existing packaging failures);
conformance baseline unchanged (no regressions).
@nickna nickna merged commit 08fccd0 into main Jun 6, 2026
3 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