@sinclair/typebox 0.34.21 → 0.34.23
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/index.d.ts +0 -1
- package/build/cjs/index.js +0 -1
- package/build/cjs/syntax/runtime.js +10 -3
- package/build/cjs/syntax/static.d.ts +1 -1
- package/build/cjs/type/instantiate/instantiate.d.ts +43 -18
- package/build/cjs/type/instantiate/instantiate.js +104 -17
- package/build/cjs/type/partial/partial.d.ts +10 -1
- package/build/cjs/type/partial/partial.js +25 -12
- package/build/cjs/type/required/required.d.ts +10 -1
- package/build/cjs/type/required/required.js +21 -8
- package/build/cjs/type/type/type.d.ts +0 -1
- package/build/cjs/type/type/type.js +30 -32
- package/build/esm/index.d.mts +0 -1
- package/build/esm/index.mjs +0 -1
- package/build/esm/syntax/runtime.mjs +10 -3
- package/build/esm/syntax/static.d.mts +1 -1
- package/build/esm/type/instantiate/instantiate.d.mts +43 -18
- package/build/esm/type/instantiate/instantiate.mjs +103 -17
- package/build/esm/type/partial/partial.d.mts +10 -1
- package/build/esm/type/partial/partial.mjs +25 -12
- package/build/esm/type/required/required.d.mts +10 -1
- package/build/esm/type/required/required.mjs +21 -8
- package/build/esm/type/type/type.d.mts +0 -1
- package/build/esm/type/type/type.mjs +0 -1
- package/package.json +1 -1
- package/readme.md +79 -64
- package/build/cjs/type/remap/index.d.ts +0 -1
- package/build/cjs/type/remap/index.js +0 -18
- package/build/cjs/type/remap/remap.d.ts +0 -30
- package/build/cjs/type/remap/remap.js +0 -47
- package/build/esm/type/remap/index.d.mts +0 -1
- package/build/esm/type/remap/index.mjs +0 -1
- package/build/esm/type/remap/remap.d.mts +0 -30
- package/build/esm/type/remap/remap.mjs +0 -43
|
@@ -1,25 +1,50 @@
|
|
|
1
|
-
import type
|
|
2
|
-
import type
|
|
1
|
+
import { type TSchema } from '../schema/index.mjs';
|
|
2
|
+
import { type TArgument } from '../argument/index.mjs';
|
|
3
3
|
import { type TNever } from '../never/index.mjs';
|
|
4
4
|
import { type TReadonlyOptional } from '../readonly-optional/index.mjs';
|
|
5
5
|
import { type TReadonly } from '../readonly/index.mjs';
|
|
6
6
|
import { type TOptional } from '../optional/index.mjs';
|
|
7
|
-
import { type
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
7
|
+
import { type TConstructor } from '../constructor/index.mjs';
|
|
8
|
+
import { type TFunction } from '../function/index.mjs';
|
|
9
|
+
import { type TIntersect } from '../intersect/index.mjs';
|
|
10
|
+
import { type TUnion } from '../union/index.mjs';
|
|
11
|
+
import { type TTuple } from '../tuple/index.mjs';
|
|
12
|
+
import { type TArray } from '../array/index.mjs';
|
|
13
|
+
import { type TAsyncIterator } from '../async-iterator/index.mjs';
|
|
14
|
+
import { type TIterator } from '../iterator/index.mjs';
|
|
15
|
+
import { type TPromise } from '../promise/index.mjs';
|
|
16
|
+
import { type TObject, type TProperties } from '../object/index.mjs';
|
|
17
|
+
import { type TRecordOrObject, type TRecord } from '../record/index.mjs';
|
|
18
|
+
type TFromConstructor<Args extends TSchema[], Parameters extends TSchema[], InstanceType extends TSchema, Result extends TConstructor = TConstructor<TFromTypes<Args, Parameters>, TFromType<Args, InstanceType>>> = Result;
|
|
19
|
+
type TFromFunction<Args extends TSchema[], Parameters extends TSchema[], ReturnType extends TSchema, Result extends TFunction = TFunction<TFromTypes<Args, Parameters>, TFromType<Args, ReturnType>>> = Result;
|
|
20
|
+
type TFromIntersect<Args extends TSchema[], Types extends TSchema[], Result extends TIntersect = TIntersect<TFromTypes<Args, Types>>> = Result;
|
|
21
|
+
type TFromUnion<Args extends TSchema[], Types extends TSchema[], Result extends TUnion = TUnion<TFromTypes<Args, Types>>> = Result;
|
|
22
|
+
type TFromTuple<Args extends TSchema[], Types extends TSchema[], Result extends TTuple = TTuple<TFromTypes<Args, Types>>> = Result;
|
|
23
|
+
type TFromArray<Args extends TSchema[], Type extends TSchema, Result extends TArray = TArray<TFromType<Args, Type>>> = Result;
|
|
24
|
+
type TFromAsyncIterator<Args extends TSchema[], Type extends TSchema, Result extends TAsyncIterator = TAsyncIterator<TFromType<Args, Type>>> = Result;
|
|
25
|
+
type TFromIterator<Args extends TSchema[], Type extends TSchema, Result extends TIterator = TIterator<TFromType<Args, Type>>> = Result;
|
|
26
|
+
type TFromPromise<Args extends TSchema[], Type extends TSchema, Result extends TPromise = TPromise<TFromType<Args, Type>>> = Result;
|
|
27
|
+
type TFromObject<Args extends TSchema[], Properties extends TProperties, Result extends TObject = TObject<TFromProperties<Args, Properties>>> = Result;
|
|
28
|
+
type TFromRecord<Args extends TSchema[], Key extends TSchema, Value extends TSchema, MappedKey extends TSchema = TFromType<Args, Key>, MappedValue extends TSchema = TFromType<Args, Value>, Result extends TSchema = TRecordOrObject<MappedKey, MappedValue>> = Result;
|
|
29
|
+
type TFromArgument<Args extends TSchema[], Index extends number> = (Index extends keyof Args ? Args[Index] : TNever);
|
|
30
|
+
type TFromProperty<Args extends TSchema[], Type extends TSchema, IsReadonly extends boolean = Type extends TReadonly<Type> ? true : false, IsOptional extends boolean = Type extends TOptional<Type> ? true : false, Mapped extends TSchema = TFromType<Args, Type>, Result extends TSchema = ([
|
|
31
|
+
IsReadonly,
|
|
32
|
+
IsOptional
|
|
33
|
+
] extends [true, true] ? TReadonlyOptional<Mapped> : [
|
|
34
|
+
IsReadonly,
|
|
35
|
+
IsOptional
|
|
36
|
+
] extends [true, false] ? TReadonly<Mapped> : [
|
|
37
|
+
IsReadonly,
|
|
38
|
+
IsOptional
|
|
39
|
+
] extends [false, true] ? TOptional<Mapped> : Mapped)> = Result;
|
|
40
|
+
type TFromProperties<Args extends TSchema[], Properties extends TProperties, Result extends TProperties = {
|
|
41
|
+
[Key in keyof Properties]: TFromProperty<Args, Properties[Key]>;
|
|
42
|
+
}> = Result;
|
|
43
|
+
export type TFromTypes<Args extends TSchema[], Types extends TSchema[], Result extends TSchema[] = []> = (Types extends [infer Left extends TSchema, ...infer Right extends TSchema[]] ? TFromTypes<Args, Right, [...Result, TFromType<Args, Left>]> : Result);
|
|
44
|
+
export declare function FromTypes<Args extends TSchema[], Types extends TSchema[]>(args: [...Args], types: [...Types]): TFromTypes<Args, Types>;
|
|
45
|
+
export type TFromType<Args extends TSchema[], Type extends TSchema> = (Type extends TConstructor<infer Parameters extends TSchema[], infer InstanceType extends TSchema> ? TFromConstructor<Args, Parameters, InstanceType> : Type extends TFunction<infer Parameters extends TSchema[], infer ReturnType extends TSchema> ? TFromFunction<Args, Parameters, ReturnType> : Type extends TIntersect<infer Types extends TSchema[]> ? TFromIntersect<Args, Types> : Type extends TUnion<infer Types extends TSchema[]> ? TFromUnion<Args, Types> : Type extends TTuple<infer Types extends TSchema[]> ? TFromTuple<Args, Types> : Type extends TArray<infer Type extends TSchema> ? TFromArray<Args, Type> : Type extends TAsyncIterator<infer Type extends TSchema> ? TFromAsyncIterator<Args, Type> : Type extends TIterator<infer Type extends TSchema> ? TFromIterator<Args, Type> : Type extends TPromise<infer Type extends TSchema> ? TFromPromise<Args, Type> : Type extends TObject<infer Properties extends TProperties> ? TFromObject<Args, Properties> : Type extends TRecord<infer Key extends TSchema, infer Value extends TSchema> ? TFromRecord<Args, Key, Value> : Type extends TArgument<infer Index extends number> ? TFromArgument<Args, Index> : Type);
|
|
21
46
|
/** `[JavaScript]` Instantiates a type with the given parameters */
|
|
22
|
-
export type TInstantiate<Type extends TSchema,
|
|
47
|
+
export type TInstantiate<Type extends TSchema, Args extends TSchema[], Result extends TSchema = TFromType<Args, Type>> = Result;
|
|
23
48
|
/** `[JavaScript]` Instantiates a type with the given parameters */
|
|
24
|
-
export declare function Instantiate<Type extends TSchema,
|
|
49
|
+
export declare function Instantiate<Type extends TSchema, Args extends TSchema[]>(type: Type, args: [...Args]): TInstantiate<Type, Args>;
|
|
25
50
|
export {};
|
|
@@ -1,28 +1,114 @@
|
|
|
1
|
+
import { CloneType } from '../clone/type.mjs';
|
|
1
2
|
import { Never } from '../never/index.mjs';
|
|
2
3
|
import { ReadonlyOptional } from '../readonly-optional/index.mjs';
|
|
3
4
|
import { Readonly } from '../readonly/index.mjs';
|
|
4
5
|
import { Optional } from '../optional/index.mjs';
|
|
5
|
-
import {
|
|
6
|
+
import { Record, RecordKey, RecordValue } from '../record/index.mjs';
|
|
7
|
+
import * as ValueGuard from '../guard/value.mjs';
|
|
6
8
|
import * as KindGuard from '../guard/kind.mjs';
|
|
7
9
|
// prettier-ignore
|
|
8
|
-
function
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
return
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
10
|
+
function FromConstructor(args, type) {
|
|
11
|
+
type.parameters = FromTypes(args, type.parameters);
|
|
12
|
+
type.returns = FromType(args, type.returns);
|
|
13
|
+
return type;
|
|
14
|
+
}
|
|
15
|
+
// prettier-ignore
|
|
16
|
+
function FromFunction(args, type) {
|
|
17
|
+
type.parameters = FromTypes(args, type.parameters);
|
|
18
|
+
type.returns = FromType(args, type.returns);
|
|
19
|
+
return type;
|
|
20
|
+
}
|
|
21
|
+
// prettier-ignore
|
|
22
|
+
function FromIntersect(args, type) {
|
|
23
|
+
type.allOf = FromTypes(args, type.allOf);
|
|
24
|
+
return type;
|
|
25
|
+
}
|
|
26
|
+
// prettier-ignore
|
|
27
|
+
function FromUnion(args, type) {
|
|
28
|
+
type.anyOf = FromTypes(args, type.anyOf);
|
|
29
|
+
return type;
|
|
30
|
+
}
|
|
31
|
+
// prettier-ignore
|
|
32
|
+
function FromTuple(args, type) {
|
|
33
|
+
if (ValueGuard.IsUndefined(type.items))
|
|
34
|
+
return type;
|
|
35
|
+
type.items = FromTypes(args, type.items);
|
|
36
|
+
return type;
|
|
37
|
+
}
|
|
38
|
+
// prettier-ignore
|
|
39
|
+
function FromArray(args, type) {
|
|
40
|
+
type.items = FromType(args, type.items);
|
|
41
|
+
return type;
|
|
42
|
+
}
|
|
43
|
+
// prettier-ignore
|
|
44
|
+
function FromAsyncIterator(args, type) {
|
|
45
|
+
type.items = FromType(args, type.items);
|
|
46
|
+
return type;
|
|
47
|
+
}
|
|
48
|
+
// prettier-ignore
|
|
49
|
+
function FromIterator(args, type) {
|
|
50
|
+
type.items = FromType(args, type.items);
|
|
51
|
+
return type;
|
|
52
|
+
}
|
|
53
|
+
// prettier-ignore
|
|
54
|
+
function FromPromise(args, type) {
|
|
55
|
+
type.item = FromType(args, type.item);
|
|
56
|
+
return type;
|
|
57
|
+
}
|
|
58
|
+
// prettier-ignore
|
|
59
|
+
function FromObject(args, type) {
|
|
60
|
+
type.properties = FromProperties(args, type.properties);
|
|
61
|
+
return type;
|
|
62
|
+
}
|
|
63
|
+
// prettier-ignore
|
|
64
|
+
function FromRecord(args, type) {
|
|
65
|
+
const mappedKey = FromType(args, RecordKey(type));
|
|
66
|
+
const mappedValue = FromType(args, RecordValue(type));
|
|
67
|
+
const result = Record(mappedKey, mappedValue);
|
|
68
|
+
return { ...type, ...result }; // retain options
|
|
69
|
+
}
|
|
70
|
+
// prettier-ignore
|
|
71
|
+
function FromArgument(args, argument) {
|
|
72
|
+
return argument.index in args ? args[argument.index] : Never();
|
|
73
|
+
}
|
|
74
|
+
// prettier-ignore
|
|
75
|
+
function FromProperty(args, type) {
|
|
76
|
+
const isReadonly = KindGuard.IsReadonly(type);
|
|
77
|
+
const isOptional = KindGuard.IsOptional(type);
|
|
78
|
+
const mapped = FromType(args, type);
|
|
79
|
+
return (isReadonly && isOptional ? ReadonlyOptional(mapped) :
|
|
80
|
+
isReadonly && !isOptional ? Readonly(mapped) :
|
|
81
|
+
!isReadonly && isOptional ? Optional(mapped) :
|
|
82
|
+
mapped);
|
|
83
|
+
}
|
|
84
|
+
// prettier-ignore
|
|
85
|
+
function FromProperties(args, properties) {
|
|
86
|
+
return globalThis.Object.getOwnPropertyNames(properties).reduce((result, key) => {
|
|
87
|
+
return { ...result, [key]: FromProperty(args, properties[key]) };
|
|
88
|
+
}, {});
|
|
89
|
+
}
|
|
90
|
+
// prettier-ignore
|
|
91
|
+
export function FromTypes(args, types) {
|
|
92
|
+
return types.map(type => FromType(args, type));
|
|
93
|
+
}
|
|
94
|
+
// prettier-ignore
|
|
95
|
+
function FromType(args, type) {
|
|
96
|
+
return (KindGuard.IsConstructor(type) ? FromConstructor(args, type) :
|
|
97
|
+
KindGuard.IsFunction(type) ? FromFunction(args, type) :
|
|
98
|
+
KindGuard.IsIntersect(type) ? FromIntersect(args, type) :
|
|
99
|
+
KindGuard.IsUnion(type) ? FromUnion(args, type) :
|
|
100
|
+
KindGuard.IsTuple(type) ? FromTuple(args, type) :
|
|
101
|
+
KindGuard.IsArray(type) ? FromArray(args, type) :
|
|
102
|
+
KindGuard.IsAsyncIterator(type) ? FromAsyncIterator(args, type) :
|
|
103
|
+
KindGuard.IsIterator(type) ? FromIterator(args, type) :
|
|
104
|
+
KindGuard.IsPromise(type) ? FromPromise(args, type) :
|
|
105
|
+
KindGuard.IsObject(type) ? FromObject(args, type) :
|
|
106
|
+
KindGuard.IsRecord(type) ? FromRecord(args, type) :
|
|
107
|
+
KindGuard.IsArgument(type) ? FromArgument(args, type) :
|
|
108
|
+
type);
|
|
15
109
|
}
|
|
16
110
|
/** `[JavaScript]` Instantiates a type with the given parameters */
|
|
17
111
|
// prettier-ignore
|
|
18
112
|
export function Instantiate(type, args) {
|
|
19
|
-
return
|
|
20
|
-
return KindGuard.IsArgument(type)
|
|
21
|
-
? type.index in args
|
|
22
|
-
? KindGuard.IsSchema(args[type.index])
|
|
23
|
-
? InstantiateArgument(type, args[type.index])
|
|
24
|
-
: Never()
|
|
25
|
-
: Never()
|
|
26
|
-
: type;
|
|
27
|
-
});
|
|
113
|
+
return FromType(args, CloneType(type));
|
|
28
114
|
}
|
|
@@ -10,6 +10,15 @@ import { type TObject, type TProperties } from '../object/index.mjs';
|
|
|
10
10
|
import { type TIntersect } from '../intersect/index.mjs';
|
|
11
11
|
import { type TUnion } from '../union/index.mjs';
|
|
12
12
|
import { type TRef } from '../ref/index.mjs';
|
|
13
|
+
import { type TBigInt } from '../bigint/index.mjs';
|
|
14
|
+
import { type TBoolean } from '../boolean/index.mjs';
|
|
15
|
+
import { type TInteger } from '../integer/index.mjs';
|
|
16
|
+
import { type TLiteral } from '../literal/index.mjs';
|
|
17
|
+
import { type TNull } from '../null/index.mjs';
|
|
18
|
+
import { type TNumber } from '../number/index.mjs';
|
|
19
|
+
import { type TString } from '../string/index.mjs';
|
|
20
|
+
import { type TSymbol } from '../symbol/index.mjs';
|
|
21
|
+
import { type TUndefined } from '../undefined/index.mjs';
|
|
13
22
|
import { type TPartialFromMappedResult } from './partial-from-mapped-result.mjs';
|
|
14
23
|
type TFromComputed<Target extends string, Parameters extends TSchema[]> = Ensure<TComputed<'Partial', [TComputed<Target, Parameters>]>>;
|
|
15
24
|
type TFromRef<Ref extends string> = Ensure<TComputed<'Partial', [TRef<Ref>]>>;
|
|
@@ -18,7 +27,7 @@ type TFromProperties<Properties extends TProperties> = Evaluate<{
|
|
|
18
27
|
}>;
|
|
19
28
|
type TFromObject<Type extends TObject, Properties extends TProperties = Type['properties']> = Ensure<TObject<(TFromProperties<Properties>)>>;
|
|
20
29
|
type TFromRest<Types extends TSchema[], Result extends TSchema[] = []> = (Types extends [infer L extends TSchema, ...infer R extends TSchema[]] ? TFromRest<R, [...Result, TPartial<L>]> : Result);
|
|
21
|
-
export type TPartial<
|
|
30
|
+
export type TPartial<Type extends TSchema> = (Type extends TRecursive<infer Type extends TSchema> ? TRecursive<TPartial<Type>> : Type extends TComputed<infer Target extends string, infer Parameters extends TSchema[]> ? TFromComputed<Target, Parameters> : Type extends TRef<infer Ref extends string> ? TFromRef<Ref> : Type extends TIntersect<infer Types extends TSchema[]> ? TIntersect<TFromRest<Types>> : Type extends TUnion<infer Types extends TSchema[]> ? TUnion<TFromRest<Types>> : Type extends TObject<infer Properties extends TProperties> ? TFromObject<TObject<Properties>> : Type extends TBigInt ? Type : Type extends TBoolean ? Type : Type extends TInteger ? Type : Type extends TLiteral ? Type : Type extends TNull ? Type : Type extends TNumber ? Type : Type extends TString ? Type : Type extends TSymbol ? Type : Type extends TUndefined ? Type : TObject<{}>);
|
|
22
31
|
/** `[Json]` Constructs a type where all properties are optional */
|
|
23
32
|
export declare function Partial<MappedResult extends TMappedResult>(type: MappedResult, options?: SchemaOptions): TPartialFromMappedResult<MappedResult>;
|
|
24
33
|
/** `[Json]` Constructs a type where all properties are optional */
|
|
@@ -9,9 +9,9 @@ import { Discard } from '../discard/index.mjs';
|
|
|
9
9
|
import { TransformKind } from '../symbols/index.mjs';
|
|
10
10
|
import { PartialFromMappedResult } from './partial-from-mapped-result.mjs';
|
|
11
11
|
// ------------------------------------------------------------------
|
|
12
|
-
//
|
|
12
|
+
// KindGuard
|
|
13
13
|
// ------------------------------------------------------------------
|
|
14
|
-
import
|
|
14
|
+
import * as KindGuard from '../guard/kind.mjs';
|
|
15
15
|
// prettier-ignore
|
|
16
16
|
function FromComputed(target, parameters) {
|
|
17
17
|
return Computed('Partial', [Computed(target, parameters)]);
|
|
@@ -28,9 +28,9 @@ function FromProperties(properties) {
|
|
|
28
28
|
return partialProperties;
|
|
29
29
|
}
|
|
30
30
|
// prettier-ignore
|
|
31
|
-
function FromObject(
|
|
32
|
-
const options = Discard(
|
|
33
|
-
const properties = FromProperties(
|
|
31
|
+
function FromObject(type) {
|
|
32
|
+
const options = Discard(type, [TransformKind, '$id', 'required', 'properties']);
|
|
33
|
+
const properties = FromProperties(type['properties']);
|
|
34
34
|
return Object(properties, options);
|
|
35
35
|
}
|
|
36
36
|
// prettier-ignore
|
|
@@ -42,16 +42,29 @@ function FromRest(types) {
|
|
|
42
42
|
// ------------------------------------------------------------------
|
|
43
43
|
// prettier-ignore
|
|
44
44
|
function PartialResolve(type) {
|
|
45
|
-
return (
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
45
|
+
return (
|
|
46
|
+
// Mappable
|
|
47
|
+
KindGuard.IsComputed(type) ? FromComputed(type.target, type.parameters) :
|
|
48
|
+
KindGuard.IsRef(type) ? FromRef(type.$ref) :
|
|
49
|
+
KindGuard.IsIntersect(type) ? Intersect(FromRest(type.allOf)) :
|
|
50
|
+
KindGuard.IsUnion(type) ? Union(FromRest(type.anyOf)) :
|
|
51
|
+
KindGuard.IsObject(type) ? FromObject(type) :
|
|
52
|
+
// Intrinsic
|
|
53
|
+
KindGuard.IsBigInt(type) ? type :
|
|
54
|
+
KindGuard.IsBoolean(type) ? type :
|
|
55
|
+
KindGuard.IsInteger(type) ? type :
|
|
56
|
+
KindGuard.IsLiteral(type) ? type :
|
|
57
|
+
KindGuard.IsNull(type) ? type :
|
|
58
|
+
KindGuard.IsNumber(type) ? type :
|
|
59
|
+
KindGuard.IsString(type) ? type :
|
|
60
|
+
KindGuard.IsSymbol(type) ? type :
|
|
61
|
+
KindGuard.IsUndefined(type) ? type :
|
|
62
|
+
// Passthrough
|
|
63
|
+
Object({}));
|
|
51
64
|
}
|
|
52
65
|
/** `[Json]` Constructs a type where all properties are optional */
|
|
53
66
|
export function Partial(type, options) {
|
|
54
|
-
if (IsMappedResult(type)) {
|
|
67
|
+
if (KindGuard.IsMappedResult(type)) {
|
|
55
68
|
return PartialFromMappedResult(type, options);
|
|
56
69
|
}
|
|
57
70
|
else {
|
|
@@ -10,6 +10,15 @@ import { type TObject, type TProperties } from '../object/index.mjs';
|
|
|
10
10
|
import { type TIntersect } from '../intersect/index.mjs';
|
|
11
11
|
import { type TUnion } from '../union/index.mjs';
|
|
12
12
|
import { type TRef } from '../ref/index.mjs';
|
|
13
|
+
import { type TBigInt } from '../bigint/index.mjs';
|
|
14
|
+
import { type TBoolean } from '../boolean/index.mjs';
|
|
15
|
+
import { type TInteger } from '../integer/index.mjs';
|
|
16
|
+
import { type TLiteral } from '../literal/index.mjs';
|
|
17
|
+
import { type TNull } from '../null/index.mjs';
|
|
18
|
+
import { type TNumber } from '../number/index.mjs';
|
|
19
|
+
import { type TString } from '../string/index.mjs';
|
|
20
|
+
import { type TSymbol } from '../symbol/index.mjs';
|
|
21
|
+
import { type TUndefined } from '../undefined/index.mjs';
|
|
13
22
|
import { type TRequiredFromMappedResult } from './required-from-mapped-result.mjs';
|
|
14
23
|
type TFromComputed<Target extends string, Parameters extends TSchema[]> = Ensure<TComputed<'Required', [TComputed<Target, Parameters>]>>;
|
|
15
24
|
type TFromRef<Ref extends string> = Ensure<TComputed<'Required', [TRef<Ref>]>>;
|
|
@@ -18,7 +27,7 @@ type TFromProperties<Properties extends TProperties> = Evaluate<{
|
|
|
18
27
|
}>;
|
|
19
28
|
type TFromObject<Type extends TObject, Properties extends TProperties = Type['properties']> = Ensure<TObject<(TFromProperties<Properties>)>>;
|
|
20
29
|
type TFromRest<Types extends TSchema[], Result extends TSchema[] = []> = (Types extends [infer L extends TSchema, ...infer R extends TSchema[]] ? TFromRest<R, [...Result, TRequired<L>]> : Result);
|
|
21
|
-
export type TRequired<Type extends TSchema> = (Type extends TRecursive<infer Type extends TSchema> ? TRecursive<TRequired<Type>> : Type extends TComputed<infer Target extends string, infer Parameters extends TSchema[]> ? TFromComputed<Target, Parameters> : Type extends TRef<infer Ref extends string> ? TFromRef<Ref> : Type extends TIntersect<infer Types extends TSchema[]> ? TIntersect<TFromRest<Types>> : Type extends TUnion<infer Types extends TSchema[]> ? TUnion<TFromRest<Types>> : Type extends TObject<infer Properties extends TProperties> ? TFromObject<TObject<Properties>> : TObject<{}>);
|
|
30
|
+
export type TRequired<Type extends TSchema> = (Type extends TRecursive<infer Type extends TSchema> ? TRecursive<TRequired<Type>> : Type extends TComputed<infer Target extends string, infer Parameters extends TSchema[]> ? TFromComputed<Target, Parameters> : Type extends TRef<infer Ref extends string> ? TFromRef<Ref> : Type extends TIntersect<infer Types extends TSchema[]> ? TIntersect<TFromRest<Types>> : Type extends TUnion<infer Types extends TSchema[]> ? TUnion<TFromRest<Types>> : Type extends TObject<infer Properties extends TProperties> ? TFromObject<TObject<Properties>> : Type extends TBigInt ? Type : Type extends TBoolean ? Type : Type extends TInteger ? Type : Type extends TLiteral ? Type : Type extends TNull ? Type : Type extends TNumber ? Type : Type extends TString ? Type : Type extends TSymbol ? Type : Type extends TUndefined ? Type : TObject<{}>);
|
|
22
31
|
/** `[Json]` Constructs a type where all properties are required */
|
|
23
32
|
export declare function Required<MappedResult extends TMappedResult>(type: MappedResult, options?: SchemaOptions): TRequiredFromMappedResult<MappedResult>;
|
|
24
33
|
/** `[Json]` Constructs a type where all properties are required */
|
|
@@ -10,7 +10,7 @@ import { RequiredFromMappedResult } from './required-from-mapped-result.mjs';
|
|
|
10
10
|
// ------------------------------------------------------------------
|
|
11
11
|
// TypeGuard
|
|
12
12
|
// ------------------------------------------------------------------
|
|
13
|
-
import
|
|
13
|
+
import * as KindGuard from '../guard/kind.mjs';
|
|
14
14
|
// prettier-ignore
|
|
15
15
|
function FromComputed(target, parameters) {
|
|
16
16
|
return Computed('Required', [Computed(target, parameters)]);
|
|
@@ -41,16 +41,29 @@ function FromRest(types) {
|
|
|
41
41
|
// ------------------------------------------------------------------
|
|
42
42
|
// prettier-ignore
|
|
43
43
|
function RequiredResolve(type) {
|
|
44
|
-
return (
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
44
|
+
return (
|
|
45
|
+
// Mappable
|
|
46
|
+
KindGuard.IsComputed(type) ? FromComputed(type.target, type.parameters) :
|
|
47
|
+
KindGuard.IsRef(type) ? FromRef(type.$ref) :
|
|
48
|
+
KindGuard.IsIntersect(type) ? Intersect(FromRest(type.allOf)) :
|
|
49
|
+
KindGuard.IsUnion(type) ? Union(FromRest(type.anyOf)) :
|
|
50
|
+
KindGuard.IsObject(type) ? FromObject(type) :
|
|
51
|
+
// Intrinsic
|
|
52
|
+
KindGuard.IsBigInt(type) ? type :
|
|
53
|
+
KindGuard.IsBoolean(type) ? type :
|
|
54
|
+
KindGuard.IsInteger(type) ? type :
|
|
55
|
+
KindGuard.IsLiteral(type) ? type :
|
|
56
|
+
KindGuard.IsNull(type) ? type :
|
|
57
|
+
KindGuard.IsNumber(type) ? type :
|
|
58
|
+
KindGuard.IsString(type) ? type :
|
|
59
|
+
KindGuard.IsSymbol(type) ? type :
|
|
60
|
+
KindGuard.IsUndefined(type) ? type :
|
|
61
|
+
// Passthrough
|
|
62
|
+
Object({}));
|
|
50
63
|
}
|
|
51
64
|
/** `[Json]` Constructs a type where all properties are required */
|
|
52
65
|
export function Required(type, options) {
|
|
53
|
-
if (IsMappedResult(type)) {
|
|
66
|
+
if (KindGuard.IsMappedResult(type)) {
|
|
54
67
|
return RequiredFromMappedResult(type, options);
|
|
55
68
|
}
|
|
56
69
|
else {
|
|
@@ -43,7 +43,6 @@ export { Record } from '../record/index.mjs';
|
|
|
43
43
|
export { Recursive } from '../recursive/index.mjs';
|
|
44
44
|
export { Ref } from '../ref/index.mjs';
|
|
45
45
|
export { RegExp } from '../regexp/index.mjs';
|
|
46
|
-
export { Remap } from '../remap/index.mjs';
|
|
47
46
|
export { Required } from '../required/index.mjs';
|
|
48
47
|
export { Rest } from '../rest/index.mjs';
|
|
49
48
|
export { ReturnType } from '../return-type/index.mjs';
|
|
@@ -46,7 +46,6 @@ export { Record } from '../record/index.mjs';
|
|
|
46
46
|
export { Recursive } from '../recursive/index.mjs';
|
|
47
47
|
export { Ref } from '../ref/index.mjs';
|
|
48
48
|
export { RegExp } from '../regexp/index.mjs';
|
|
49
|
-
export { Remap } from '../remap/index.mjs';
|
|
50
49
|
export { Required } from '../required/index.mjs';
|
|
51
50
|
export { Rest } from '../rest/index.mjs';
|
|
52
51
|
export { ReturnType } from '../return-type/index.mjs';
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -64,7 +64,6 @@ License MIT
|
|
|
64
64
|
- [Types](#types)
|
|
65
65
|
- [Json](#types-json)
|
|
66
66
|
- [JavaScript](#types-javascript)
|
|
67
|
-
- [Import](#types-import)
|
|
68
67
|
- [Options](#types-options)
|
|
69
68
|
- [Properties](#types-properties)
|
|
70
69
|
- [Generics](#types-generics)
|
|
@@ -97,9 +96,9 @@ License MIT
|
|
|
97
96
|
- [Mutate](#values-mutate)
|
|
98
97
|
- [Pointer](#values-pointer)
|
|
99
98
|
- [Syntax](#syntax)
|
|
100
|
-
- [
|
|
101
|
-
- [Options](#syntax-options)
|
|
99
|
+
- [Create](#syntax-create)
|
|
102
100
|
- [Parameters](#syntax-parameters)
|
|
101
|
+
- [Options](#syntax-options)
|
|
103
102
|
- [Generics](#syntax-generics)
|
|
104
103
|
- [TypeRegistry](#typeregistry)
|
|
105
104
|
- [Type](#typeregistry-type)
|
|
@@ -107,6 +106,8 @@ License MIT
|
|
|
107
106
|
- [TypeCheck](#typecheck)
|
|
108
107
|
- [Ajv](#typecheck-ajv)
|
|
109
108
|
- [TypeCompiler](#typecheck-typecompiler)
|
|
109
|
+
- [TypeMap](#typemap)
|
|
110
|
+
- [Usage](#typemap-usage)
|
|
110
111
|
- [TypeSystem](#typesystem)
|
|
111
112
|
- [Policies](#typesystem-policies)
|
|
112
113
|
- [Error Function](#error-function)
|
|
@@ -638,22 +639,6 @@ TypeBox provides an extended type set that can be used to create schematics for
|
|
|
638
639
|
└────────────────────────────────┴─────────────────────────────┴────────────────────────────────┘
|
|
639
640
|
```
|
|
640
641
|
|
|
641
|
-
<a name='types-import'></a>
|
|
642
|
-
|
|
643
|
-
### Import
|
|
644
|
-
|
|
645
|
-
Import the Type namespace to bring in the full TypeBox type system. This is recommended for most users.
|
|
646
|
-
|
|
647
|
-
```typescript
|
|
648
|
-
import { Type, type Static } from '@sinclair/typebox'
|
|
649
|
-
```
|
|
650
|
-
|
|
651
|
-
You can also selectively import types. This enables modern bundlers to tree shake for unused types.
|
|
652
|
-
|
|
653
|
-
```typescript
|
|
654
|
-
import { Object, Number, String, Boolean, type Static } from '@sinclair/typebox'
|
|
655
|
-
```
|
|
656
|
-
|
|
657
642
|
<a name='types-options'></a>
|
|
658
643
|
|
|
659
644
|
### Options
|
|
@@ -741,22 +726,6 @@ const Nullable = <T extends TSchema>(T: T) => { // type Nullable<T> = T | nu
|
|
|
741
726
|
const T = Nullable(Type.String()) // type T = Nullable<string>
|
|
742
727
|
```
|
|
743
728
|
|
|
744
|
-
Generic types can also be created with Argument types
|
|
745
|
-
|
|
746
|
-
```typescript
|
|
747
|
-
const Vector = Type.Object({ // type Vector<A_0, A_1, A_2> = {
|
|
748
|
-
x: Type.Argument(0), // x: A_0,
|
|
749
|
-
y: Type.Argument(1), // y: A_1,
|
|
750
|
-
z: Type.Argument(2), // z: A_2
|
|
751
|
-
}) // }
|
|
752
|
-
|
|
753
|
-
const T = Type.Instantiate(Vector, [ // type T = Vector<
|
|
754
|
-
Type.Boolean(), // boolean,
|
|
755
|
-
Type.Number(), // number,
|
|
756
|
-
Type.String() // string
|
|
757
|
-
]) // >
|
|
758
|
-
```
|
|
759
|
-
|
|
760
729
|
<a name='types-recursive'></a>
|
|
761
730
|
|
|
762
731
|
### Recursive Types
|
|
@@ -1337,30 +1306,50 @@ ValuePointer.Set(A, '/z', 1) // A' = { x: 1, y: 1, z: 1
|
|
|
1337
1306
|
|
|
1338
1307
|
## Syntax Types
|
|
1339
1308
|
|
|
1340
|
-
TypeBox
|
|
1309
|
+
TypeBox has support for parsing TypeScript syntax at runtime as well as statically in the type system. This feature offers a syntactical frontend to the TypeBox type builder.
|
|
1310
|
+
|
|
1311
|
+
Syntax types are available via optional import.
|
|
1341
1312
|
|
|
1342
1313
|
```typescript
|
|
1343
1314
|
import { Syntax } from '@sinclair/typebox/syntax'
|
|
1344
1315
|
```
|
|
1345
1316
|
|
|
1346
|
-
<a name='syntax-
|
|
1317
|
+
<a name='syntax-create'></a>
|
|
1318
|
+
|
|
1319
|
+
### Create
|
|
1320
|
+
|
|
1321
|
+
Use the Syntax function to create TypeBox types from TypeScript syntax
|
|
1347
1322
|
|
|
1348
|
-
|
|
1323
|
+
```typescript
|
|
1324
|
+
const T = Syntax(`{ x: number, y: number }`) // const T: TObject<{
|
|
1325
|
+
// x: TNumber,
|
|
1326
|
+
// y: TNumber
|
|
1327
|
+
// }>
|
|
1328
|
+
```
|
|
1329
|
+
|
|
1330
|
+
<a name="syntax-parameters"></a>
|
|
1331
|
+
|
|
1332
|
+
### Parameters
|
|
1349
1333
|
|
|
1350
|
-
|
|
1334
|
+
Syntax types can be parameterized to receive exterior types.
|
|
1351
1335
|
|
|
1352
1336
|
```typescript
|
|
1353
1337
|
const T = Syntax(`{ x: number, y: number }`) // const T: TObject<{
|
|
1354
|
-
// x: TNumber
|
|
1338
|
+
// x: TNumber,
|
|
1355
1339
|
// y: TNumber
|
|
1356
1340
|
// }>
|
|
1341
|
+
|
|
1342
|
+
const S = Syntax({ T }, `Partial<T>`) // const S: TObject<{
|
|
1343
|
+
// x: TOptional<TNumber>,
|
|
1344
|
+
// y: TOptional<TNumber>
|
|
1345
|
+
// }>
|
|
1357
1346
|
```
|
|
1358
1347
|
|
|
1359
1348
|
<a name='syntax-options'></a>
|
|
1360
1349
|
|
|
1361
1350
|
### Options
|
|
1362
1351
|
|
|
1363
|
-
Options can be passed
|
|
1352
|
+
Options can be passed via the last parameter
|
|
1364
1353
|
|
|
1365
1354
|
```typescript
|
|
1366
1355
|
const T = Syntax(`number`, { // const T = {
|
|
@@ -1370,42 +1359,30 @@ const T = Syntax(`number`, { // const T = {
|
|
|
1370
1359
|
// }
|
|
1371
1360
|
```
|
|
1372
1361
|
|
|
1373
|
-
<a name="syntax-parameters"></a>
|
|
1374
|
-
|
|
1375
|
-
### Parameters
|
|
1376
|
-
|
|
1377
|
-
Syntax types can be parameterized to accept exterior types.
|
|
1378
|
-
|
|
1379
|
-
```typescript
|
|
1380
|
-
const T = Syntax('number')
|
|
1381
|
-
|
|
1382
|
-
const S = Syntax({ T }, `{ x: T, y: T, z: T }`) // const S: TObject<{
|
|
1383
|
-
// x: TNumber,
|
|
1384
|
-
// y: TNumber,
|
|
1385
|
-
// z: TNumber
|
|
1386
|
-
// }>
|
|
1387
|
-
```
|
|
1388
|
-
|
|
1389
1362
|
<a name="syntax-generics"></a>
|
|
1390
1363
|
|
|
1391
1364
|
### Generics
|
|
1392
1365
|
|
|
1393
|
-
Generic types can be created using
|
|
1366
|
+
Generic types can be created using positional argument types ([Example](https://www.typescriptlang.org/play/?moduleResolution=99&module=199&ts=5.8.0-beta#code/JYWwDg9gTgLgBAbzgZQJ4DsYEMAecC+cAZlBCHAOQACAzsOgMYA2WwUA9DKmAKYBGEHOxoZsOCgChQkWIhTYYwBgWKly1OoxZtO3foMkSGEdDXgAVAAxwAvClG4AFBQCCUAOYBXED0wAeSwA+CgBKIxMzOHMARlt7TCdXD29fGD9o4LDjUwsAJji0BJxnNy8ff1zMiXCcuAA1HgYYaAKHYqQrABoo6O7zfPxugAMECTg4HAAuKMtOsbhUaZi58YAvJdyJfCGwmsiAISw6Ggam6BpWosckU+aoAmHR8an6xrv07tm4IJWF6dvoAFur1voFfutXmcoEDvsCwVsdtUuLw4IdjgCoBc7MgFEo-MieBAiKijsATm9zoFxtT2Ow4ASSeiKZi4k9qeyOZyudyeTzadSXkgXiDFrC4BDrIN5ryZbK5ez+eNRULpl9RSCJQ9pfKdbqFXS1tMVWLRV8IbF8Nq9da5fzCEA))
|
|
1394
1367
|
|
|
1395
1368
|
```typescript
|
|
1396
|
-
const
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1369
|
+
const T0 = Syntax('Argument<0>')
|
|
1370
|
+
const T1 = Syntax('Argument<1>')
|
|
1371
|
+
const T2 = Syntax('Argument<2>')
|
|
1372
|
+
|
|
1373
|
+
const Vector = Syntax({ T0, T1, T2 }, `{
|
|
1374
|
+
x: T0,
|
|
1375
|
+
y: T1,
|
|
1376
|
+
z: T2
|
|
1400
1377
|
}`)
|
|
1401
1378
|
|
|
1402
|
-
const
|
|
1379
|
+
const BasisVectors = Syntax({ Vector }, `{
|
|
1403
1380
|
x: Vector<1, 0, 0>,
|
|
1404
1381
|
y: Vector<0, 1, 0>,
|
|
1405
1382
|
z: Vector<0, 0, 1>,
|
|
1406
1383
|
}`)
|
|
1407
1384
|
|
|
1408
|
-
type
|
|
1385
|
+
type BasisVectors = Static<typeof BasisVectors> // type BasisVectors = {
|
|
1409
1386
|
// x: { x: 1, y: 0, z: 0 },
|
|
1410
1387
|
// y: { x: 0, y: 1, z: 0 },
|
|
1411
1388
|
// z: { x: 0, y: 0, z: 1 }
|
|
@@ -1573,6 +1550,44 @@ const C = TypeCompiler.Code(Type.String()) // const C = `return functi
|
|
|
1573
1550
|
// }`
|
|
1574
1551
|
```
|
|
1575
1552
|
|
|
1553
|
+
<a name='typemap'></a>
|
|
1554
|
+
|
|
1555
|
+
## TypeMap
|
|
1556
|
+
|
|
1557
|
+
TypeBox offers an external package for bi-directional mapping between TypeBox, Valibot, and Zod type libraries. It also includes syntax parsing support for Valibot and Zod and supports the Standard Schema specification. For more details on TypeMap, refer to the project repository.
|
|
1558
|
+
|
|
1559
|
+
[TypeMap Repository](https://github.com/sinclairzx81/typemap)
|
|
1560
|
+
|
|
1561
|
+
<a name='typemap-usage'></a>
|
|
1562
|
+
|
|
1563
|
+
### Usage
|
|
1564
|
+
|
|
1565
|
+
TypeMap needs to be installed seperately
|
|
1566
|
+
|
|
1567
|
+
```bash
|
|
1568
|
+
$ npm install @sinclair/typemap
|
|
1569
|
+
```
|
|
1570
|
+
|
|
1571
|
+
Once installed it offers advanced structural remapping between various runtime type libraries ([Example](https://www.typescriptlang.org/play/?moduleResolution=99&module=199&ts=5.8.0-beta#code/JYWwDg9gTgLgBAbzgFQJ5gKYCEIA8A0cAyqgHYwCGBcAWhACZwC+cAZlBCHAOQACAzsFIBjADYVgUAPQx0GEBTDcAUMuERS-eMjgBeFHJy4AFAAMkuAFxxSAVxAAjDFEKprdx88IAvd-adQzKYAlHBwUlJw6pra1sgA8g4AVhjCMAA8CMphObl5+QWFRcW5ETlWKABy-s4A3NkljU3NBWVhblU1UPUtvX3FbXC+nZ7dDf0TE2VMAHyq0VrEesRklCbIoS1lC-BE1twWfqOuRwE+p87MKmoaiwBKy3T0xkTBAHRgFFD8GMZ2oqJNnltrd4HdrFlJltImEKh4Aj0oU1Bh14XVxkiBjChhcxpjGtMwkA))
|
|
1572
|
+
|
|
1573
|
+
```typescript
|
|
1574
|
+
import { TypeBox, Syntax, Zod } from '@sinclair/typemap'
|
|
1575
|
+
|
|
1576
|
+
const T = TypeBox(`{ x: number, y: number, z: number }`) // const T: TObject<{
|
|
1577
|
+
// x: TNumber;
|
|
1578
|
+
// y: TNumber;
|
|
1579
|
+
// z: TNumber;
|
|
1580
|
+
// }>
|
|
1581
|
+
|
|
1582
|
+
const S = Syntax(T) // const S: '{ x: number, y: number, z: number }'
|
|
1583
|
+
|
|
1584
|
+
const R = Zod(S).parse(null) // const R: {
|
|
1585
|
+
// x: number;
|
|
1586
|
+
// y: number;
|
|
1587
|
+
// z: number;
|
|
1588
|
+
// }
|
|
1589
|
+
```
|
|
1590
|
+
|
|
1576
1591
|
<a name='typesystem'></a>
|
|
1577
1592
|
|
|
1578
1593
|
## TypeSystem
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './remap';
|