diff --git a/examples/index.ts b/examples/index.ts index de5058a87..e75241a79 100644 --- a/examples/index.ts +++ b/examples/index.ts @@ -1,133 +1,155 @@ import { TypeSystem } from '@sinclair/typebox/system' import { TypeCompiler } from '@sinclair/typebox/compiler' import { Value, ValuePointer } from '@sinclair/typebox/value' -import { Type, Static, TSchema, TProperties, TArray, TLiteral, TTemplateLiteral, TypeGuard, TemplateLiteralParser, TemplateLiteralGenerator, TemplateLiteralResolver } from '@sinclair/typebox' +import { + Type, + Static, + TSchema, + TProperties, + TArray, + TLiteral, + TTemplateLiteral, + TypeGuard, + TemplateLiteralParser, + TemplateLiteralGenerator, + TemplateLiteralResolver, + TemplateLiteralFinite, + IsTemplateLiteralFiniteCheck, + KeyResolver, + TOptional, + UnionTypeIsOptional, +} from '@sinclair/typebox' import { TObject, TUnion, UnionToTuple, TInteger, TBigInt, TIntersect, TString, TNumber, TBoolean, TNever, TTuple, UnionType, TRecursive } from '@sinclair/typebox' -// ------------------------------------------------------------------ -// TIndex -// ------------------------------------------------------------------ -// prettier-ignore -export type TIndexProperties = - K extends keyof T - ? [T[K]] - : [] -// prettier-ignore -export type TIndexTuple = - K extends keyof T - ? [T[K]] - : [] -// prettier-ignore -export type TIndexArray = - K extends number - ? [T] - : [] -// prettier-ignore -export type TIndexIntersect = - T extends [infer L extends TSchema, ...infer R extends TSchema[]] - ? [...TIndexKey, ...TIndexIntersect] - : [] -// prettier-ignore -export type TIndexUnionGather = - T extends [infer L extends TSchema, ...infer R extends TSchema[]] - ? [TIndexKey, ...TIndexUnionGather] - : [] -// prettier-ignore -export type TIndexUnionCheck = - T extends [infer L extends TSchema[], ...infer R extends TSchema[][]] - ? L extends [] - ? false - : TIndexUnionCheck - : true -// prettier-ignore -export type TIndexUnionFinal = - T extends [infer L extends TSchema[], ...infer R extends TSchema[][]] - ? [...L, ...TIndexUnionFinal] - : [] -// prettier-ignore -export type TIndexUnion, C extends boolean = TIndexUnionCheck> = - C extends true - ? TIndexUnionFinal - : [] -// prettier-ignore -export type TIndexKey = - T extends TRecursive ? TIndexKey : - T extends TIntersect ? TIndexIntersect : - T extends TUnion ? TIndexUnion : - T extends TTuple ? TIndexTuple : - T extends TArray ? TIndexArray : - T extends TObject ? TIndexProperties : - [] -// prettier-ignore -export type TIndexKeys = - K extends [infer L extends PropertyKey, ...infer R extends PropertyKey[]] - ? [...TIndexKey, ...TIndexKeys] - : [] -export type TIndex> = UnionType +const T = Type.Intersect([Type.Object({ x: Type.Optional(Type.Number()) }), Type.Object({ x: Type.Optional(Type.Number()) })]) -export function Index(schema: T, keys: [...K]): TIndex -export function Index>(schema: T, keys: I): TIndex -export function Index(schema: TSchema, keys: unknown): any { +const S = Type.Intersect([Type.Number(), Type.Object({ x: Type.Number() })]) -} -// ------------------------------------------------------------------ -// TIndexer -// ------------------------------------------------------------------ -export type TIndexerTemplateLiteral = UnionToTuple> -export type TIndexerUnion = - T extends [infer L extends TSchema, ...infer R extends TSchema[]] - ? [...TIndexer, ...TIndexerUnion] - : [] -// prettier-ignore -export type TIndexer = - T extends TTemplateLiteral ? TIndexerTemplateLiteral : - T extends TUnion ? TIndexerUnion : - T extends TLiteral ? [S] : - T extends TString ? [string] : - T extends TNumber ? [number] : - T extends TInteger ? [number] : - T extends TBigInt ? [bigint] : - [] - -export namespace Indexer { - function TIndexerTemplateLiteral(schema: TUnion | TString | TNever): string[] { - switch(true) { - case TypeGuard.TUnionLiteral(schema): return schema.anyOf.map(schema => schema.const.toString()) - case TypeGuard.TString(schema): return [`{string}`] - default: return [] - } - } - function TIndexerUnion(schemas: TSchema[]): string[] { - const [L, R] = [schemas.slice(0, 1), ...schemas.slice(1)] - return [] - } - function Visit(schema: TSchema, keys: string): string[] { - switch (true) { - case TypeGuard.TTemplateLiteral(schema): return TIndexerTemplateLiteral(TemplateLiteralResolver.Resolve(schema)) - case TypeGuard.TUnion(schema): return TIndexerUnion(schema.anyOf) - case TypeGuard.TLiteral(schema): return [schema.const.toString()] - case TypeGuard.TString(schema): return ['{string}'] - case TypeGuard.TNumber(schema): return ['{number}'] - case TypeGuard.TInteger(schema): return ['{number}'] - case TypeGuard.TBigInt(schema): return ['{bigint}'] - default: return [] - } - } -} +type A = Static +const I = Type.Index(T, Type.KeyOf(T)) - - -const A = Type.TemplateLiteral('a${1|2|3}') - -const R = TemplateLiteralResolver.Resolve(A) -console.log(R) - -const S = Type.Intersect([ - Type.Object({ x: Type.Number() }), - Type.Object({ y: Type.String() }), - Type.Object({ z: Type.Boolean() }) -]) +// prettier-ignore +// export type UnionTypeIsOptional = +// T extends [infer L extends TSchema, ...infer R extends TSchema[]] +// ? L extends TOptional +// ? UnionTypeIsOptional +// : false +// : true + +type X = UnionTypeIsOptional<[ + TOptional, + TOptional, +]> + +// // ------------------------------------------------------------------ +// // TIndex +// // ------------------------------------------------------------------ +// // prettier-ignore +// export type TIndexProperties = +// K extends keyof T +// ? [T[K]] +// : [] +// // prettier-ignore +// export type TIndexTuple = +// K extends keyof T +// ? [T[K]] +// : [] +// // prettier-ignore +// export type TIndexArray = +// K extends number +// ? [T] +// : [] +// // prettier-ignore +// export type TIndexIntersect = +// T extends [infer L extends TSchema, ...infer R extends TSchema[]] +// ? [...TIndexKey, ...TIndexIntersect] +// : [] +// // prettier-ignore +// export type TIndexUnionGather = +// T extends [infer L extends TSchema, ...infer R extends TSchema[]] +// ? [TIndexKey, ...TIndexUnionGather] +// : [] +// // prettier-ignore +// export type TIndexUnionCheck = +// T extends [infer L extends TSchema[], ...infer R extends TSchema[][]] +// ? L extends [] +// ? false +// : TIndexUnionCheck +// : true +// // prettier-ignore +// export type TIndexUnionFinal = +// T extends [infer L extends TSchema[], ...infer R extends TSchema[][]] +// ? [...L, ...TIndexUnionFinal] +// : [] +// // prettier-ignore +// export type TIndexUnion, C extends boolean = TIndexUnionCheck> = +// C extends true +// ? TIndexUnionFinal +// : [] +// // prettier-ignore +// export type TIndexKey = +// T extends TRecursive ? TIndexKey : +// T extends TIntersect ? TIndexIntersect : +// T extends TUnion ? TIndexUnion : +// T extends TTuple ? TIndexTuple : +// T extends TArray ? TIndexArray : +// T extends TObject ? TIndexProperties : +// [] +// // prettier-ignore +// export type TIndexKeys = +// K extends [infer L extends PropertyKey, ...infer R extends PropertyKey[]] +// ? [...TIndexKey, ...TIndexKeys] +// : [] +// export type TIndex> = UnionType + +// export function Index(schema: T, keys: [...K]): TIndex +// export function Index>(schema: T, keys: I): TIndex +// export function Index(schema: TSchema, keys: unknown): any { + +// } + +// import {} from '@sinclair/typebox' +// // ------------------------------------------------------------------ +// // TIndexer +// // ------------------------------------------------------------------ +// export type TKeyResolverTemplateLiteral> = +// F extends true +// ? UnionToTuple> +// : [] +// export type TKeyResolverUnion = +// T extends [infer L extends TSchema, ...infer R extends TSchema[]] +// ? [...TKeyResolver, ...TKeyResolverUnion] +// : [] +// // prettier-ignore +// export type TKeyResolver = +// T extends TTemplateLiteral ? TKeyResolverTemplateLiteral : +// T extends TUnion ? TKeyResolverUnion : +// T extends TLiteral ? [S] : +// T extends TString ? [] : +// T extends TNumber ? [] : +// T extends TInteger ? [] : +// T extends TBigInt ? [] : +// [] + +// const T = Type.TemplateLiteral('a${a|b}${string}') +// const S = KeyResolver.ResolveKeys(T, { includePatterns: false }) + +// console.log(S, 1) + +// console.log(S) + +// const E = Type.TemplateLiteral('a${string}') +// type E = TKeyResolver + +// const R = TemplateLiteralResolver.Resolve(A) +// console.log(R) + +// const S = Type.Intersect([ +// Type.Object({ x: Type.Number() }), +// Type.Object({ y: Type.String() }), +// Type.Object({ z: Type.Boolean() }) +// ]) // const T = Type.Object({ // x: Type.Object({ @@ -139,7 +161,6 @@ const S = Type.Intersect([ // type T = Static - // type A = TObject<{ x: TNumber }> // type B = TObject<{ x: TString }> // type C = TIntersect<[A, B]> @@ -151,12 +172,10 @@ const S = Type.Intersect([ // type S = TIndex - // type Q = Array<{ x: 1 }> // type U = Q[number] - - - - +export function Resolve(T: TObject<{}>, arg1: {}) { + throw new Error('Function not implemented.') +} diff --git a/package-lock.json b/package-lock.json index 88740ec7e..7f6bb758f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -16,7 +16,7 @@ "ajv-formats": "^2.1.1", "mocha": "^9.2.2", "prettier": "^2.7.1", - "typescript": "^5.3.2" + "typescript": "^5.4.0-dev.20231122" } }, "node_modules/@esbuild/linux-loong64": { @@ -1317,9 +1317,9 @@ } }, "node_modules/typescript": { - "version": "5.3.2", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.3.2.tgz", - "integrity": "sha512-6l+RyNy7oAHDfxC4FzSJcz9vnjTKxrLpDG5M2Vu4SHRVNg6xzqZp6LYSR9zjqQTu8DU/f5xwxUdADOkbrIX2gQ==", + "version": "5.4.0-dev.20231122", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.4.0-dev.20231122.tgz", + "integrity": "sha512-jEdb2OJDseS3Vi8dg4ARi3ZDqwopyxJP9qsIFawXzTrVSfcIj4WGbBvDx4MIJuwFqGhkJ4G2cJESi3+eN0Rydw==", "dev": true, "bin": { "tsc": "bin/tsc", @@ -2279,9 +2279,9 @@ } }, "typescript": { - "version": "5.3.2", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.3.2.tgz", - "integrity": "sha512-6l+RyNy7oAHDfxC4FzSJcz9vnjTKxrLpDG5M2Vu4SHRVNg6xzqZp6LYSR9zjqQTu8DU/f5xwxUdADOkbrIX2gQ==", + "version": "5.4.0-dev.20231122", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.4.0-dev.20231122.tgz", + "integrity": "sha512-jEdb2OJDseS3Vi8dg4ARi3ZDqwopyxJP9qsIFawXzTrVSfcIj4WGbBvDx4MIJuwFqGhkJ4G2cJESi3+eN0Rydw==", "dev": true }, "uri-js": { diff --git a/package.json b/package.json index 064dc20a4..f3c144a3d 100644 --- a/package.json +++ b/package.json @@ -59,6 +59,6 @@ "ajv-formats": "^2.1.1", "mocha": "^9.2.2", "prettier": "^2.7.1", - "typescript": "^5.3.2" + "typescript": "^5.4.0-dev.20231122" } } diff --git a/src/typebox.ts b/src/typebox.ts index be90cb8b3..9476d489f 100644 --- a/src/typebox.ts +++ b/src/typebox.ts @@ -74,65 +74,71 @@ export type TOptional = T & { [Optional]: 'Optional' } // -------------------------------------------------------------------------- // prettier-ignore export type ReadonlyUnwrapType = - T extends TReadonly ? ReadonlyUnwrapType : - T extends TOptional ? TOptional> : + T extends TReadonly ? ReadonlyUnwrapType : + T extends TOptional ? TOptional> : T // prettier-ignore -export type ReadonlyUnwrapRest = T extends [infer L, ...infer R] - ? L extends TReadonly - ? [ReadonlyUnwrapType>, ...ReadonlyUnwrapRest>] - : [L, ...ReadonlyUnwrapRest>] +export type ReadonlyUnwrapRest = + T extends [infer L extends TSchema, ...infer R extends TSchema[]] + ? L extends TReadonly + ? [ReadonlyUnwrapType, ...ReadonlyUnwrapRest] + : [L, ...ReadonlyUnwrapRest] : [] // -------------------------------------------------------------------------- // Optional Unwrap // -------------------------------------------------------------------------- // prettier-ignore export type OptionalUnwrapType = - T extends TReadonly ? TReadonly> : - T extends TOptional ? OptionalUnwrapType : + T extends TReadonly ? TReadonly> : + T extends TOptional ? OptionalUnwrapType : T // prettier-ignore -export type OptionalUnwrapRest = T extends [infer L, ...infer R] - ? L extends TOptional - ? [OptionalUnwrapType>, ...OptionalUnwrapRest>] - : [L, ...OptionalUnwrapRest>] - : [] +export type OptionalUnwrapRest = + T extends [infer L extends TSchema, ...infer R extends TSchema[]] + ? L extends TOptional + ? [OptionalUnwrapType, ...OptionalUnwrapRest] + : [L, ...OptionalUnwrapRest] + : [] // -------------------------------------------------------------------------- // IntersectType // -------------------------------------------------------------------------- // prettier-ignore -export type IntersectOptional = T extends [infer L, ...infer R] - ? L extends TOptional> - ? IntersectOptional> +export type IntersectTypeIsOptional = + T extends [infer L extends TSchema, ...infer R extends TSchema[]] + ? L extends TOptional + ? IntersectTypeIsOptional : false : true // prettier-ignore -export type IntersectResolve>> = IntersectOptional> extends true - ? TOptional>> - : TIntersect> +export type IntersectTypeOptional> = + IntersectTypeIsOptional extends true + ? TOptional> + : TIntersect // prettier-ignore export type IntersectType = - T extends [] ? TNever : - T extends [TSchema] ? AssertType : - IntersectResolve + T extends [] ? TNever : + T extends [TSchema] ? AssertType : + IntersectTypeOptional // -------------------------------------------------------------------------- // UnionType // -------------------------------------------------------------------------- // prettier-ignore -export type UnionOptional = T extends [infer L, ...infer R] - ? L extends (TOptional>) - ? true - : UnionOptional> - : false +export type UnionTypeIsOptional = + T extends [infer L extends TSchema, ...infer R extends TSchema[]] + ? L extends TOptional + ? true + : UnionTypeIsOptional + : false // prettier-ignore -export type UnionResolve>> = UnionOptional> extends true - ? TOptional>> - : TUnion> +export type UnionTypeOptional> = + UnionTypeIsOptional extends true + ? TOptional> + : TUnion // prettier-ignore export type UnionType = T extends [] ? TNever : - T extends [TSchema] ? AssertType : - UnionResolve + T extends [TSchema] ? T[0] : + UnionTypeOptional // -------------------------------------------------------------------------- // TSchema // -------------------------------------------------------------------------- @@ -252,8 +258,9 @@ export interface TAsyncIterator extends TSchema { // TAwaited // ------------------------------------------------------------------------------- // prettier-ignore -export type TAwaitedRest = T extends [infer L, ...infer R] - ? [TAwaited>, ...TAwaitedRest>] +export type TAwaitedRest = + T extends [infer L extends TSchema, ...infer R extends TSchema[]] + ? [TAwaited, ...TAwaitedRest] : [] // prettier-ignore export type TAwaited = @@ -289,12 +296,14 @@ export type TInstanceType> = T['retur // TComposite // -------------------------------------------------------------------------- // prettier-ignore -export type TCompositeKeys = T extends [infer L, ...infer R] - ? keyof Assert['properties'] | TCompositeKeys> - : never +export type TCompositeKeys = + T extends [infer L extends TObject, ...infer R extends TObject[]] + ? keyof L['properties'] | TCompositeKeys + : never // prettier-ignore -export type TCompositeIndex, K extends string[]> = K extends [infer L, ...infer R] - ? { [_ in Assert]: TIndexType> } & TCompositeIndex> +export type TCompositeIndex, K extends string[]> = + K extends [infer L extends string, ...infer R extends string[]] + ? { [_ in L]: TIndex } & TCompositeIndex : {} // prettier-ignore export type TCompositeReduce = UnionToTuple> extends infer K @@ -354,8 +363,10 @@ export interface TEnum = Record = - (Static extends Static ? T : U) extends infer O ? - UnionToTuple extends [infer X, infer Y] ? TUnion<[AssertType, AssertType]> : AssertType + (Static extends Static ? T : U) extends infer O extends TSchema ? + UnionToTuple extends [infer X extends TSchema, infer Y extends TSchema] + ? TUnion<[X, Y]> + : O : never // -------------------------------------------------------------------------- // TExclude @@ -365,7 +376,7 @@ export type TExcludeTemplateLiteral = AssertRest> extends Static ? never : T[K] -}[number]>> extends infer R ? UnionType> : never +}[number]>> extends infer R extends TSchema[] ? UnionType : never // prettier-ignore export type TExclude = T extends TTemplateLiteral ? TExcludeTemplateLiteral : @@ -379,7 +390,7 @@ export type TExtractTemplateLiteral = AssertRest> extends Static ? T[K] : never -}[number]>> extends infer R ? UnionType> : never +}[number]>> extends infer R extends TSchema[] ? UnionType : never // prettier-ignore export type TExtract = T extends TTemplateLiteral ? TExtractTemplateLiteral : @@ -399,40 +410,106 @@ export interface TFunction = T extends [infer L, ...infer R] ? [TIndexType, K>, ...TIndexRest, K>] : [] -export type TIndexProperty = K extends keyof T ? [T[K]] : [] -export type TIndexTuple = K extends keyof T ? [T[K]] : [] +export type TKeyResolverTemplateLiteral> = F extends true ? UnionToTuple> : [] +export type TKeyResolverUnion = T extends [infer L extends TSchema, ...infer R extends TSchema[]] ? [...TKeyResolver, ...TKeyResolverUnion] : [] +export type TKeyResolverLiteral = T extends PropertyKey ? [T] : [] // prettier-ignore -export type TIndexType = - T extends TRecursive ? TIndexType : - T extends TIntersect ? IntersectType>, TNever>>> : - T extends TUnion ? UnionType>>> : - T extends TObject ? UnionType>>> : - T extends TTuple ? UnionType>>> : +export type TKeyResolver = + T extends TTemplateLiteral ? TKeyResolverTemplateLiteral : + T extends TUnion ? TKeyResolverUnion : + T extends TLiteral ? TKeyResolverLiteral : + T extends TString ? [] : + T extends TNumber ? [] : + T extends TInteger ? [] : + T extends TBigInt ? [] : [] +// ------------------------------------------------------------------ +// TIndex +// ------------------------------------------------------------------ // prettier-ignore -export type TIndexRestMany = - K extends [infer L, ...infer R] ? [TIndexType>, ...TIndexRestMany>] : - [] +export type TIndexProperties = + K extends keyof T + ? [T[K]] + : [] // prettier-ignore -export type TIndex = - T extends TRecursive ? TIndex : - T extends TIntersect ? UnionType>> : - T extends TUnion ? UnionType>> : - T extends TObject ? UnionType>> : - T extends TTuple ? UnionType>> : - TNever +export type TIndexTuple = + K extends keyof T + ? [T[K]] + : [] +// prettier-ignore +export type TIndexArray = + K extends number + ? [T] + : [] +// prettier-ignore +export type TIndexIntersect = + T extends [infer L extends TSchema, ...infer R extends TSchema[]] + ? [...TIndexKeyResolve, ...TIndexIntersect] + : [] +// prettier-ignore +export type TIndexUnionGather = + T extends [infer L extends TSchema, ...infer R extends TSchema[]] + ? [TIndexKeyResolve, ...TIndexUnionGather] + : [] +// prettier-ignore +export type TIndexUnionCheck = + T extends [infer L extends TSchema[], ...infer R extends TSchema[][]] + ? L extends [] + ? false + : TIndexUnionCheck + : true +// prettier-ignore +export type TIndexUnionFinal = + T extends [infer L extends TSchema[], ...infer R extends TSchema[][]] + ? [...L, ...TIndexUnionFinal] + : [] +// prettier-ignore +export type TIndexUnion, C extends boolean = TIndexUnionCheck> = + C extends true + ? TIndexUnionFinal + : [] +// prettier-ignore +export type TIndexKeyResolve = + T extends TRecursive ? TIndexKeyResolve : + T extends TIntersect ? TIndexIntersect : + T extends TUnion ? TIndexUnion : + T extends TTuple ? TIndexTuple : + T extends TArray ? TIndexArray : + T extends TObject ? TIndexProperties : + [] +// prettier-ignore +export type TIndexKey> = + R extends TSchema[] + ? IntersectType + : TNever +// prettier-ignore +export type TIndexKeysResolve = + K extends [infer L extends PropertyKey, ...infer R extends PropertyKey[]] + ? [...TIndexKeyResolve, ...TIndexKeysResolve] + : [] +// prettier-ignore +export type TIndexKeys> = + R extends TSchema[] + ? UnionType + : TNever +export type TIndex = TIndexKeys // -------------------------------------------------------------------------- // TIntrinsic // -------------------------------------------------------------------------- export type TIntrinsicMode = 'Uppercase' | 'Lowercase' | 'Capitalize' | 'Uncapitalize' // prettier-ignore export type TIntrinsicTemplateLiteral = - M extends ('Lowercase' | 'Uppercase') ? T extends [infer L, ...infer R] ? [TIntrinsic, M>, ...TIntrinsicTemplateLiteral, M>] : T : - M extends ('Capitalize' | 'Uncapitalize') ? T extends [infer L, ...infer R] ? [TIntrinsic, M>, ...R] : T : - T + M extends ('Lowercase' | 'Uppercase') ? + T extends [infer L extends TTemplateLiteralKind, ...infer R extends TTemplateLiteralKind[]] + ? [TIntrinsic, ...TIntrinsicTemplateLiteral] + : T : + M extends ('Capitalize' | 'Uncapitalize') + ? T extends [infer L extends TTemplateLiteralKind, ...infer R extends TTemplateLiteralKind[]] + ? [TIntrinsic, ...TIntrinsicTemplateLiteral] + : T + : T // prettier-ignore export type TIntrinsicLiteral = T extends string ? @@ -443,8 +520,9 @@ export type TIntrinsicLiteral = string : T // prettier-ignore -export type TIntrinsicRest = T extends [infer L, ...infer R] - ? [TIntrinsic, M>, ...TIntrinsicRest, M>] +export type TIntrinsicRest = + T extends [infer L extends TSchema, ...infer R extends TSchema[]] + ? [TIntrinsic, ...TIntrinsicRest] : [] // prettier-ignore export type TIntrinsic = @@ -467,9 +545,10 @@ export type TUnevaluatedProperties = undefined | TSchema | boolean export interface IntersectOptions extends SchemaOptions { unevaluatedProperties?: TUnevaluatedProperties } +export type TIntersectResolve = T extends [infer L extends TSchema, ...infer R extends TSchema[]] ? Static & TIntersectResolve : {} export interface TIntersect extends TSchema, IntersectOptions { [Kind]: 'Intersect' - static: TupleToIntersect<{ [K in keyof T]: Static, this['params']> }> + static: TIntersectResolve type?: 'object' allOf: [...T] } @@ -2489,6 +2568,14 @@ export namespace KeyResolver { function UnwrapPattern(key: string) { return key[0] === '^' && key[key.length - 1] === '$' ? key.slice(1, key.length - 1) : key } + function TTemplateLiteral(schema: TTemplateLiteral, options: KeyResolverOptions): string[] { + const expression = TemplateLiteralParser.ParseExact(schema.pattern) + const finite = TemplateLiteralFinite.Check(expression) + if (!finite && options.includePatterns) return [schema.pattern] + if (!finite && !options.includePatterns) return [] + const resolved = TemplateLiteralResolver.Resolve(schema) + return TypeGuard.TUnionLiteral(resolved) ? resolved.anyOf.map((schema) => schema.const.toString()) : [] + } function TIntersect(schema: TIntersect, options: KeyResolverOptions): string[] { return schema.allOf.reduce((acc, schema) => [...acc, ...Visit(schema, options)], [] as string[]) } @@ -2505,6 +2592,7 @@ export namespace KeyResolver { function Visit(schema: TSchema, options: KeyResolverOptions): string[] { // prettier-ignore return ( + TypeGuard.TTemplateLiteral(schema) ? TTemplateLiteral(schema, options) : TypeGuard.TIntersect(schema) ? TIntersect(schema, options) : TypeGuard.TUnion(schema) ? TUnion(schema, options) : TypeGuard.TObject(schema) ? TObject(schema, options) : @@ -2982,7 +3070,7 @@ export class JsonTypeBuilder extends TypeBuilder { // prettier-ignore return ( TypeGuard.TTemplateLiteral(type) ? this.Extract(TemplateLiteralResolver.Resolve(type), union, options) : - TypeGuard.TTemplateLiteral(union) ? this.Extract(type, TemplateLiteralResolver.Resolve(union), options) : + TypeGuard.TTemplateLiteral(union) ? this.Extract(type, TemplateLiteralResolver.Resolve(union) as any, options) : TypeGuard.TUnion(type) ? (() => { const narrowed = type.anyOf.filter((inner) => TypeExtends.Extends(inner, union) !== TypeExtendsResult.False) return (narrowed.length === 1 ? TypeClone.Type(narrowed[0], options) : this.Union(narrowed, options)) @@ -2991,22 +3079,27 @@ export class JsonTypeBuilder extends TypeBuilder { this.Never(options) ) as TExtract } + // /** `[Json]` Returns an Indexed property type for the given keys */ + // public Index(schema: T, keys: K, options?: SchemaOptions): AssertType + // /** `[Json]` Returns an Indexed property type for the given keys */ + // public Index)[]>(schema: T, keys: [...K], options?: SchemaOptions): TIndex> + // /** `[Json]` Returns an Indexed property type for the given keys */ + // public Index(schema: T, keys: K, options?: SchemaOptions): UnionType> + // /** `[Json]` Returns an Indexed property type for the given keys */ + // public Index(schema: T, keys: K, options?: SchemaOptions): TIndex> + // /** `[Json]` Returns an Indexed property type for the given keys */ + // public Index>(schema: T, keys: K, options?: SchemaOptions): TIndex + // /** `[Json]` Returns an Indexed property type for the given keys */ + // public Index)[]>(schema: T, keys: [...K], options?: SchemaOptions): TIndex> + // /** `[Json]` Returns an Indexed property type for the given keys */ + // public Index[]>>(schema: T, keys: K, options?: SchemaOptions): TIndex> + // /** `[Json]` Returns an Indexed property type for the given keys */ + // public Index(schema: T, key: K, options?: SchemaOptions): TSchema + /** `[Json]` Returns an Indexed property type for the given keys */ - public Index(schema: T, keys: K, options?: SchemaOptions): AssertType - /** `[Json]` Returns an Indexed property type for the given keys */ - public Index)[]>(schema: T, keys: [...K], options?: SchemaOptions): TIndex> - /** `[Json]` Returns an Indexed property type for the given keys */ - public Index(schema: T, keys: K, options?: SchemaOptions): UnionType> - /** `[Json]` Returns an Indexed property type for the given keys */ - public Index(schema: T, keys: K, options?: SchemaOptions): TIndex> - /** `[Json]` Returns an Indexed property type for the given keys */ - public Index>(schema: T, keys: K, options?: SchemaOptions): TIndex - /** `[Json]` Returns an Indexed property type for the given keys */ - public Index)[]>(schema: T, keys: [...K], options?: SchemaOptions): TIndex> - /** `[Json]` Returns an Indexed property type for the given keys */ - public Index[]>>(schema: T, keys: K, options?: SchemaOptions): TIndex> + public Index>(schema: T, keys: I, options?: SchemaOptions): TIndex /** `[Json]` Returns an Indexed property type for the given keys */ - public Index(schema: T, key: K, options?: SchemaOptions): TSchema + public Index(schema: T, keys: [...K], options?: SchemaOptions): TIndex /** `[Json]` Returns an Indexed property type for the given keys */ public Index(schema: TSchema, unresolved: any, options: SchemaOptions = {}): any { // prettier-ignore