Skip to content

fix: emit patternProperties for numeric index signatures - #2553

Open
rqms40 wants to merge 7 commits into
vega:nextfrom
rqms40:fix/numeric-index-signature-pattern-properties
Open

rqms40 wants to merge 7 commits into
vega:nextfrom
rqms40:fix/numeric-index-signature-pattern-properties

Conversation

@rqms40

@rqms40 rqms40 commented Oct 22, 2026

Copy link
Copy Markdown

Closes #2134

What

{ [index: number]: T } (and Record<number, T>) used to produce additionalProperties: T, which also accepts non-numeric keys. A numeric index now produces:

{
  "type": "object",
  "additionalProperties": false,
  "patternProperties": { "^[0-9]+$": T }
}
  • When a string index is also present, the string index still owns additionalProperties, and the numeric index only narrows ^[0-9]+$.
  • Record<string | number, T> stays additionalProperties: T, because both key kinds share one value type.
  • Index-signature handling for interfaces, classes and type literals is consolidated in src/Utils/indexSignature.ts, and the mapped-type, keyof, intersection and allOf paths are updated to carry patternProperties through.

Behaviour change

The output for numeric-keyed maps changes. test/valid-data/type-mapped-number/schema.json is updated to match. Let me know if you'd rather put this behind an option or treat it as a breaking change.

Testing

  • New fixture: test/valid-data/numeric-index-signature
  • npm run build, npm run lint (no new warnings) and npm test (380 passed) all succeed.

A numeric index signature is not an open set of keys. Emit
patternProperties for "^[0-9]+$" and keep additionalProperties for a
string index when both signatures are present.
An intersection of two numeric index signatures must satisfy both value
schemas. Combine a shared pattern key with allOf instead of keeping only
one side.
… as objects

allOf of two closed object schemas rejects a property that exists on only
one side. Merge object-valued numeric indexes with the existing object
intersection, and keep Partial of a numeric index as an object.
A $ref pattern value is not an object schema, so the intersection fell
through to allOf and the closed base rejected properties added by the
derived type. Resolve those refs to their object schemas before merging.
An intermediate interface stores a cloned pattern $ref, which drops the
WeakMap back to the named object. Reattach that source so the next merge
can still resolve it.
Cloning an object schema dropped WeakMap metadata on nested pattern
$refs, so an inner numeric index fell back to allOf. Reattach those
sources on every nested schema in the merge clones.
Merging a numeric index that refers to itself resolved the same $ref pair
forever. When that pair is already being merged, keep the ref or fall back
to allOf.
@domoritz

Copy link
Copy Markdown
Member

Did you manually review the code in this pull request?

@rqms40

rqms40 commented Oct 23, 2026

Copy link
Copy Markdown
Author

Thanks for asking. Yes, though I should be upfront: the code was written with AI assistance, and I ran it through an AI review before opening the PR. I've since gone through the changes myself and can explain any part of it.

Two known limitations remain with recursive types: mixed recursive nesting (e.g. interface A { [n: number]: { [m: number]: A } } extended with [n: number]: A) still overflows the stack, and a recursive subtype that adds a field can reject valid data. The PR also grew larger than I'd like. If it's easier to review, I'm happy to cut it down to just the patternProperties mapping from #2134 and handle overlapping index merging separately, or fix the recursive cases first. Whatever you prefer.

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.

[key: number] implementation is incorrectly open-ended, like [key: string]

2 participants