Support number-keyed records in record() and Describe#1297
Open
ATOM00blue wants to merge 1 commit into
Open
Conversation
Broaden record() to accept a number-keyed struct and update the StructSchema inference (via IsRecord) so that Describe<Record<number, V>> resolves to a record struct schema instead of an object schema. This fixes the TypeScript compile error when typing a number-keyed Record, while leaving string-keyed Record behaviour unchanged.
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Extends record() and related type utilities/tests to support numeric keys (Record<number, V>) in addition to string keys.
Changes:
- Add typings tests for
Record<number, number>andDescribe<Record<number, number>>. - Broaden
IsRecordto accept types withnumberkeys. - Widen
record()’s generic key constraint fromstringtostring | number.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| test/typings/record.ts | Adds typings coverage for record(number(), number()) returning Record<number, number>. |
| test/typings/describe.ts | Adds typings coverage for describing Record<number, number>. |
| src/utils.ts | Updates IsRecord conditional type to include numeric-keyed records. |
| src/structs/types.ts | Updates record() signature to allow `K extends string |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+367
to
370
| export function record<K extends string | number, V>( | ||
| Key: Struct<K>, | ||
| Value: Struct<V> | ||
| ): Struct<Record<K, V>, null> { |
Comment on lines
242
to
248
| export type IsRecord<T> = T extends object | ||
| ? string extends keyof T | ||
| ? T | ||
| : never | ||
| : number extends keyof T | ||
| ? T | ||
| : never | ||
| : never |
| return x | ||
| }) | ||
|
|
||
| test<Record<number, number>>((x) => { |
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.
Closes #929
Describe<Record<number, number>>currently produces a TypeScript compile error becauserecord()is typedK extends stringand theStructSchemainference (viaIsRecord) only collapses string-indexed records to anullschema. Number-keyed records fell through to the object-schema branch, so the inferred schema ({ [x: number]: Describe<number> }) didn't matchrecord()'snullschema.Changes
record<K extends string, V>torecord<K extends string | number, V>.IsRecord<T>to also match whennumber extends keyof T, soDescribe<Record<number, V>>resolves to a record struct schema.String-keyed
Recordbehaviour is unchanged, and numeric-literal-keyed object types (e.g.{ 0: string }) still correctly resolve to object schemas. Added type tests intest/typings/describe.tsandtest/typings/record.ts.