Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin' into defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
sinclairzx81 committed Oct 26, 2023
2 parents a723662 + a9cfd7e commit fc16bd9
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 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.

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 fc16bd9

Please sign in to comment.