@sinclair/typebox 0.34.3 → 0.34.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -48,7 +48,7 @@ export declare const Module: Runtime.Module<{
48
48
  Iterator: Runtime.ITuple<Types.TIterator<Types.TSchema>>;
49
49
  Awaited: Runtime.ITuple<Types.TSchema>;
50
50
  Array: Runtime.ITuple<Types.TArray<Types.TSchema>>;
51
- Record: Runtime.ITuple<Types.TNever>;
51
+ Record: Runtime.ITuple<Types.TSchema>;
52
52
  Promise: Runtime.ITuple<Types.TPromise<Types.TSchema>>;
53
53
  ConstructorParameters: Runtime.ITuple<Types.TTuple<Types.TSchema[]>>;
54
54
  FunctionParameters: Runtime.ITuple<Types.TTuple<Types.TSchema[]>>;
@@ -6,6 +6,7 @@ import { type TAsyncIterator } from '../async-iterator/index';
6
6
  import { TComputed } from '../computed/index';
7
7
  import { type TConstructor } from '../constructor/index';
8
8
  import { type TIndex } from '../indexed/index';
9
+ import { TEnum, TEnumRecord } from '../enum/index';
9
10
  import { type TFunction } from '../function/index';
10
11
  import { type TIntersect, type TIntersectEvaluated } from '../intersect/index';
11
12
  import { type TIterator } from '../iterator/index';
@@ -43,7 +44,7 @@ type TFromArray<ModuleProperties extends TProperties, Type extends TSchema> = (E
43
44
  type TFromAsyncIterator<ModuleProperties extends TProperties, Type extends TSchema> = (TAsyncIterator<TFromType<ModuleProperties, Type>>);
44
45
  type TFromIterator<ModuleProperties extends TProperties, Type extends TSchema> = (TIterator<TFromType<ModuleProperties, Type>>);
45
46
  type TFromRest<ModuleProperties extends TProperties, Types extends TSchema[], Result extends TSchema[] = []> = (Types extends [infer Left extends TSchema, ...infer Right extends TSchema[]] ? TFromRest<ModuleProperties, Right, [...Result, TFromType<ModuleProperties, Left>]> : Result);
46
- export type TFromType<ModuleProperties extends TProperties, Type extends TSchema> = (Type extends TComputed<infer T extends string, infer S extends TSchema[]> ? TFromComputed<ModuleProperties, T, S> : Type extends TObject<infer S extends TProperties> ? TFromObject<ModuleProperties, S> : Type extends TConstructor<infer S extends TSchema[], infer R extends TSchema> ? TFromConstructor<ModuleProperties, S, R> : Type extends TFunction<infer S extends TSchema[], infer R extends TSchema> ? TFromFunction<ModuleProperties, S, R> : Type extends TTuple<infer S extends TSchema[]> ? TFromTuple<ModuleProperties, S> : Type extends TIntersect<infer S extends TSchema[]> ? TFromIntersect<ModuleProperties, S> : Type extends TUnion<infer S extends TSchema[]> ? TFromUnion<ModuleProperties, S> : Type extends TArray<infer S extends TSchema> ? TFromArray<ModuleProperties, S> : Type extends TAsyncIterator<infer S extends TSchema> ? TFromAsyncIterator<ModuleProperties, S> : Type extends TIterator<infer S extends TSchema> ? TFromIterator<ModuleProperties, S> : Type);
47
+ export type TFromType<ModuleProperties extends TProperties, Type extends TSchema> = (Type extends TArray<infer Type extends TSchema> ? TFromArray<ModuleProperties, Type> : Type extends TAsyncIterator<infer Type extends TSchema> ? TFromAsyncIterator<ModuleProperties, Type> : Type extends TComputed<infer Target extends string, infer Parameters extends TSchema[]> ? TFromComputed<ModuleProperties, Target, Parameters> : Type extends TConstructor<infer Parameters extends TSchema[], infer InstanceType extends TSchema> ? TFromConstructor<ModuleProperties, Parameters, InstanceType> : Type extends TFunction<infer Parameters extends TSchema[], infer ReturnType extends TSchema> ? TFromFunction<ModuleProperties, Parameters, ReturnType> : Type extends TIntersect<infer Types extends TSchema[]> ? TFromIntersect<ModuleProperties, Types> : Type extends TIterator<infer Type extends TSchema> ? TFromIterator<ModuleProperties, Type> : Type extends TObject<infer Properties extends TProperties> ? TFromObject<ModuleProperties, Properties> : Type extends TTuple<infer Types extends TSchema[]> ? TFromTuple<ModuleProperties, Types> : Type extends TEnum<infer _ extends TEnumRecord> ? Type : Type extends TUnion<infer Types extends TSchema[]> ? TFromUnion<ModuleProperties, Types> : Type);
47
48
  export declare function FromType<ModuleProperties extends TProperties, Type extends TSchema>(moduleProperties: ModuleProperties, type: Type): TFromType<ModuleProperties, Type>;
48
49
  export type TComputeType<ModuleProperties extends TProperties, Key extends PropertyKey> = (Key extends keyof ModuleProperties ? TFromType<ModuleProperties, ModuleProperties[Key]> : TNever);
49
50
  export declare function ComputeType<ModuleProperties extends TProperties, Key extends PropertyKey>(moduleProperties: ModuleProperties, key: Key): TComputeType<ModuleProperties, Key>;
@@ -124,20 +124,19 @@ function FromRest(moduleProperties, types) {
124
124
  }
125
125
  // prettier-ignore
126
126
  function FromType(moduleProperties, type) {
127
- return (
128
- // Note: The 'as never' is required due to excessive resolution of TIndex. In fact TIndex, TPick, TOmit and
129
- // all need re-implementation to remove the PropertyKey[] selector. Reimplementation of these types should
130
- // be a priority as there is a potential for the current inference to break on TS compiler changes.
131
- KindGuard.IsComputed(type) ? (0, index_1.CreateType)(FromComputed(moduleProperties, type.target, type.parameters)) :
132
- KindGuard.IsObject(type) ? (0, index_1.CreateType)(FromObject(moduleProperties, type.properties), type) :
133
- KindGuard.IsConstructor(type) ? (0, index_1.CreateType)(FromConstructor(moduleProperties, type.parameters, type.returns), type) :
134
- KindGuard.IsFunction(type) ? (0, index_1.CreateType)(FromFunction(moduleProperties, type.parameters, type.returns), type) :
135
- KindGuard.IsTuple(type) ? (0, index_1.CreateType)(FromTuple(moduleProperties, type.items || []), type) :
127
+ return (KindGuard.IsArray(type) ? (0, index_1.CreateType)(FromArray(moduleProperties, type.items), type) :
128
+ KindGuard.IsAsyncIterator(type) ? (0, index_1.CreateType)(FromAsyncIterator(moduleProperties, type.items), type) :
129
+ // Note: The 'as never' is required due to excessive resolution of TIndex. In fact TIndex, TPick, TOmit and
130
+ // all need re-implementation to remove the PropertyKey[] selector. Reimplementation of these types should
131
+ // be a priority as there is a potential for the current inference to break on TS compiler changes.
132
+ KindGuard.IsComputed(type) ? (0, index_1.CreateType)(FromComputed(moduleProperties, type.target, type.parameters)) :
133
+ KindGuard.IsConstructor(type) ? (0, index_1.CreateType)(FromConstructor(moduleProperties, type.parameters, type.returns), type) :
134
+ KindGuard.IsFunction(type) ? (0, index_1.CreateType)(FromFunction(moduleProperties, type.parameters, type.returns), type) :
136
135
  KindGuard.IsIntersect(type) ? (0, index_1.CreateType)(FromIntersect(moduleProperties, type.allOf), type) :
137
- KindGuard.IsUnion(type) ? (0, index_1.CreateType)(FromUnion(moduleProperties, type.anyOf), type) :
138
- KindGuard.IsArray(type) ? (0, index_1.CreateType)(FromArray(moduleProperties, type.items), type) :
139
- KindGuard.IsAsyncIterator(type) ? (0, index_1.CreateType)(FromAsyncIterator(moduleProperties, type.items), type) :
140
- KindGuard.IsIterator(type) ? (0, index_1.CreateType)(FromIterator(moduleProperties, type.items), type) :
136
+ KindGuard.IsIterator(type) ? (0, index_1.CreateType)(FromIterator(moduleProperties, type.items), type) :
137
+ KindGuard.IsObject(type) ? (0, index_1.CreateType)(FromObject(moduleProperties, type.properties), type) :
138
+ KindGuard.IsTuple(type) ? (0, index_1.CreateType)(FromTuple(moduleProperties, type.items || []), type) :
139
+ KindGuard.IsUnion(type) ? (0, index_1.CreateType)(FromUnion(moduleProperties, type.anyOf), type) :
141
140
  type);
142
141
  }
143
142
  // prettier-ignore
@@ -3,11 +3,13 @@ import { TSchema } from '../schema/index';
3
3
  import { TArray } from '../array/index';
4
4
  import { TAsyncIterator } from '../async-iterator/index';
5
5
  import { TConstructor } from '../constructor/index';
6
+ import { TEnum, TEnumRecord } from '../enum/index';
6
7
  import { TFunction } from '../function/index';
7
8
  import { TIntersect } from '../intersect/index';
8
9
  import { TIterator } from '../iterator/index';
9
10
  import { TObject, TProperties } from '../object/index';
10
11
  import { TOptional } from '../optional/index';
12
+ import { TRecord } from '../record/index';
11
13
  import { TReadonly } from '../readonly/index';
12
14
  import { TRef } from '../ref/index';
13
15
  import { TTuple } from '../tuple/index';
@@ -18,15 +20,15 @@ type TInferAsyncIterator<ModuleProperties extends TProperties, Type extends TSch
18
20
  type TInferConstructor<ModuleProperties extends TProperties, Parameters extends TSchema[], InstanceType extends TSchema> = Ensure<new (...args: TInferTuple<ModuleProperties, Parameters>) => TInfer<ModuleProperties, InstanceType>>;
19
21
  type TInferFunction<ModuleProperties extends TProperties, Parameters extends TSchema[], ReturnType extends TSchema> = Ensure<(...args: TInferTuple<ModuleProperties, Parameters>) => TInfer<ModuleProperties, ReturnType>>;
20
22
  type TInferIterator<ModuleProperties extends TProperties, Type extends TSchema> = (Ensure<IterableIterator<TInfer<ModuleProperties, Type>>>);
21
- type TInferIntersect<ModuleProperties extends TProperties, Types extends TSchema[], Result extends unknown = unknown> = (Types extends [infer L extends TSchema, ...infer R extends TSchema[]] ? TInferIntersect<ModuleProperties, R, Result & TInfer<ModuleProperties, L>> : Result);
23
+ type TInferIntersect<ModuleProperties extends TProperties, Types extends TSchema[], Result extends unknown = unknown> = (Types extends [infer Left extends TSchema, ...infer Right extends TSchema[]] ? TInferIntersect<ModuleProperties, Right, Result & TInfer<ModuleProperties, Left>> : Result);
22
24
  type ReadonlyOptionalPropertyKeys<Properties extends TProperties> = {
23
- [K in keyof Properties]: Properties[K] extends TReadonly<TSchema> ? (Properties[K] extends TOptional<Properties[K]> ? K : never) : never;
25
+ [Key in keyof Properties]: Properties[Key] extends TReadonly<TSchema> ? (Properties[Key] extends TOptional<Properties[Key]> ? Key : never) : never;
24
26
  }[keyof Properties];
25
27
  type ReadonlyPropertyKeys<Source extends TProperties> = {
26
- [K in keyof Source]: Source[K] extends TReadonly<TSchema> ? (Source[K] extends TOptional<Source[K]> ? never : K) : never;
28
+ [Key in keyof Source]: Source[Key] extends TReadonly<TSchema> ? (Source[Key] extends TOptional<Source[Key]> ? never : Key) : never;
27
29
  }[keyof Source];
28
30
  type OptionalPropertyKeys<Source extends TProperties> = {
29
- [K in keyof Source]: Source[K] extends TOptional<TSchema> ? (Source[K] extends TReadonly<Source[K]> ? never : K) : never;
31
+ [Key in keyof Source]: Source[Key] extends TOptional<TSchema> ? (Source[Key] extends TReadonly<Source[Key]> ? never : Key) : never;
30
32
  }[keyof Source];
31
33
  type RequiredPropertyKeys<Source extends TProperties> = keyof Omit<Source, ReadonlyOptionalPropertyKeys<Source> | ReadonlyPropertyKeys<Source> | OptionalPropertyKeys<Source>>;
32
34
  type InferPropertiesWithModifiers<Properties extends TProperties, Source extends Record<keyof any, unknown>> = Evaluate<(Readonly<Partial<Pick<Source, ReadonlyOptionalPropertyKeys<Properties>>>> & Readonly<Pick<Source, ReadonlyPropertyKeys<Properties>>> & Partial<Pick<Source, OptionalPropertyKeys<Properties>>> & Required<Pick<Source, RequiredPropertyKeys<Properties>>>)>;
@@ -35,9 +37,10 @@ type InferProperties<ModuleProperties extends TProperties, Properties extends TP
35
37
  }>;
36
38
  type TInferObject<ModuleProperties extends TProperties, Properties extends TProperties> = (InferProperties<ModuleProperties, Properties>);
37
39
  type TInferTuple<ModuleProperties extends TProperties, Types extends TSchema[], Result extends unknown[] = []> = (Types extends [infer L extends TSchema, ...infer R extends TSchema[]] ? TInferTuple<ModuleProperties, R, [...Result, TInfer<ModuleProperties, L>]> : Result);
40
+ type TInferRecord<ModuleProperties extends TProperties, Key extends TSchema, Type extends TSchema, InferredKey extends PropertyKey = TInfer<ModuleProperties, Key> extends infer Key extends PropertyKey ? Key : never, InferedType extends unknown = TInfer<ModuleProperties, Type>> = Record<InferredKey, InferedType>;
38
41
  type TInferRef<ModuleProperties extends TProperties, Ref extends string> = (Ref extends keyof ModuleProperties ? TInfer<ModuleProperties, ModuleProperties[Ref]> : unknown);
39
42
  type TInferUnion<ModuleProperties extends TProperties, Types extends TSchema[], Result extends unknown = never> = (Types extends [infer L extends TSchema, ...infer R extends TSchema[]] ? TInferUnion<ModuleProperties, R, Result | TInfer<ModuleProperties, L>> : Result);
40
- type TInfer<ModuleProperties extends TProperties, Type extends TSchema> = (Type extends TArray<infer Type extends TSchema> ? TInferArray<ModuleProperties, Type> : Type extends TAsyncIterator<infer Type extends TSchema> ? TInferAsyncIterator<ModuleProperties, Type> : Type extends TConstructor<infer Parameters extends TSchema[], infer InstanceType extends TSchema> ? TInferConstructor<ModuleProperties, Parameters, InstanceType> : Type extends TFunction<infer Parameters extends TSchema[], infer ReturnType extends TSchema> ? TInferFunction<ModuleProperties, Parameters, ReturnType> : Type extends TIntersect<infer Types extends TSchema[]> ? TInferIntersect<ModuleProperties, Types> : Type extends TIterator<infer Type extends TSchema> ? TInferIterator<ModuleProperties, Type> : Type extends TObject<infer Properties extends TProperties> ? TInferObject<ModuleProperties, Properties> : Type extends TRef<infer Ref extends string> ? TInferRef<ModuleProperties, Ref> : Type extends TTuple<infer Types extends TSchema[]> ? TInferTuple<ModuleProperties, Types> : Type extends TUnion<infer Types extends TSchema[]> ? TInferUnion<ModuleProperties, Types> : Static<Type>);
43
+ type TInfer<ModuleProperties extends TProperties, Type extends TSchema> = (Type extends TArray<infer Type extends TSchema> ? TInferArray<ModuleProperties, Type> : Type extends TAsyncIterator<infer Type extends TSchema> ? TInferAsyncIterator<ModuleProperties, Type> : Type extends TConstructor<infer Parameters extends TSchema[], infer InstanceType extends TSchema> ? TInferConstructor<ModuleProperties, Parameters, InstanceType> : Type extends TFunction<infer Parameters extends TSchema[], infer ReturnType extends TSchema> ? TInferFunction<ModuleProperties, Parameters, ReturnType> : Type extends TIntersect<infer Types extends TSchema[]> ? TInferIntersect<ModuleProperties, Types> : Type extends TIterator<infer Type extends TSchema> ? TInferIterator<ModuleProperties, Type> : Type extends TObject<infer Properties extends TProperties> ? TInferObject<ModuleProperties, Properties> : Type extends TRecord<infer Key extends TSchema, infer Type extends TSchema> ? TInferRecord<ModuleProperties, Key, Type> : Type extends TRef<infer Ref extends string> ? TInferRef<ModuleProperties, Ref> : Type extends TTuple<infer Types extends TSchema[]> ? TInferTuple<ModuleProperties, Types> : Type extends TEnum<infer _ extends TEnumRecord> ? Static<Type> : Type extends TUnion<infer Types extends TSchema[]> ? TInferUnion<ModuleProperties, Types> : Static<Type>);
41
44
  /** Inference Path for Imports. This type is used to compute TImport `static` */
42
45
  export type TInferFromModuleKey<ModuleProperties extends TProperties, Key extends PropertyKey> = (Key extends keyof ModuleProperties ? TInfer<ModuleProperties, ModuleProperties[Key]> : never);
43
46
  export {};
@@ -21,7 +21,8 @@ class TModule {
21
21
  }
22
22
  /** `[Json]` Imports a Type by Key. */
23
23
  Import(key, options) {
24
- return (0, index_1.CreateType)({ [index_2.Kind]: 'Import', $defs: this.$defs, $ref: key }, options);
24
+ const $defs = { ...this.$defs, [key]: (0, index_1.CreateType)(this.$defs[key], options) };
25
+ return (0, index_1.CreateType)({ [index_2.Kind]: 'Import', $defs, $ref: key });
25
26
  }
26
27
  // prettier-ignore
27
28
  WithIdentifiers($defs) {
@@ -1,58 +1,58 @@
1
+ import { Kind } from '../symbols/index';
1
2
  import type { TSchema } from '../schema/index';
2
3
  import type { Static } from '../static/index';
3
4
  import type { Evaluate, Ensure, Assert } from '../helpers/index';
4
5
  import { type TAny } from '../any/index';
5
6
  import { type TComputed } from '../computed/index';
6
- import { type TObject, type TProperties, type TAdditionalProperties, type ObjectOptions } from '../object/index';
7
+ import { type TEnumRecord, type TEnum } from '../enum/index';
8
+ import { type TInteger } from '../integer/index';
7
9
  import { type TLiteral, type TLiteralValue } from '../literal/index';
8
10
  import { type TNever } from '../never/index';
9
- import { type TUnion } from '../union/index';
10
- import { type TRegExp } from '../regexp/index';
11
- import { type TString } from '../string/index';
12
- import { type TInteger } from '../integer/index';
13
11
  import { type TNumber } from '../number/index';
14
- import { type TEnum } from '../enum/index';
12
+ import { type TObject, type TProperties, type TAdditionalProperties, type ObjectOptions } from '../object/index';
15
13
  import { type TRef } from '../ref/index';
14
+ import { type TRegExp } from '../regexp/index';
15
+ import { type TString } from '../string/index';
16
+ import { type TUnion } from '../union/index';
16
17
  import { TIsTemplateLiteralFinite, type TTemplateLiteral } from '../template-literal/index';
17
- import { Kind } from '../symbols/index';
18
- type TFromTemplateLiteralKeyInfinite<K extends TTemplateLiteral, T extends TSchema> = Ensure<TRecord<K, T>>;
19
- type TFromTemplateLiteralKeyFinite<K extends TTemplateLiteral, T extends TSchema, I extends string = Static<K>> = (Ensure<TObject<Evaluate<{
20
- [_ in I]: T;
18
+ type TFromTemplateLiteralKeyInfinite<Key extends TTemplateLiteral, Type extends TSchema> = Ensure<TRecord<Key, Type>>;
19
+ type TFromTemplateLiteralKeyFinite<Key extends TTemplateLiteral, Type extends TSchema, I extends string = Static<Key>> = (Ensure<TObject<Evaluate<{
20
+ [_ in I]: Type;
21
21
  }>>>);
22
- type TFromTemplateLiteralKey<K extends TTemplateLiteral, T extends TSchema> = TIsTemplateLiteralFinite<K> extends false ? TFromTemplateLiteralKeyInfinite<K, T> : TFromTemplateLiteralKeyFinite<K, T>;
23
- type TFromEnumKey<K extends Record<string, string | number>, T extends TSchema> = Ensure<TObject<{
24
- [_ in K[keyof K]]: T;
22
+ type TFromTemplateLiteralKey<Key extends TTemplateLiteral, Type extends TSchema> = TIsTemplateLiteralFinite<Key> extends false ? TFromTemplateLiteralKeyInfinite<Key, Type> : TFromTemplateLiteralKeyFinite<Key, Type>;
23
+ type TFromEnumKey<Key extends Record<string, string | number>, Type extends TSchema> = Ensure<TObject<{
24
+ [_ in Key[keyof Key]]: Type;
25
25
  }>>;
26
- type TFromUnionKeyLiteralString<K extends TLiteral<string>, T extends TSchema> = {
27
- [_ in K['const']]: T;
26
+ type TFromUnionKeyLiteralString<Key extends TLiteral<string>, Type extends TSchema> = {
27
+ [_ in Key['const']]: Type;
28
28
  };
29
- type TFromUnionKeyLiteralNumber<K extends TLiteral<number>, T extends TSchema> = {
30
- [_ in K['const']]: T;
29
+ type TFromUnionKeyLiteralNumber<Key extends TLiteral<number>, Type extends TSchema> = {
30
+ [_ in Key['const']]: Type;
31
31
  };
32
- type TFromUnionKeyRest<K extends TSchema[], T extends TSchema> = K extends [infer L extends TSchema, ...infer R extends TSchema[]] ? (L extends TUnion<infer S> ? TFromUnionKeyRest<S, T> & TFromUnionKeyRest<R, T> : L extends TLiteral<string> ? TFromUnionKeyLiteralString<L, T> & TFromUnionKeyRest<R, T> : L extends TLiteral<number> ? TFromUnionKeyLiteralNumber<L, T> & TFromUnionKeyRest<R, T> : {}) : {};
33
- type TFromUnionKey<K extends TSchema[], T extends TSchema, P extends TProperties = TFromUnionKeyRest<K, T>> = (Ensure<TObject<Evaluate<P>>>);
34
- type TFromLiteralKey<K extends TLiteralValue, T extends TSchema> = (Ensure<TObject<{
35
- [_ in Assert<K, PropertyKey>]: T;
32
+ type TFromUnionKeyRest<Keys extends TSchema[], Type extends TSchema> = Keys extends [infer Left extends TSchema, ...infer Right extends TSchema[]] ? (Left extends TUnion<infer Types extends TSchema[]> ? TFromUnionKeyRest<Types, Type> & TFromUnionKeyRest<Right, Type> : Left extends TLiteral<string> ? TFromUnionKeyLiteralString<Left, Type> & TFromUnionKeyRest<Right, Type> : Left extends TLiteral<number> ? TFromUnionKeyLiteralNumber<Left, Type> & TFromUnionKeyRest<Right, Type> : {}) : {};
33
+ type TFromUnionKey<Key extends TSchema[], Type extends TSchema, P extends TProperties = TFromUnionKeyRest<Key, Type>> = (Ensure<TObject<Evaluate<P>>>);
34
+ type TFromLiteralKey<Key extends TLiteralValue, Type extends TSchema> = (Ensure<TObject<{
35
+ [_ in Assert<Key, PropertyKey>]: Type;
36
36
  }>>);
37
- type TFromRegExpKey<_ extends TRegExp, T extends TSchema> = (Ensure<TRecord<TRegExp, T>>);
38
- type TFromStringKey<_ extends TString, T extends TSchema> = (Ensure<TRecord<TString, T>>);
39
- type TFromAnyKey<_ extends TAny, T extends TSchema> = (Ensure<TRecord<TAny, T>>);
40
- type TFromNeverKey<_ extends TNever, T extends TSchema> = (Ensure<TRecord<TNever, T>>);
41
- type TFromIntegerKey<_ extends TSchema, T extends TSchema> = (Ensure<TRecord<TNumber, T>>);
42
- type TFromNumberKey<_ extends TSchema, T extends TSchema> = (Ensure<TRecord<TNumber, T>>);
43
- type RecordStatic<K extends TSchema, T extends TSchema, P extends unknown[]> = (Evaluate<{
44
- [_ in Assert<Static<K>, PropertyKey>]: Static<T, P>;
37
+ type TFromRegExpKey<_Key extends TRegExp, Type extends TSchema> = (Ensure<TRecord<TRegExp, Type>>);
38
+ type TFromStringKey<_Key extends TString, Type extends TSchema> = (Ensure<TRecord<TString, Type>>);
39
+ type TFromAnyKey<_Key extends TAny, Type extends TSchema> = (Ensure<TRecord<TAny, Type>>);
40
+ type TFromNeverKey<_Key extends TNever, Type extends TSchema> = (Ensure<TRecord<TNever, Type>>);
41
+ type TFromIntegerKey<_Key extends TSchema, Type extends TSchema> = (Ensure<TRecord<TNumber, Type>>);
42
+ type TFromNumberKey<_Key extends TSchema, Type extends TSchema> = (Ensure<TRecord<TNumber, Type>>);
43
+ type RecordStatic<Key extends TSchema, Type extends TSchema, P extends unknown[]> = (Evaluate<{
44
+ [_ in Assert<Static<Key>, PropertyKey>]: Static<Type, P>;
45
45
  }>);
46
- export interface TRecord<K extends TSchema = TSchema, T extends TSchema = TSchema> extends TSchema {
46
+ export interface TRecord<Key extends TSchema = TSchema, Type extends TSchema = TSchema> extends TSchema {
47
47
  [Kind]: 'Record';
48
- static: RecordStatic<K, T, this['params']>;
48
+ static: RecordStatic<Key, Type, this['params']>;
49
49
  type: 'object';
50
50
  patternProperties: {
51
- [pattern: string]: T;
51
+ [pattern: string]: Type;
52
52
  };
53
53
  additionalProperties: TAdditionalProperties;
54
54
  }
55
- export type TRecordOrObject<Key extends TSchema, Type extends TSchema> = Type extends TRef ? TComputed<'Record', [Key, Type]> : Key extends TRef ? TComputed<'Record', [Key, Type]> : Key extends TTemplateLiteral ? TFromTemplateLiteralKey<Key, Type> : Key extends TEnum<infer S> ? TFromEnumKey<S, Type> : Key extends TUnion<infer S> ? TFromUnionKey<S, Type> : Key extends TLiteral<infer S> ? TFromLiteralKey<S, Type> : Key extends TInteger ? TFromIntegerKey<Key, Type> : Key extends TNumber ? TFromNumberKey<Key, Type> : Key extends TRegExp ? TFromRegExpKey<Key, Type> : Key extends TString ? TFromStringKey<Key, Type> : Key extends TAny ? TFromAnyKey<Key, Type> : Key extends TNever ? TFromNeverKey<Key, Type> : TNever;
55
+ export type TRecordOrObject<Key extends TSchema, Type extends TSchema> = (Type extends TComputed<infer Target extends string, infer Parameters extends TSchema[]> ? TComputed<'Record', [Key, TComputed<Target, Parameters>]> : Key extends TComputed<infer Target extends string, infer Parameters extends TSchema[]> ? TComputed<'Record', [TComputed<Target, Parameters>, Type]> : Key extends TRef<infer Ref extends string> ? TComputed<'Record', [TRef<Ref>, Type]> : Key extends TTemplateLiteral ? TFromTemplateLiteralKey<Key, Type> : Key extends TEnum<infer Enum extends TEnumRecord> ? TFromEnumKey<Enum, Type> : Key extends TUnion<infer Types extends TSchema[]> ? TFromUnionKey<Types, Type> : Key extends TLiteral<infer Value extends TLiteralValue> ? TFromLiteralKey<Value, Type> : Key extends TInteger ? TFromIntegerKey<Key, Type> : Key extends TNumber ? TFromNumberKey<Key, Type> : Key extends TRegExp ? TFromRegExpKey<Key, Type> : Key extends TString ? TFromStringKey<Key, Type> : Key extends TAny ? TFromAnyKey<Key, Type> : Key extends TNever ? TFromNeverKey<Key, Type> : Key);
56
56
  /** `[Json]` Creates a Record type */
57
57
  export declare function Record<Key extends TSchema, Type extends TSchema>(key: Key, type: Type, options?: ObjectOptions): TRecordOrObject<Key, Type>;
58
58
  export {};
@@ -3,14 +3,15 @@
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
4
  exports.Record = Record;
5
5
  const type_1 = require("../create/type");
6
- const index_1 = require("../computed/index");
7
- const index_2 = require("../object/index");
6
+ const index_1 = require("../symbols/index");
7
+ const index_2 = require("../computed/index");
8
8
  const index_3 = require("../never/index");
9
- const index_4 = require("../union/index");
10
- const index_5 = require("../template-literal/index");
11
- const index_6 = require("../patterns/index");
12
- const index_7 = require("../indexed/index");
13
- const index_8 = require("../symbols/index");
9
+ const index_4 = require("../object/index");
10
+ const index_5 = require("../ref/index");
11
+ const index_6 = require("../union/index");
12
+ const index_7 = require("../template-literal/index");
13
+ const index_8 = require("../patterns/index");
14
+ const index_9 = require("../indexed/index");
14
15
  // ------------------------------------------------------------------
15
16
  // ValueGuard
16
17
  // ------------------------------------------------------------------
@@ -24,60 +25,56 @@ const kind_1 = require("../guard/kind");
24
25
  // ------------------------------------------------------------------
25
26
  // prettier-ignore
26
27
  function RecordCreateFromPattern(pattern, T, options) {
27
- return (0, type_1.CreateType)({
28
- [index_8.Kind]: 'Record',
29
- type: 'object',
30
- patternProperties: { [pattern]: T }
31
- }, options);
28
+ return (0, type_1.CreateType)({ [index_1.Kind]: 'Record', type: 'object', patternProperties: { [pattern]: T } }, options);
32
29
  }
33
30
  // ------------------------------------------------------------------
34
31
  // RecordCreateFromKeys
35
32
  // ------------------------------------------------------------------
36
33
  // prettier-ignore
37
34
  function RecordCreateFromKeys(K, T, options) {
38
- const Acc = {};
35
+ const result = {};
39
36
  for (const K2 of K)
40
- Acc[K2] = T;
41
- return (0, index_2.Object)(Acc, { ...options, [index_8.Hint]: 'Record' });
37
+ result[K2] = T;
38
+ return (0, index_4.Object)(result, { ...options, [index_1.Hint]: 'Record' });
42
39
  }
43
40
  // prettier-ignore
44
41
  function FromTemplateLiteralKey(K, T, options) {
45
- return ((0, index_5.IsTemplateLiteralFinite)(K)
46
- ? RecordCreateFromKeys((0, index_7.IndexPropertyKeys)(K), T, options)
42
+ return ((0, index_7.IsTemplateLiteralFinite)(K)
43
+ ? RecordCreateFromKeys((0, index_9.IndexPropertyKeys)(K), T, options)
47
44
  : RecordCreateFromPattern(K.pattern, T, options));
48
45
  }
49
46
  // prettier-ignore
50
- function FromUnionKey(K, T, options) {
51
- return RecordCreateFromKeys((0, index_7.IndexPropertyKeys)((0, index_4.Union)(K)), T, options);
47
+ function FromUnionKey(key, type, options) {
48
+ return RecordCreateFromKeys((0, index_9.IndexPropertyKeys)((0, index_6.Union)(key)), type, options);
52
49
  }
53
50
  // prettier-ignore
54
- function FromLiteralKey(K, T, options) {
55
- return RecordCreateFromKeys([K.toString()], T, options);
51
+ function FromLiteralKey(key, type, options) {
52
+ return RecordCreateFromKeys([key.toString()], type, options);
56
53
  }
57
54
  // prettier-ignore
58
- function FromRegExpKey(K, T, options) {
59
- return RecordCreateFromPattern(K.source, T, options);
55
+ function FromRegExpKey(key, type, options) {
56
+ return RecordCreateFromPattern(key.source, type, options);
60
57
  }
61
58
  // prettier-ignore
62
- function FromStringKey(K, T, options) {
63
- const pattern = (0, value_1.IsUndefined)(K.pattern) ? index_6.PatternStringExact : K.pattern;
64
- return RecordCreateFromPattern(pattern, T, options);
59
+ function FromStringKey(key, type, options) {
60
+ const pattern = (0, value_1.IsUndefined)(key.pattern) ? index_8.PatternStringExact : key.pattern;
61
+ return RecordCreateFromPattern(pattern, type, options);
65
62
  }
66
63
  // prettier-ignore
67
- function FromAnyKey(K, T, options) {
68
- return RecordCreateFromPattern(index_6.PatternStringExact, T, options);
64
+ function FromAnyKey(_, type, options) {
65
+ return RecordCreateFromPattern(index_8.PatternStringExact, type, options);
69
66
  }
70
67
  // prettier-ignore
71
- function FromNeverKey(K, T, options) {
72
- return RecordCreateFromPattern(index_6.PatternNeverExact, T, options);
68
+ function FromNeverKey(_key, type, options) {
69
+ return RecordCreateFromPattern(index_8.PatternNeverExact, type, options);
73
70
  }
74
71
  // prettier-ignore
75
- function FromIntegerKey(_, T, options) {
76
- return RecordCreateFromPattern(index_6.PatternNumberExact, T, options);
72
+ function FromIntegerKey(_key, type, options) {
73
+ return RecordCreateFromPattern(index_8.PatternNumberExact, type, options);
77
74
  }
78
75
  // prettier-ignore
79
- function FromNumberKey(_, T, options) {
80
- return RecordCreateFromPattern(index_6.PatternNumberExact, T, options);
76
+ function FromNumberKey(_, type, options) {
77
+ return RecordCreateFromPattern(index_8.PatternNumberExact, type, options);
81
78
  }
82
79
  // ------------------------------------------------------------------
83
80
  // TRecordOrObject
@@ -85,16 +82,17 @@ function FromNumberKey(_, T, options) {
85
82
  /** `[Json]` Creates a Record type */
86
83
  function Record(key, type, options = {}) {
87
84
  // prettier-ignore
88
- return ((0, kind_1.IsRef)(type) ? (0, index_1.Computed)('Record', [key, type]) :
89
- (0, kind_1.IsRef)(key) ? (0, index_1.Computed)('Record', [key, type]) :
90
- (0, kind_1.IsUnion)(key) ? FromUnionKey(key.anyOf, type, options) :
91
- (0, kind_1.IsTemplateLiteral)(key) ? FromTemplateLiteralKey(key, type, options) :
92
- (0, kind_1.IsLiteral)(key) ? FromLiteralKey(key.const, type, options) :
93
- (0, kind_1.IsInteger)(key) ? FromIntegerKey(key, type, options) :
94
- (0, kind_1.IsNumber)(key) ? FromNumberKey(key, type, options) :
95
- (0, kind_1.IsRegExp)(key) ? FromRegExpKey(key, type, options) :
96
- (0, kind_1.IsString)(key) ? FromStringKey(key, type, options) :
97
- (0, kind_1.IsAny)(key) ? FromAnyKey(key, type, options) :
98
- (0, kind_1.IsNever)(key) ? FromNeverKey(key, type, options) :
99
- (0, index_3.Never)(options));
85
+ return ((0, kind_1.IsComputed)(type) ? (0, index_2.Computed)('Record', [key, (0, index_2.Computed)(type.target, type.parameters)], options) :
86
+ (0, kind_1.IsComputed)(key) ? (0, index_2.Computed)('Record', [(0, index_2.Computed)(type.target, type.parameters), type], options) :
87
+ (0, kind_1.IsRef)(key) ? (0, index_2.Computed)('Record', [(0, index_5.Ref)(key.$ref), type]) :
88
+ (0, kind_1.IsUnion)(key) ? FromUnionKey(key.anyOf, type, options) :
89
+ (0, kind_1.IsTemplateLiteral)(key) ? FromTemplateLiteralKey(key, type, options) :
90
+ (0, kind_1.IsLiteral)(key) ? FromLiteralKey(key.const, type, options) :
91
+ (0, kind_1.IsInteger)(key) ? FromIntegerKey(key, type, options) :
92
+ (0, kind_1.IsNumber)(key) ? FromNumberKey(key, type, options) :
93
+ (0, kind_1.IsRegExp)(key) ? FromRegExpKey(key, type, options) :
94
+ (0, kind_1.IsString)(key) ? FromStringKey(key, type, options) :
95
+ (0, kind_1.IsAny)(key) ? FromAnyKey(key, type, options) :
96
+ (0, kind_1.IsNever)(key) ? FromNeverKey(key, type, options) :
97
+ (0, index_3.Never)(options));
100
98
  }
@@ -48,7 +48,7 @@ export declare const Module: Runtime.Module<{
48
48
  Iterator: Runtime.ITuple<Types.TIterator<Types.TSchema>>;
49
49
  Awaited: Runtime.ITuple<Types.TSchema>;
50
50
  Array: Runtime.ITuple<Types.TArray<Types.TSchema>>;
51
- Record: Runtime.ITuple<Types.TNever>;
51
+ Record: Runtime.ITuple<Types.TSchema>;
52
52
  Promise: Runtime.ITuple<Types.TPromise<Types.TSchema>>;
53
53
  ConstructorParameters: Runtime.ITuple<Types.TTuple<Types.TSchema[]>>;
54
54
  FunctionParameters: Runtime.ITuple<Types.TTuple<Types.TSchema[]>>;
@@ -6,6 +6,7 @@ import { type TAsyncIterator } from '../async-iterator/index.mjs';
6
6
  import { TComputed } from '../computed/index.mjs';
7
7
  import { type TConstructor } from '../constructor/index.mjs';
8
8
  import { type TIndex } from '../indexed/index.mjs';
9
+ import { TEnum, TEnumRecord } from '../enum/index.mjs';
9
10
  import { type TFunction } from '../function/index.mjs';
10
11
  import { type TIntersect, type TIntersectEvaluated } from '../intersect/index.mjs';
11
12
  import { type TIterator } from '../iterator/index.mjs';
@@ -43,7 +44,7 @@ type TFromArray<ModuleProperties extends TProperties, Type extends TSchema> = (E
43
44
  type TFromAsyncIterator<ModuleProperties extends TProperties, Type extends TSchema> = (TAsyncIterator<TFromType<ModuleProperties, Type>>);
44
45
  type TFromIterator<ModuleProperties extends TProperties, Type extends TSchema> = (TIterator<TFromType<ModuleProperties, Type>>);
45
46
  type TFromRest<ModuleProperties extends TProperties, Types extends TSchema[], Result extends TSchema[] = []> = (Types extends [infer Left extends TSchema, ...infer Right extends TSchema[]] ? TFromRest<ModuleProperties, Right, [...Result, TFromType<ModuleProperties, Left>]> : Result);
46
- export type TFromType<ModuleProperties extends TProperties, Type extends TSchema> = (Type extends TComputed<infer T extends string, infer S extends TSchema[]> ? TFromComputed<ModuleProperties, T, S> : Type extends TObject<infer S extends TProperties> ? TFromObject<ModuleProperties, S> : Type extends TConstructor<infer S extends TSchema[], infer R extends TSchema> ? TFromConstructor<ModuleProperties, S, R> : Type extends TFunction<infer S extends TSchema[], infer R extends TSchema> ? TFromFunction<ModuleProperties, S, R> : Type extends TTuple<infer S extends TSchema[]> ? TFromTuple<ModuleProperties, S> : Type extends TIntersect<infer S extends TSchema[]> ? TFromIntersect<ModuleProperties, S> : Type extends TUnion<infer S extends TSchema[]> ? TFromUnion<ModuleProperties, S> : Type extends TArray<infer S extends TSchema> ? TFromArray<ModuleProperties, S> : Type extends TAsyncIterator<infer S extends TSchema> ? TFromAsyncIterator<ModuleProperties, S> : Type extends TIterator<infer S extends TSchema> ? TFromIterator<ModuleProperties, S> : Type);
47
+ export type TFromType<ModuleProperties extends TProperties, Type extends TSchema> = (Type extends TArray<infer Type extends TSchema> ? TFromArray<ModuleProperties, Type> : Type extends TAsyncIterator<infer Type extends TSchema> ? TFromAsyncIterator<ModuleProperties, Type> : Type extends TComputed<infer Target extends string, infer Parameters extends TSchema[]> ? TFromComputed<ModuleProperties, Target, Parameters> : Type extends TConstructor<infer Parameters extends TSchema[], infer InstanceType extends TSchema> ? TFromConstructor<ModuleProperties, Parameters, InstanceType> : Type extends TFunction<infer Parameters extends TSchema[], infer ReturnType extends TSchema> ? TFromFunction<ModuleProperties, Parameters, ReturnType> : Type extends TIntersect<infer Types extends TSchema[]> ? TFromIntersect<ModuleProperties, Types> : Type extends TIterator<infer Type extends TSchema> ? TFromIterator<ModuleProperties, Type> : Type extends TObject<infer Properties extends TProperties> ? TFromObject<ModuleProperties, Properties> : Type extends TTuple<infer Types extends TSchema[]> ? TFromTuple<ModuleProperties, Types> : Type extends TEnum<infer _ extends TEnumRecord> ? Type : Type extends TUnion<infer Types extends TSchema[]> ? TFromUnion<ModuleProperties, Types> : Type);
47
48
  export declare function FromType<ModuleProperties extends TProperties, Type extends TSchema>(moduleProperties: ModuleProperties, type: Type): TFromType<ModuleProperties, Type>;
48
49
  export type TComputeType<ModuleProperties extends TProperties, Key extends PropertyKey> = (Key extends keyof ModuleProperties ? TFromType<ModuleProperties, ModuleProperties[Key]> : TNever);
49
50
  export declare function ComputeType<ModuleProperties extends TProperties, Key extends PropertyKey>(moduleProperties: ModuleProperties, key: Key): TComputeType<ModuleProperties, Key>;
@@ -118,20 +118,19 @@ function FromRest(moduleProperties, types) {
118
118
  }
119
119
  // prettier-ignore
120
120
  export function FromType(moduleProperties, type) {
121
- return (
122
- // Note: The 'as never' is required due to excessive resolution of TIndex. In fact TIndex, TPick, TOmit and
123
- // all need re-implementation to remove the PropertyKey[] selector. Reimplementation of these types should
124
- // be a priority as there is a potential for the current inference to break on TS compiler changes.
125
- KindGuard.IsComputed(type) ? CreateType(FromComputed(moduleProperties, type.target, type.parameters)) :
126
- KindGuard.IsObject(type) ? CreateType(FromObject(moduleProperties, type.properties), type) :
127
- KindGuard.IsConstructor(type) ? CreateType(FromConstructor(moduleProperties, type.parameters, type.returns), type) :
128
- KindGuard.IsFunction(type) ? CreateType(FromFunction(moduleProperties, type.parameters, type.returns), type) :
129
- KindGuard.IsTuple(type) ? CreateType(FromTuple(moduleProperties, type.items || []), type) :
121
+ return (KindGuard.IsArray(type) ? CreateType(FromArray(moduleProperties, type.items), type) :
122
+ KindGuard.IsAsyncIterator(type) ? CreateType(FromAsyncIterator(moduleProperties, type.items), type) :
123
+ // Note: The 'as never' is required due to excessive resolution of TIndex. In fact TIndex, TPick, TOmit and
124
+ // all need re-implementation to remove the PropertyKey[] selector. Reimplementation of these types should
125
+ // be a priority as there is a potential for the current inference to break on TS compiler changes.
126
+ KindGuard.IsComputed(type) ? CreateType(FromComputed(moduleProperties, type.target, type.parameters)) :
127
+ KindGuard.IsConstructor(type) ? CreateType(FromConstructor(moduleProperties, type.parameters, type.returns), type) :
128
+ KindGuard.IsFunction(type) ? CreateType(FromFunction(moduleProperties, type.parameters, type.returns), type) :
130
129
  KindGuard.IsIntersect(type) ? CreateType(FromIntersect(moduleProperties, type.allOf), type) :
131
- KindGuard.IsUnion(type) ? CreateType(FromUnion(moduleProperties, type.anyOf), type) :
132
- KindGuard.IsArray(type) ? CreateType(FromArray(moduleProperties, type.items), type) :
133
- KindGuard.IsAsyncIterator(type) ? CreateType(FromAsyncIterator(moduleProperties, type.items), type) :
134
- KindGuard.IsIterator(type) ? CreateType(FromIterator(moduleProperties, type.items), type) :
130
+ KindGuard.IsIterator(type) ? CreateType(FromIterator(moduleProperties, type.items), type) :
131
+ KindGuard.IsObject(type) ? CreateType(FromObject(moduleProperties, type.properties), type) :
132
+ KindGuard.IsTuple(type) ? CreateType(FromTuple(moduleProperties, type.items || []), type) :
133
+ KindGuard.IsUnion(type) ? CreateType(FromUnion(moduleProperties, type.anyOf), type) :
135
134
  type);
136
135
  }
137
136
  // prettier-ignore
@@ -3,11 +3,13 @@ import { TSchema } from '../schema/index.mjs';
3
3
  import { TArray } from '../array/index.mjs';
4
4
  import { TAsyncIterator } from '../async-iterator/index.mjs';
5
5
  import { TConstructor } from '../constructor/index.mjs';
6
+ import { TEnum, TEnumRecord } from '../enum/index.mjs';
6
7
  import { TFunction } from '../function/index.mjs';
7
8
  import { TIntersect } from '../intersect/index.mjs';
8
9
  import { TIterator } from '../iterator/index.mjs';
9
10
  import { TObject, TProperties } from '../object/index.mjs';
10
11
  import { TOptional } from '../optional/index.mjs';
12
+ import { TRecord } from '../record/index.mjs';
11
13
  import { TReadonly } from '../readonly/index.mjs';
12
14
  import { TRef } from '../ref/index.mjs';
13
15
  import { TTuple } from '../tuple/index.mjs';
@@ -18,15 +20,15 @@ type TInferAsyncIterator<ModuleProperties extends TProperties, Type extends TSch
18
20
  type TInferConstructor<ModuleProperties extends TProperties, Parameters extends TSchema[], InstanceType extends TSchema> = Ensure<new (...args: TInferTuple<ModuleProperties, Parameters>) => TInfer<ModuleProperties, InstanceType>>;
19
21
  type TInferFunction<ModuleProperties extends TProperties, Parameters extends TSchema[], ReturnType extends TSchema> = Ensure<(...args: TInferTuple<ModuleProperties, Parameters>) => TInfer<ModuleProperties, ReturnType>>;
20
22
  type TInferIterator<ModuleProperties extends TProperties, Type extends TSchema> = (Ensure<IterableIterator<TInfer<ModuleProperties, Type>>>);
21
- type TInferIntersect<ModuleProperties extends TProperties, Types extends TSchema[], Result extends unknown = unknown> = (Types extends [infer L extends TSchema, ...infer R extends TSchema[]] ? TInferIntersect<ModuleProperties, R, Result & TInfer<ModuleProperties, L>> : Result);
23
+ type TInferIntersect<ModuleProperties extends TProperties, Types extends TSchema[], Result extends unknown = unknown> = (Types extends [infer Left extends TSchema, ...infer Right extends TSchema[]] ? TInferIntersect<ModuleProperties, Right, Result & TInfer<ModuleProperties, Left>> : Result);
22
24
  type ReadonlyOptionalPropertyKeys<Properties extends TProperties> = {
23
- [K in keyof Properties]: Properties[K] extends TReadonly<TSchema> ? (Properties[K] extends TOptional<Properties[K]> ? K : never) : never;
25
+ [Key in keyof Properties]: Properties[Key] extends TReadonly<TSchema> ? (Properties[Key] extends TOptional<Properties[Key]> ? Key : never) : never;
24
26
  }[keyof Properties];
25
27
  type ReadonlyPropertyKeys<Source extends TProperties> = {
26
- [K in keyof Source]: Source[K] extends TReadonly<TSchema> ? (Source[K] extends TOptional<Source[K]> ? never : K) : never;
28
+ [Key in keyof Source]: Source[Key] extends TReadonly<TSchema> ? (Source[Key] extends TOptional<Source[Key]> ? never : Key) : never;
27
29
  }[keyof Source];
28
30
  type OptionalPropertyKeys<Source extends TProperties> = {
29
- [K in keyof Source]: Source[K] extends TOptional<TSchema> ? (Source[K] extends TReadonly<Source[K]> ? never : K) : never;
31
+ [Key in keyof Source]: Source[Key] extends TOptional<TSchema> ? (Source[Key] extends TReadonly<Source[Key]> ? never : Key) : never;
30
32
  }[keyof Source];
31
33
  type RequiredPropertyKeys<Source extends TProperties> = keyof Omit<Source, ReadonlyOptionalPropertyKeys<Source> | ReadonlyPropertyKeys<Source> | OptionalPropertyKeys<Source>>;
32
34
  type InferPropertiesWithModifiers<Properties extends TProperties, Source extends Record<keyof any, unknown>> = Evaluate<(Readonly<Partial<Pick<Source, ReadonlyOptionalPropertyKeys<Properties>>>> & Readonly<Pick<Source, ReadonlyPropertyKeys<Properties>>> & Partial<Pick<Source, OptionalPropertyKeys<Properties>>> & Required<Pick<Source, RequiredPropertyKeys<Properties>>>)>;
@@ -35,9 +37,10 @@ type InferProperties<ModuleProperties extends TProperties, Properties extends TP
35
37
  }>;
36
38
  type TInferObject<ModuleProperties extends TProperties, Properties extends TProperties> = (InferProperties<ModuleProperties, Properties>);
37
39
  type TInferTuple<ModuleProperties extends TProperties, Types extends TSchema[], Result extends unknown[] = []> = (Types extends [infer L extends TSchema, ...infer R extends TSchema[]] ? TInferTuple<ModuleProperties, R, [...Result, TInfer<ModuleProperties, L>]> : Result);
40
+ type TInferRecord<ModuleProperties extends TProperties, Key extends TSchema, Type extends TSchema, InferredKey extends PropertyKey = TInfer<ModuleProperties, Key> extends infer Key extends PropertyKey ? Key : never, InferedType extends unknown = TInfer<ModuleProperties, Type>> = Record<InferredKey, InferedType>;
38
41
  type TInferRef<ModuleProperties extends TProperties, Ref extends string> = (Ref extends keyof ModuleProperties ? TInfer<ModuleProperties, ModuleProperties[Ref]> : unknown);
39
42
  type TInferUnion<ModuleProperties extends TProperties, Types extends TSchema[], Result extends unknown = never> = (Types extends [infer L extends TSchema, ...infer R extends TSchema[]] ? TInferUnion<ModuleProperties, R, Result | TInfer<ModuleProperties, L>> : Result);
40
- type TInfer<ModuleProperties extends TProperties, Type extends TSchema> = (Type extends TArray<infer Type extends TSchema> ? TInferArray<ModuleProperties, Type> : Type extends TAsyncIterator<infer Type extends TSchema> ? TInferAsyncIterator<ModuleProperties, Type> : Type extends TConstructor<infer Parameters extends TSchema[], infer InstanceType extends TSchema> ? TInferConstructor<ModuleProperties, Parameters, InstanceType> : Type extends TFunction<infer Parameters extends TSchema[], infer ReturnType extends TSchema> ? TInferFunction<ModuleProperties, Parameters, ReturnType> : Type extends TIntersect<infer Types extends TSchema[]> ? TInferIntersect<ModuleProperties, Types> : Type extends TIterator<infer Type extends TSchema> ? TInferIterator<ModuleProperties, Type> : Type extends TObject<infer Properties extends TProperties> ? TInferObject<ModuleProperties, Properties> : Type extends TRef<infer Ref extends string> ? TInferRef<ModuleProperties, Ref> : Type extends TTuple<infer Types extends TSchema[]> ? TInferTuple<ModuleProperties, Types> : Type extends TUnion<infer Types extends TSchema[]> ? TInferUnion<ModuleProperties, Types> : Static<Type>);
43
+ type TInfer<ModuleProperties extends TProperties, Type extends TSchema> = (Type extends TArray<infer Type extends TSchema> ? TInferArray<ModuleProperties, Type> : Type extends TAsyncIterator<infer Type extends TSchema> ? TInferAsyncIterator<ModuleProperties, Type> : Type extends TConstructor<infer Parameters extends TSchema[], infer InstanceType extends TSchema> ? TInferConstructor<ModuleProperties, Parameters, InstanceType> : Type extends TFunction<infer Parameters extends TSchema[], infer ReturnType extends TSchema> ? TInferFunction<ModuleProperties, Parameters, ReturnType> : Type extends TIntersect<infer Types extends TSchema[]> ? TInferIntersect<ModuleProperties, Types> : Type extends TIterator<infer Type extends TSchema> ? TInferIterator<ModuleProperties, Type> : Type extends TObject<infer Properties extends TProperties> ? TInferObject<ModuleProperties, Properties> : Type extends TRecord<infer Key extends TSchema, infer Type extends TSchema> ? TInferRecord<ModuleProperties, Key, Type> : Type extends TRef<infer Ref extends string> ? TInferRef<ModuleProperties, Ref> : Type extends TTuple<infer Types extends TSchema[]> ? TInferTuple<ModuleProperties, Types> : Type extends TEnum<infer _ extends TEnumRecord> ? Static<Type> : Type extends TUnion<infer Types extends TSchema[]> ? TInferUnion<ModuleProperties, Types> : Static<Type>);
41
44
  /** Inference Path for Imports. This type is used to compute TImport `static` */
42
45
  export type TInferFromModuleKey<ModuleProperties extends TProperties, Key extends PropertyKey> = (Key extends keyof ModuleProperties ? TInfer<ModuleProperties, ModuleProperties[Key]> : never);
43
46
  export {};
@@ -16,7 +16,8 @@ export class TModule {
16
16
  }
17
17
  /** `[Json]` Imports a Type by Key. */
18
18
  Import(key, options) {
19
- return CreateType({ [Kind]: 'Import', $defs: this.$defs, $ref: key }, options);
19
+ const $defs = { ...this.$defs, [key]: CreateType(this.$defs[key], options) };
20
+ return CreateType({ [Kind]: 'Import', $defs, $ref: key });
20
21
  }
21
22
  // prettier-ignore
22
23
  WithIdentifiers($defs) {
@@ -1,58 +1,58 @@
1
+ import { Kind } from '../symbols/index.mjs';
1
2
  import type { TSchema } from '../schema/index.mjs';
2
3
  import type { Static } from '../static/index.mjs';
3
4
  import type { Evaluate, Ensure, Assert } from '../helpers/index.mjs';
4
5
  import { type TAny } from '../any/index.mjs';
5
6
  import { type TComputed } from '../computed/index.mjs';
6
- import { type TObject, type TProperties, type TAdditionalProperties, type ObjectOptions } from '../object/index.mjs';
7
+ import { type TEnumRecord, type TEnum } from '../enum/index.mjs';
8
+ import { type TInteger } from '../integer/index.mjs';
7
9
  import { type TLiteral, type TLiteralValue } from '../literal/index.mjs';
8
10
  import { type TNever } from '../never/index.mjs';
9
- import { type TUnion } from '../union/index.mjs';
10
- import { type TRegExp } from '../regexp/index.mjs';
11
- import { type TString } from '../string/index.mjs';
12
- import { type TInteger } from '../integer/index.mjs';
13
11
  import { type TNumber } from '../number/index.mjs';
14
- import { type TEnum } from '../enum/index.mjs';
12
+ import { type TObject, type TProperties, type TAdditionalProperties, type ObjectOptions } from '../object/index.mjs';
15
13
  import { type TRef } from '../ref/index.mjs';
14
+ import { type TRegExp } from '../regexp/index.mjs';
15
+ import { type TString } from '../string/index.mjs';
16
+ import { type TUnion } from '../union/index.mjs';
16
17
  import { TIsTemplateLiteralFinite, type TTemplateLiteral } from '../template-literal/index.mjs';
17
- import { Kind } from '../symbols/index.mjs';
18
- type TFromTemplateLiteralKeyInfinite<K extends TTemplateLiteral, T extends TSchema> = Ensure<TRecord<K, T>>;
19
- type TFromTemplateLiteralKeyFinite<K extends TTemplateLiteral, T extends TSchema, I extends string = Static<K>> = (Ensure<TObject<Evaluate<{
20
- [_ in I]: T;
18
+ type TFromTemplateLiteralKeyInfinite<Key extends TTemplateLiteral, Type extends TSchema> = Ensure<TRecord<Key, Type>>;
19
+ type TFromTemplateLiteralKeyFinite<Key extends TTemplateLiteral, Type extends TSchema, I extends string = Static<Key>> = (Ensure<TObject<Evaluate<{
20
+ [_ in I]: Type;
21
21
  }>>>);
22
- type TFromTemplateLiteralKey<K extends TTemplateLiteral, T extends TSchema> = TIsTemplateLiteralFinite<K> extends false ? TFromTemplateLiteralKeyInfinite<K, T> : TFromTemplateLiteralKeyFinite<K, T>;
23
- type TFromEnumKey<K extends Record<string, string | number>, T extends TSchema> = Ensure<TObject<{
24
- [_ in K[keyof K]]: T;
22
+ type TFromTemplateLiteralKey<Key extends TTemplateLiteral, Type extends TSchema> = TIsTemplateLiteralFinite<Key> extends false ? TFromTemplateLiteralKeyInfinite<Key, Type> : TFromTemplateLiteralKeyFinite<Key, Type>;
23
+ type TFromEnumKey<Key extends Record<string, string | number>, Type extends TSchema> = Ensure<TObject<{
24
+ [_ in Key[keyof Key]]: Type;
25
25
  }>>;
26
- type TFromUnionKeyLiteralString<K extends TLiteral<string>, T extends TSchema> = {
27
- [_ in K['const']]: T;
26
+ type TFromUnionKeyLiteralString<Key extends TLiteral<string>, Type extends TSchema> = {
27
+ [_ in Key['const']]: Type;
28
28
  };
29
- type TFromUnionKeyLiteralNumber<K extends TLiteral<number>, T extends TSchema> = {
30
- [_ in K['const']]: T;
29
+ type TFromUnionKeyLiteralNumber<Key extends TLiteral<number>, Type extends TSchema> = {
30
+ [_ in Key['const']]: Type;
31
31
  };
32
- type TFromUnionKeyRest<K extends TSchema[], T extends TSchema> = K extends [infer L extends TSchema, ...infer R extends TSchema[]] ? (L extends TUnion<infer S> ? TFromUnionKeyRest<S, T> & TFromUnionKeyRest<R, T> : L extends TLiteral<string> ? TFromUnionKeyLiteralString<L, T> & TFromUnionKeyRest<R, T> : L extends TLiteral<number> ? TFromUnionKeyLiteralNumber<L, T> & TFromUnionKeyRest<R, T> : {}) : {};
33
- type TFromUnionKey<K extends TSchema[], T extends TSchema, P extends TProperties = TFromUnionKeyRest<K, T>> = (Ensure<TObject<Evaluate<P>>>);
34
- type TFromLiteralKey<K extends TLiteralValue, T extends TSchema> = (Ensure<TObject<{
35
- [_ in Assert<K, PropertyKey>]: T;
32
+ type TFromUnionKeyRest<Keys extends TSchema[], Type extends TSchema> = Keys extends [infer Left extends TSchema, ...infer Right extends TSchema[]] ? (Left extends TUnion<infer Types extends TSchema[]> ? TFromUnionKeyRest<Types, Type> & TFromUnionKeyRest<Right, Type> : Left extends TLiteral<string> ? TFromUnionKeyLiteralString<Left, Type> & TFromUnionKeyRest<Right, Type> : Left extends TLiteral<number> ? TFromUnionKeyLiteralNumber<Left, Type> & TFromUnionKeyRest<Right, Type> : {}) : {};
33
+ type TFromUnionKey<Key extends TSchema[], Type extends TSchema, P extends TProperties = TFromUnionKeyRest<Key, Type>> = (Ensure<TObject<Evaluate<P>>>);
34
+ type TFromLiteralKey<Key extends TLiteralValue, Type extends TSchema> = (Ensure<TObject<{
35
+ [_ in Assert<Key, PropertyKey>]: Type;
36
36
  }>>);
37
- type TFromRegExpKey<_ extends TRegExp, T extends TSchema> = (Ensure<TRecord<TRegExp, T>>);
38
- type TFromStringKey<_ extends TString, T extends TSchema> = (Ensure<TRecord<TString, T>>);
39
- type TFromAnyKey<_ extends TAny, T extends TSchema> = (Ensure<TRecord<TAny, T>>);
40
- type TFromNeverKey<_ extends TNever, T extends TSchema> = (Ensure<TRecord<TNever, T>>);
41
- type TFromIntegerKey<_ extends TSchema, T extends TSchema> = (Ensure<TRecord<TNumber, T>>);
42
- type TFromNumberKey<_ extends TSchema, T extends TSchema> = (Ensure<TRecord<TNumber, T>>);
43
- type RecordStatic<K extends TSchema, T extends TSchema, P extends unknown[]> = (Evaluate<{
44
- [_ in Assert<Static<K>, PropertyKey>]: Static<T, P>;
37
+ type TFromRegExpKey<_Key extends TRegExp, Type extends TSchema> = (Ensure<TRecord<TRegExp, Type>>);
38
+ type TFromStringKey<_Key extends TString, Type extends TSchema> = (Ensure<TRecord<TString, Type>>);
39
+ type TFromAnyKey<_Key extends TAny, Type extends TSchema> = (Ensure<TRecord<TAny, Type>>);
40
+ type TFromNeverKey<_Key extends TNever, Type extends TSchema> = (Ensure<TRecord<TNever, Type>>);
41
+ type TFromIntegerKey<_Key extends TSchema, Type extends TSchema> = (Ensure<TRecord<TNumber, Type>>);
42
+ type TFromNumberKey<_Key extends TSchema, Type extends TSchema> = (Ensure<TRecord<TNumber, Type>>);
43
+ type RecordStatic<Key extends TSchema, Type extends TSchema, P extends unknown[]> = (Evaluate<{
44
+ [_ in Assert<Static<Key>, PropertyKey>]: Static<Type, P>;
45
45
  }>);
46
- export interface TRecord<K extends TSchema = TSchema, T extends TSchema = TSchema> extends TSchema {
46
+ export interface TRecord<Key extends TSchema = TSchema, Type extends TSchema = TSchema> extends TSchema {
47
47
  [Kind]: 'Record';
48
- static: RecordStatic<K, T, this['params']>;
48
+ static: RecordStatic<Key, Type, this['params']>;
49
49
  type: 'object';
50
50
  patternProperties: {
51
- [pattern: string]: T;
51
+ [pattern: string]: Type;
52
52
  };
53
53
  additionalProperties: TAdditionalProperties;
54
54
  }
55
- export type TRecordOrObject<Key extends TSchema, Type extends TSchema> = Type extends TRef ? TComputed<'Record', [Key, Type]> : Key extends TRef ? TComputed<'Record', [Key, Type]> : Key extends TTemplateLiteral ? TFromTemplateLiteralKey<Key, Type> : Key extends TEnum<infer S> ? TFromEnumKey<S, Type> : Key extends TUnion<infer S> ? TFromUnionKey<S, Type> : Key extends TLiteral<infer S> ? TFromLiteralKey<S, Type> : Key extends TInteger ? TFromIntegerKey<Key, Type> : Key extends TNumber ? TFromNumberKey<Key, Type> : Key extends TRegExp ? TFromRegExpKey<Key, Type> : Key extends TString ? TFromStringKey<Key, Type> : Key extends TAny ? TFromAnyKey<Key, Type> : Key extends TNever ? TFromNeverKey<Key, Type> : TNever;
55
+ export type TRecordOrObject<Key extends TSchema, Type extends TSchema> = (Type extends TComputed<infer Target extends string, infer Parameters extends TSchema[]> ? TComputed<'Record', [Key, TComputed<Target, Parameters>]> : Key extends TComputed<infer Target extends string, infer Parameters extends TSchema[]> ? TComputed<'Record', [TComputed<Target, Parameters>, Type]> : Key extends TRef<infer Ref extends string> ? TComputed<'Record', [TRef<Ref>, Type]> : Key extends TTemplateLiteral ? TFromTemplateLiteralKey<Key, Type> : Key extends TEnum<infer Enum extends TEnumRecord> ? TFromEnumKey<Enum, Type> : Key extends TUnion<infer Types extends TSchema[]> ? TFromUnionKey<Types, Type> : Key extends TLiteral<infer Value extends TLiteralValue> ? TFromLiteralKey<Value, Type> : Key extends TInteger ? TFromIntegerKey<Key, Type> : Key extends TNumber ? TFromNumberKey<Key, Type> : Key extends TRegExp ? TFromRegExpKey<Key, Type> : Key extends TString ? TFromStringKey<Key, Type> : Key extends TAny ? TFromAnyKey<Key, Type> : Key extends TNever ? TFromNeverKey<Key, Type> : Key);
56
56
  /** `[Json]` Creates a Record type */
57
57
  export declare function Record<Key extends TSchema, Type extends TSchema>(key: Key, type: Type, options?: ObjectOptions): TRecordOrObject<Key, Type>;
58
58
  export {};
@@ -1,12 +1,13 @@
1
1
  import { CreateType } from '../create/type.mjs';
2
+ import { Kind, Hint } from '../symbols/index.mjs';
2
3
  import { Computed } from '../computed/index.mjs';
3
- import { Object } from '../object/index.mjs';
4
4
  import { Never } from '../never/index.mjs';
5
+ import { Object } from '../object/index.mjs';
6
+ import { Ref } from '../ref/index.mjs';
5
7
  import { Union } from '../union/index.mjs';
6
8
  import { IsTemplateLiteralFinite } from '../template-literal/index.mjs';
7
9
  import { PatternStringExact, PatternNumberExact, PatternNeverExact } from '../patterns/index.mjs';
8
10
  import { IndexPropertyKeys } from '../indexed/index.mjs';
9
- import { Kind, Hint } from '../symbols/index.mjs';
10
11
  // ------------------------------------------------------------------
11
12
  // ValueGuard
12
13
  // ------------------------------------------------------------------
@@ -14,27 +15,23 @@ import { IsUndefined } from '../guard/value.mjs';
14
15
  // ------------------------------------------------------------------
15
16
  // TypeGuard
16
17
  // ------------------------------------------------------------------
17
- import { IsInteger, IsLiteral, IsAny, IsNever, IsNumber, IsString, IsRegExp, IsTemplateLiteral, IsUnion, IsRef } from '../guard/kind.mjs';
18
+ import { IsInteger, IsLiteral, IsAny, IsNever, IsNumber, IsString, IsRegExp, IsTemplateLiteral, IsUnion, IsRef, IsComputed } from '../guard/kind.mjs';
18
19
  // ------------------------------------------------------------------
19
20
  // RecordCreateFromPattern
20
21
  // ------------------------------------------------------------------
21
22
  // prettier-ignore
22
23
  function RecordCreateFromPattern(pattern, T, options) {
23
- return CreateType({
24
- [Kind]: 'Record',
25
- type: 'object',
26
- patternProperties: { [pattern]: T }
27
- }, options);
24
+ return CreateType({ [Kind]: 'Record', type: 'object', patternProperties: { [pattern]: T } }, options);
28
25
  }
29
26
  // ------------------------------------------------------------------
30
27
  // RecordCreateFromKeys
31
28
  // ------------------------------------------------------------------
32
29
  // prettier-ignore
33
30
  function RecordCreateFromKeys(K, T, options) {
34
- const Acc = {};
31
+ const result = {};
35
32
  for (const K2 of K)
36
- Acc[K2] = T;
37
- return Object(Acc, { ...options, [Hint]: 'Record' });
33
+ result[K2] = T;
34
+ return Object(result, { ...options, [Hint]: 'Record' });
38
35
  }
39
36
  // prettier-ignore
40
37
  function FromTemplateLiteralKey(K, T, options) {
@@ -43,37 +40,37 @@ function FromTemplateLiteralKey(K, T, options) {
43
40
  : RecordCreateFromPattern(K.pattern, T, options));
44
41
  }
45
42
  // prettier-ignore
46
- function FromUnionKey(K, T, options) {
47
- return RecordCreateFromKeys(IndexPropertyKeys(Union(K)), T, options);
43
+ function FromUnionKey(key, type, options) {
44
+ return RecordCreateFromKeys(IndexPropertyKeys(Union(key)), type, options);
48
45
  }
49
46
  // prettier-ignore
50
- function FromLiteralKey(K, T, options) {
51
- return RecordCreateFromKeys([K.toString()], T, options);
47
+ function FromLiteralKey(key, type, options) {
48
+ return RecordCreateFromKeys([key.toString()], type, options);
52
49
  }
53
50
  // prettier-ignore
54
- function FromRegExpKey(K, T, options) {
55
- return RecordCreateFromPattern(K.source, T, options);
51
+ function FromRegExpKey(key, type, options) {
52
+ return RecordCreateFromPattern(key.source, type, options);
56
53
  }
57
54
  // prettier-ignore
58
- function FromStringKey(K, T, options) {
59
- const pattern = IsUndefined(K.pattern) ? PatternStringExact : K.pattern;
60
- return RecordCreateFromPattern(pattern, T, options);
55
+ function FromStringKey(key, type, options) {
56
+ const pattern = IsUndefined(key.pattern) ? PatternStringExact : key.pattern;
57
+ return RecordCreateFromPattern(pattern, type, options);
61
58
  }
62
59
  // prettier-ignore
63
- function FromAnyKey(K, T, options) {
64
- return RecordCreateFromPattern(PatternStringExact, T, options);
60
+ function FromAnyKey(_, type, options) {
61
+ return RecordCreateFromPattern(PatternStringExact, type, options);
65
62
  }
66
63
  // prettier-ignore
67
- function FromNeverKey(K, T, options) {
68
- return RecordCreateFromPattern(PatternNeverExact, T, options);
64
+ function FromNeverKey(_key, type, options) {
65
+ return RecordCreateFromPattern(PatternNeverExact, type, options);
69
66
  }
70
67
  // prettier-ignore
71
- function FromIntegerKey(_, T, options) {
72
- return RecordCreateFromPattern(PatternNumberExact, T, options);
68
+ function FromIntegerKey(_key, type, options) {
69
+ return RecordCreateFromPattern(PatternNumberExact, type, options);
73
70
  }
74
71
  // prettier-ignore
75
- function FromNumberKey(_, T, options) {
76
- return RecordCreateFromPattern(PatternNumberExact, T, options);
72
+ function FromNumberKey(_, type, options) {
73
+ return RecordCreateFromPattern(PatternNumberExact, type, options);
77
74
  }
78
75
  // ------------------------------------------------------------------
79
76
  // TRecordOrObject
@@ -81,16 +78,17 @@ function FromNumberKey(_, T, options) {
81
78
  /** `[Json]` Creates a Record type */
82
79
  export function Record(key, type, options = {}) {
83
80
  // prettier-ignore
84
- return (IsRef(type) ? Computed('Record', [key, type]) :
85
- IsRef(key) ? Computed('Record', [key, type]) :
86
- IsUnion(key) ? FromUnionKey(key.anyOf, type, options) :
87
- IsTemplateLiteral(key) ? FromTemplateLiteralKey(key, type, options) :
88
- IsLiteral(key) ? FromLiteralKey(key.const, type, options) :
89
- IsInteger(key) ? FromIntegerKey(key, type, options) :
90
- IsNumber(key) ? FromNumberKey(key, type, options) :
91
- IsRegExp(key) ? FromRegExpKey(key, type, options) :
92
- IsString(key) ? FromStringKey(key, type, options) :
93
- IsAny(key) ? FromAnyKey(key, type, options) :
94
- IsNever(key) ? FromNeverKey(key, type, options) :
95
- Never(options));
81
+ return (IsComputed(type) ? Computed('Record', [key, Computed(type.target, type.parameters)], options) :
82
+ IsComputed(key) ? Computed('Record', [Computed(type.target, type.parameters), type], options) :
83
+ IsRef(key) ? Computed('Record', [Ref(key.$ref), type]) :
84
+ IsUnion(key) ? FromUnionKey(key.anyOf, type, options) :
85
+ IsTemplateLiteral(key) ? FromTemplateLiteralKey(key, type, options) :
86
+ IsLiteral(key) ? FromLiteralKey(key.const, type, options) :
87
+ IsInteger(key) ? FromIntegerKey(key, type, options) :
88
+ IsNumber(key) ? FromNumberKey(key, type, options) :
89
+ IsRegExp(key) ? FromRegExpKey(key, type, options) :
90
+ IsString(key) ? FromStringKey(key, type, options) :
91
+ IsAny(key) ? FromAnyKey(key, type, options) :
92
+ IsNever(key) ? FromNeverKey(key, type, options) :
93
+ Never(options));
96
94
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sinclair/typebox",
3
- "version": "0.34.3",
3
+ "version": "0.34.5",
4
4
  "description": "Json Schema Type Builder with Static Type Resolution for TypeScript",
5
5
  "keywords": [
6
6
  "typescript",
package/readme.md CHANGED
@@ -774,39 +774,47 @@ type T = Static<typeof T> // type T = string | null
774
774
 
775
775
  ### Module Types
776
776
 
777
- TypeBox Modules are containers for related types. They function as namespaces and enable internal types to reference each other via string references. Modules support both singular and mutually recursive types. They provide a mechanism to create circular types irrespective of the order in which types are defined.
777
+ TypeBox Modules are containers for related types. They provide a referential namespace, enabling types to reference one another via string identifiers. Modules support both singular and mutually recursive referencing within the context of a module, as well as referential computed types (such as Partial + Ref). All Module types must be imported before use. TypeBox represents an imported type with the `$defs` Json Schema keyword.
778
778
 
779
- ```typescript
779
+ #### Usage
780
+
781
+ The following creates a Module with User and PartialUser types. Note that the PartialUser type is specified as a Partial + Ref to the User type. It is not possible to perform a Partial operation directly on a reference, so TypeBox will return a TComputed type that defers the Partial operation until all types are resolved. The TComputed type is evaluated when calling Import on the Module.
780
782
 
781
- // The following creates a Module of circular recursive Types.
783
+ ```typescript
784
+ // Module with PartialUser and User types
782
785
 
783
786
  const Module = Type.Module({
784
- A: Type.Object({
785
- b: Type.Ref('B') // Ref B:
786
- }),
787
- B: Type.Object({
788
- c: Type.Ref('C') // Ref C:
789
- }),
790
- C: Type.Object({
791
- a: Type.Ref('A') // Ref A:
792
- }),
787
+
788
+ PartialUser: Type.Partial(Type.Ref('User')), // TComputed<'Partial', [TRef<'User'>]>
789
+
790
+ User: Type.Object({ // TObject<{
791
+ id: Type.String(), // user: TString,
792
+ name: Type.String(), // name: TString,
793
+ email: Type.String() // email: TString
794
+ }), // }>
795
+
793
796
  })
794
797
 
795
- // Module Types must be imported before use.
798
+ // Types must be imported before use.
796
799
 
797
- const A = Module.Import('A') // const A: TImport<{...}, 'A'>
800
+ const User = Module.Import('User') // const User: TImport<{...}, 'User'>
798
801
 
799
- type A = Static<typeof A> // type A = {
800
- // b: {
801
- // c: {
802
- // a: {
803
- // b: ...
804
- // }
805
- // }
806
- // }
802
+ type User = Static<typeof User> // type User = {
803
+ // id: string,
804
+ // name: string,
805
+ // email: string
806
+ // }
807
+
808
+ const PartialUser = Module.Import('PartialUser') // const PartialUser: TImport<{...}, 'PartialUser'>
809
+
810
+ type PartialUser = Static<typeof PartialUser> // type PartialUser = {
811
+ // id?: string,
812
+ // name?: string,
813
+ // email?: string
807
814
  // }
808
815
  ```
809
816
 
817
+
810
818
  <a name='types-template-literal'></a>
811
819
 
812
820
  ### Template Literal Types
@@ -1025,7 +1033,7 @@ if(TypeGuard.IsString(T)) {
1025
1033
 
1026
1034
  ## Syntax Types
1027
1035
 
1028
- TypeBox provides support for Syntax Types, enabling it to parse TypeScript syntax directly into TypeBox types. Syntax Types serve as a DSL frontend for TypeBox's type builder and are useful for converting existing TypeScript type definitions into Json Schema schematics.
1036
+ TypeBox provides support for parsing TypeScript syntax directly into TypeBox Json Schema schematics. Syntax Types offer a string based DSL frontend to TypeBox's Type Builder system and can be useful for converting existing TypeScript type definitions into Json Schema schematics without reimplementation via the Type Builder.
1029
1037
 
1030
1038
  Syntax Types are provided via optional import.
1031
1039
 
@@ -1106,14 +1114,14 @@ type PartialUser = Static<typeof PartialUser> // type PartialUser = {
1106
1114
 
1107
1115
  ### Context
1108
1116
 
1109
- The Parse function accepts an initial Context argument, allowing external types to be passed into the parser.
1117
+ The Parse function takes an optional leading Context object that contains external types. This Context allows the syntax to reference these external types using the property identifiers provided by the Context. The following passes the external type `T` to Parse.
1110
1118
 
1111
1119
  ```typescript
1112
- const T = Type.Object({ // could be written as: Parse(`{
1113
- x: Type.Number(), // x: number,
1114
- y: Type.Number(), // y: number,
1115
- z: Type.Number() // z: number
1116
- }) // }`)
1120
+ const T = Type.Object({ // const T: TObject<{
1121
+ x: Type.Number(), // x: TNumber,
1122
+ y: Type.Number(), // y: TNumber,
1123
+ z: Type.Number() // z: TNumber
1124
+ }) // }>
1117
1125
 
1118
1126
  const A = Parse({ T }, 'Partial<T>') // const A: TObject<{
1119
1127
  // x: TOptional<TNumber>,
@@ -1140,7 +1148,7 @@ const C = Parse({ T }, 'T & { w: number }') // const C: TIntersect<[TObj
1140
1148
 
1141
1149
  ### Static
1142
1150
 
1143
- Syntax Types provide two Static types for inferring TypeScript syntax from strings.
1151
+ Syntax Types provide two Static types for inferring TypeScript types and TypeBox schematics from strings.
1144
1152
 
1145
1153
  ```typescript
1146
1154
  import { StaticParseAsSchema, StaticParseAsType } from '@sinclair/typebox/syntax'
@@ -1190,7 +1198,7 @@ const B = Parse(`{
1190
1198
  }`)
1191
1199
  ```
1192
1200
 
1193
- In cases where Syntax Types busts through TypeScript instantiation limits, TypeBox offers a fallback ParseOnly function which will Parse the types at runtime, but not infer the type. This function can also be used for parsing non-constant strings.
1201
+ In cases where Syntax Types exceed TypeScript's instantiation limits, TypeBox offers a fallback ParseOnly function, which will only parse but not infer. This function can also be used to parse types where the syntax is statically unknown to TypeScript (for example, when loading types from disk).
1194
1202
 
1195
1203
  ```typescript
1196
1204
  import { ParseOnly } from '@sinclair/typebox/syntax'