diff --git a/examples/index.ts b/examples/index.ts index 93cc8464e..2c42027ca 100644 --- a/examples/index.ts +++ b/examples/index.ts @@ -1,13 +1,71 @@ import { TypeSystem } from '@sinclair/typebox/system' import { TypeCompiler } from '@sinclair/typebox/compiler' import { Value, ValuePointer } from '@sinclair/typebox/value' -import { Type, TypeGuard, Kind, Static, TSchema, TProperties, TArray, TLiteral } from '@sinclair/typebox' -import { TObject, TUnion, TIntersect, TString, TNumber, TBoolean, TNever, TTuple, UnionType, TRecursive } from '@sinclair/typebox' +import { Type, TypeGuard, Kind, Static, TSchema, TProperties, TArray, TLiteral, TTemplateLiteral } from '@sinclair/typebox' +import { TObject, TUnion, TInteger, TBigInt, TIntersect, TString, TNumber, TBoolean, TNever, TTuple, UnionType, TRecursive } from '@sinclair/typebox' +// ------------------------------------------------------------------ +// TemplateLiteralFinite +// ------------------------------------------------------------------ +// prettier-ignore +export type IsTemplateLiteralFiniteCheck = + T extends TTemplateLiteral ? IsTemplateLiteralFiniteRest : + T extends TUnion ? IsTemplateLiteralFiniteRest : + T extends TString ? false : + T extends TBoolean ? false : + T extends TNumber ? false : + T extends TInteger ? false : + T extends TBigInt ? false : + T extends TLiteral ? true : + false +// prettier-ignore +export type IsTemplateLiteralFiniteRest = + T extends [infer L extends TTemplateLiteralKind, ...infer R extends TTemplateLiteralKind[]] + ? IsTemplateLiteralFiniteCheck extends false + ? false + : IsTemplateLiteralFiniteRest + : true +// prettier-ignore +export type IsTemplateLiteralFinite = + T extends TTemplateLiteral + ? IsTemplateLiteralFiniteRest + : false +// ------------------------------------------------------------------ +// TemplateLiteralUnion +// ------------------------------------------------------------------ +// prettier-ignore +export type TTemplateLiteralKind = + | TUnion + | TLiteral + | TInteger + | TTemplateLiteral + | TNumber + | TBigInt + | TString + | TBoolean + | TNever +// prettier-ignore +export type TTemplateLiteralConst = + T extends TUnion ? { [K in keyof U]: TTemplateLiteralUnion, Acc> }[number] : + T extends TTemplateLiteral ? `${Static}` : + T extends TLiteral ? `${U}` : + T extends TString ? `${string}` : + T extends TNumber ? `${number}` : + T extends TBigInt ? `${bigint}` : + T extends TBoolean ? `${boolean}` : + never +// prettier-ignore +export type TTemplateLiteralUnion = + T extends [infer L, ...infer R] ? `${TTemplateLiteralConst}${TTemplateLiteralUnion, Acc>}` : + Acc + +// export type TTemplateLiteralKind = TUnion | TLiteral | TInteger | TTemplateLiteral | TNumber | TBigInt | TString | TBoolean | TNever // ------------------------------------------------------------------ // TIndexer // ------------------------------------------------------------------ +export type EmptyString = '' + // prettier-ignore export type TIndexerUnion = T extends [infer L extends TSchema, ...infer R extends TSchema[]] @@ -15,11 +73,17 @@ export type TIndexerUnion = : [] // prettier-ignore export type TIndexer = + T extends TTemplateLiteral ? TTemplateLiteralUnion : T extends TUnion ? TIndexerUnion : T extends TLiteral ? [S] : T extends TString ? [string] : - T extends TNumber ? [string] : + T extends TNumber ? [number] : [] + +const T = Type.TemplateLiteral('a${1|2|3}') + +type Ss = TIndexer + // ------------------------------------------------------------------ // TIndex // ------------------------------------------------------------------ @@ -81,15 +145,22 @@ export type TIndexKeys = : [] export type TIndex> = UnionType -type A = TIndexer, TLiteral<'hello1'>, TString]>> +export function Index(schema: T, keys: [...K]): TIndex +export function Index>(schema: T, keys: I): TIndex +export function Index(schema: TSchema, keys: unknown): any { -export function Index>(schema: T, keys: I): TIndex { - throw 1 } -const T = Type.Array(Type.String()) +const S = Type.Intersect([ + Type.Object({ x: Type.Number() }), + Type.Object({ y: Type.String() }) +]) + +const A = Type.TemplateLiteral('${x|y}') + +type A = TIndexer -const I = Index(T, Type.KeyOf(T)) +const I = Index(S, Type.TemplateLiteral('${x|y}')) type S = Array diff --git a/src/typebox.ts b/src/typebox.ts index 57e8cb27b..8bc1a38bd 100644 --- a/src/typebox.ts +++ b/src/typebox.ts @@ -856,7 +856,7 @@ export type TTemplateLiteralConst = T extends TBoolean ? `${boolean}` : never // prettier-ignore -export type TTemplateLiteralUnion = +export type TTemplateLiteralUnion = T extends [infer L, ...infer R] ? `${TTemplateLiteralConst}${TTemplateLiteralUnion, Acc>}` : Acc export type TTemplateLiteralKeyRest = Assert>, TPropertyKey[]>