Skip to content

Commit

Permalink
Revision 0.31.20 (#647)
Browse files Browse the repository at this point in the history
* Support Enum Key Inference on Record Types

* Version

* Use Constrained Key Type of TEnum
  • Loading branch information
sinclairzx81 authored Oct 26, 2023
1 parent b0b6689 commit a9cfd7e
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 4 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sinclair/typebox",
"version": "0.31.19",
"version": "0.31.20",
"description": "JSONSchema Type Builder with Static Type Resolution for TypeScript",
"keywords": [
"typescript",
Expand Down
3 changes: 3 additions & 0 deletions src/typebox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,8 @@ export interface TPromise<T extends TSchema = TSchema> extends TSchema {
export type TRecordFromUnionLiteralString<K extends TLiteralString, T extends TSchema> = { [_ in K['const']]: T }
export type TRecordFromUnionLiteralNumber<K extends TLiteralNumber, T extends TSchema> = { [_ in K['const']]: T }
// prettier-ignore
export type TRecordFromEnumKey<K extends TEnum, T extends TSchema> = Ensure<TRecord<K, T>>
// prettier-ignore
export type TRecordFromUnionRest<K extends TSchema[], T extends TSchema> = K extends [infer L, ...infer R] ? (
L extends TUnion<infer S> ? TRecordFromUnionRest<S, T> & TRecordFromUnionRest<AssertRest<R>, T> :
L extends TLiteralString ? TRecordFromUnionLiteralString<L, T> & TRecordFromUnionRest<AssertRest<R>, T> :
Expand All @@ -669,6 +671,7 @@ export type TRecordFromNumberKey<K extends TNumber, T extends TSchema> = Ensure<
export type TRecordFromIntegerKey<K extends TInteger, T extends TSchema> = Ensure<TRecord<K, T>>
// prettier-ignore
export type TRecordResolve<K extends TSchema, T extends TSchema> =
K extends TEnum<infer _> ? TRecordFromEnumKey<K, T> : // Enum before Union (intercept Hint)
K extends TUnion<infer S> ? TRecordFromUnion<S, T> :
K extends TTemplateLiteral ? TRecordFromTemplateLiteralKey<K, T> :
K extends TLiteralString ? TRecordFromLiteralStringKey<K, T> :
Expand Down
35 changes: 34 additions & 1 deletion test/static/record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,46 @@ import { Type, Static } from '@sinclair/typebox'
Expect(T).ToStatic<Record<number, string>>()
}
{
// Should support enum keys 1
enum E {
A = 'X',
B = 'Y',
C = 'Z',
}
const T = Type.Record(Type.Enum(E), Type.Number())
Expect(T).ToStatic<{}>()
Expect(T).ToStatic<{
X: number
Y: number
Z: number
}>()
}
{
// Should support enum keys 2
enum E {
A,
B,
C,
}
const T = Type.Record(Type.Enum(E), Type.Number())
Expect(T).ToStatic<{
0: number
1: number
2: number
}>()
}
{
// Should support enum keys 3
enum E {
A = 1,
B = '2',
C = 'Z',
}
const T = Type.Record(Type.Enum(E), Type.Number())
Expect(T).ToStatic<{
1: number
2: number
Z: number
}>()
}
{
// should support infinite record keys
Expand Down

0 comments on commit a9cfd7e

Please sign in to comment.