@sinclair/typebox 0.34.2 → 0.34.4
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.
- package/build/cjs/type/module/compute.d.ts +2 -1
- package/build/cjs/type/module/compute.js +12 -13
- package/build/cjs/type/module/infer.d.ts +2 -1
- package/build/cjs/value/default/default.js +8 -0
- package/build/esm/type/module/compute.d.mts +2 -1
- package/build/esm/type/module/compute.mjs +12 -13
- package/build/esm/type/module/infer.d.mts +2 -1
- package/build/esm/value/default/default.mjs +8 -0
- package/package.json +1 -1
- package/readme.md +39 -31
|
@@ -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
|
|
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
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
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.
|
|
138
|
-
KindGuard.
|
|
139
|
-
KindGuard.
|
|
140
|
-
KindGuard.
|
|
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,6 +3,7 @@ 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';
|
|
@@ -37,7 +38,7 @@ type TInferObject<ModuleProperties extends TProperties, Properties extends TProp
|
|
|
37
38
|
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);
|
|
38
39
|
type TInferRef<ModuleProperties extends TProperties, Ref extends string> = (Ref extends keyof ModuleProperties ? TInfer<ModuleProperties, ModuleProperties[Ref]> : unknown);
|
|
39
40
|
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>);
|
|
41
|
+
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 TEnum<infer _ extends TEnumRecord> ? Static<Type> : Type extends TUnion<infer Types extends TSchema[]> ? TInferUnion<ModuleProperties, Types> : Static<Type>);
|
|
41
42
|
/** Inference Path for Imports. This type is used to compute TImport `static` */
|
|
42
43
|
export type TInferFromModuleKey<ModuleProperties extends TProperties, Key extends PropertyKey> = (Key extends keyof ModuleProperties ? TInfer<ModuleProperties, ModuleProperties[Key]> : never);
|
|
43
44
|
export {};
|
|
@@ -32,6 +32,14 @@ function HasDefaultProperty(schema) {
|
|
|
32
32
|
// Types
|
|
33
33
|
// ------------------------------------------------------------------
|
|
34
34
|
function FromArray(schema, references, value) {
|
|
35
|
+
// if the value is an array, we attempt to initialize it's elements
|
|
36
|
+
if ((0, index_5.IsArray)(value)) {
|
|
37
|
+
for (let i = 0; i < value.length; i++) {
|
|
38
|
+
value[i] = Visit(schema.items, references, value[i]);
|
|
39
|
+
}
|
|
40
|
+
return value;
|
|
41
|
+
}
|
|
42
|
+
// ... otherwise use default initialization
|
|
35
43
|
const defaulted = ValueOrDefault(schema, value);
|
|
36
44
|
if (!(0, index_5.IsArray)(defaulted))
|
|
37
45
|
return defaulted;
|
|
@@ -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
|
|
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
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
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.
|
|
132
|
-
KindGuard.
|
|
133
|
-
KindGuard.
|
|
134
|
-
KindGuard.
|
|
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,6 +3,7 @@ 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';
|
|
@@ -37,7 +38,7 @@ type TInferObject<ModuleProperties extends TProperties, Properties extends TProp
|
|
|
37
38
|
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);
|
|
38
39
|
type TInferRef<ModuleProperties extends TProperties, Ref extends string> = (Ref extends keyof ModuleProperties ? TInfer<ModuleProperties, ModuleProperties[Ref]> : unknown);
|
|
39
40
|
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>);
|
|
41
|
+
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 TEnum<infer _ extends TEnumRecord> ? Static<Type> : Type extends TUnion<infer Types extends TSchema[]> ? TInferUnion<ModuleProperties, Types> : Static<Type>);
|
|
41
42
|
/** Inference Path for Imports. This type is used to compute TImport `static` */
|
|
42
43
|
export type TInferFromModuleKey<ModuleProperties extends TProperties, Key extends PropertyKey> = (Key extends keyof ModuleProperties ? TInfer<ModuleProperties, ModuleProperties[Key]> : never);
|
|
43
44
|
export {};
|
|
@@ -28,6 +28,14 @@ function HasDefaultProperty(schema) {
|
|
|
28
28
|
// Types
|
|
29
29
|
// ------------------------------------------------------------------
|
|
30
30
|
function FromArray(schema, references, value) {
|
|
31
|
+
// if the value is an array, we attempt to initialize it's elements
|
|
32
|
+
if (IsArray(value)) {
|
|
33
|
+
for (let i = 0; i < value.length; i++) {
|
|
34
|
+
value[i] = Visit(schema.items, references, value[i]);
|
|
35
|
+
}
|
|
36
|
+
return value;
|
|
37
|
+
}
|
|
38
|
+
// ... otherwise use default initialization
|
|
31
39
|
const defaulted = ValueOrDefault(schema, value);
|
|
32
40
|
if (!IsArray(defaulted))
|
|
33
41
|
return defaulted;
|
package/package.json
CHANGED
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
|
|
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
|
-
|
|
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
|
-
|
|
783
|
+
```typescript
|
|
784
|
+
// Module with PartialUser and User types
|
|
782
785
|
|
|
783
786
|
const Module = Type.Module({
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
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
|
-
//
|
|
798
|
+
// Types must be imported before use.
|
|
796
799
|
|
|
797
|
-
const
|
|
800
|
+
const User = Module.Import('User') // const User: TImport<{...}, 'User'>
|
|
798
801
|
|
|
799
|
-
type
|
|
800
|
-
//
|
|
801
|
-
//
|
|
802
|
-
//
|
|
803
|
-
//
|
|
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
|
|
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
|
|
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({ //
|
|
1113
|
-
x: Type.Number(), // x:
|
|
1114
|
-
y: Type.Number(), // y:
|
|
1115
|
-
z: Type.Number() // z:
|
|
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
|
|
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
|
|
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'
|