feat: model class-body index signatures (#123)#127
Merged
Conversation
`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).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
TryParseClassIndexSignaturereturns the parsedStmt.IndexSignature(key name, key-type token, value type) instead of discarding;ClassDeclarationcollects them into a newStmt.Class.IndexSignaturesslot.ClassMetadataCore,MutableClass, andTypeInfo.ClassgainString/Number/SymbolIndexType(optional defaulted — existing construction sites unaffected).CheckClassDeclaration/CheckDeclareClassresolve the signatures inside the class type-environment scope (so value types can reference the class's own type parameters).TypeInfo.ClassorInstance<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
ClassIndexSignatureTests(7): parsing, string/number index access, type-mismatch errors, numeric-key fallback, ambientdeclare class, and computed-property-name disambiguation.Scope notes
Record↔Recordeither) and orthogonal to this feature.assignmentCompatWith{String,Numeric}Indexertests until TS Conformance: propagate TSnnnn codes through the recovery path (unlocks negative-test matching) #125 (recovery-pathTsCode) lands.const C = class { [k]: T }) are deferred (rare); only class declarations are covered.