diff --git a/package-lock.json b/package-lock.json index c831cfa01..49319b749 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@sinclair/typebox", - "version": "0.31.19", + "version": "0.31.20", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@sinclair/typebox", - "version": "0.31.19", + "version": "0.31.20", "license": "MIT", "devDependencies": { "@sinclair/hammer": "^0.18.0", diff --git a/package.json b/package.json index 85d102ed9..42fe99b9f 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/typebox.ts b/src/typebox.ts index 4514b9c80..700e1c3ad 100644 --- a/src/typebox.ts +++ b/src/typebox.ts @@ -650,6 +650,8 @@ export interface TPromise extends TSchema { export type TRecordFromUnionLiteralString = { [_ in K['const']]: T } export type TRecordFromUnionLiteralNumber = { [_ in K['const']]: T } // prettier-ignore +export type TRecordFromEnumKey = Ensure> +// prettier-ignore export type TRecordFromUnionRest = K extends [infer L, ...infer R] ? ( L extends TUnion ? TRecordFromUnionRest & TRecordFromUnionRest, T> : L extends TLiteralString ? TRecordFromUnionLiteralString & TRecordFromUnionRest, T> : @@ -669,6 +671,7 @@ export type TRecordFromNumberKey = Ensure< export type TRecordFromIntegerKey = Ensure> // prettier-ignore export type TRecordResolve = + K extends TEnum ? TRecordFromEnumKey : // Enum before Union (intercept Hint) K extends TUnion ? TRecordFromUnion : K extends TTemplateLiteral ? TRecordFromTemplateLiteralKey : K extends TLiteralString ? TRecordFromLiteralStringKey : diff --git a/test/static/record.ts b/test/static/record.ts index 819c0ad27..1951bd4cd 100644 --- a/test/static/record.ts +++ b/test/static/record.ts @@ -71,13 +71,46 @@ import { Type, Static } from '@sinclair/typebox' Expect(T).ToStatic>() } { + // 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