@sinclair/typebox 0.34.22 → 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/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/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
package/build/cjs/index.d.ts
CHANGED
|
@@ -52,7 +52,6 @@ export * from './type/record/index';
|
|
|
52
52
|
export * from './type/recursive/index';
|
|
53
53
|
export * from './type/ref/index';
|
|
54
54
|
export * from './type/regexp/index';
|
|
55
|
-
export * from './type/remap/index';
|
|
56
55
|
export * from './type/required/index';
|
|
57
56
|
export * from './type/rest/index';
|
|
58
57
|
export * from './type/return-type/index';
|
package/build/cjs/index.js
CHANGED
|
@@ -75,7 +75,6 @@ __exportStar(require("./type/record/index"), exports);
|
|
|
75
75
|
__exportStar(require("./type/recursive/index"), exports);
|
|
76
76
|
__exportStar(require("./type/ref/index"), exports);
|
|
77
77
|
__exportStar(require("./type/regexp/index"), exports);
|
|
78
|
-
__exportStar(require("./type/remap/index"), exports);
|
|
79
78
|
__exportStar(require("./type/required/index"), exports);
|
|
80
79
|
__exportStar(require("./type/rest/index"), exports);
|
|
81
80
|
__exportStar(require("./type/return-type/index"), exports);
|
|
@@ -43,10 +43,17 @@ const Dereference = (context, key) => {
|
|
|
43
43
|
// GenericReference
|
|
44
44
|
// ------------------------------------------------------------------
|
|
45
45
|
function GenericReferenceMapping(results, context) {
|
|
46
|
-
const
|
|
47
|
-
|
|
46
|
+
const type = Dereference(context, results[0]);
|
|
47
|
+
const args = results[2];
|
|
48
|
+
return t.Instantiate(type, args);
|
|
48
49
|
}
|
|
49
|
-
|
|
50
|
+
// prettier-ignore
|
|
51
|
+
const GenericReference = index_1.Runtime.Tuple([
|
|
52
|
+
index_1.Runtime.Ident(),
|
|
53
|
+
index_1.Runtime.Const(LAngle),
|
|
54
|
+
index_1.Runtime.Ref('Elements'),
|
|
55
|
+
index_1.Runtime.Const(RAngle)
|
|
56
|
+
], (results, context) => GenericReferenceMapping(results, context));
|
|
50
57
|
// ------------------------------------------------------------------
|
|
51
58
|
// Reference
|
|
52
59
|
// ------------------------------------------------------------------
|
|
@@ -43,7 +43,7 @@ type Delimit<Parser extends Static.IParser, Delimiter extends Static.IParser> =
|
|
|
43
43
|
], DelimitMapping>);
|
|
44
44
|
type Dereference<Context extends t.TProperties, Ref extends string> = (Ref extends keyof Context ? Context[Ref] : t.TRef<Ref>);
|
|
45
45
|
interface GenericReferenceMapping extends Static.IMapping {
|
|
46
|
-
output: this['context'] extends t.TProperties ? this['input'] extends [infer Reference extends string, LAngle, infer
|
|
46
|
+
output: this['context'] extends t.TProperties ? this['input'] extends [infer Reference extends string, LAngle, infer Args extends t.TSchema[], RAngle] ? t.TInstantiate<Dereference<this['context'], Reference>, Args> : never : never;
|
|
47
47
|
}
|
|
48
48
|
type GenericReference = Static.Tuple<[
|
|
49
49
|
Static.Ident,
|
|
@@ -1,25 +1,50 @@
|
|
|
1
|
-
import type
|
|
2
|
-
import type
|
|
1
|
+
import { type TSchema } from '../schema/index';
|
|
2
|
+
import { type TArgument } from '../argument/index';
|
|
3
3
|
import { type TNever } from '../never/index';
|
|
4
4
|
import { type TReadonlyOptional } from '../readonly-optional/index';
|
|
5
5
|
import { type TReadonly } from '../readonly/index';
|
|
6
6
|
import { type TOptional } from '../optional/index';
|
|
7
|
-
import { type
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
7
|
+
import { type TConstructor } from '../constructor/index';
|
|
8
|
+
import { type TFunction } from '../function/index';
|
|
9
|
+
import { type TIntersect } from '../intersect/index';
|
|
10
|
+
import { type TUnion } from '../union/index';
|
|
11
|
+
import { type TTuple } from '../tuple/index';
|
|
12
|
+
import { type TArray } from '../array/index';
|
|
13
|
+
import { type TAsyncIterator } from '../async-iterator/index';
|
|
14
|
+
import { type TIterator } from '../iterator/index';
|
|
15
|
+
import { type TPromise } from '../promise/index';
|
|
16
|
+
import { type TObject, type TProperties } from '../object/index';
|
|
17
|
+
import { type TRecordOrObject, type TRecord } from '../record/index';
|
|
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,32 +1,119 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.FromTypes = FromTypes;
|
|
4
5
|
exports.Instantiate = Instantiate;
|
|
6
|
+
const type_1 = require("../clone/type");
|
|
5
7
|
const index_1 = require("../never/index");
|
|
6
8
|
const index_2 = require("../readonly-optional/index");
|
|
7
9
|
const index_3 = require("../readonly/index");
|
|
8
10
|
const index_4 = require("../optional/index");
|
|
9
|
-
const index_5 = require("../
|
|
11
|
+
const index_5 = require("../record/index");
|
|
12
|
+
const ValueGuard = require("../guard/value");
|
|
10
13
|
const KindGuard = require("../guard/kind");
|
|
11
14
|
// prettier-ignore
|
|
12
|
-
function
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
return
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
15
|
+
function FromConstructor(args, type) {
|
|
16
|
+
type.parameters = FromTypes(args, type.parameters);
|
|
17
|
+
type.returns = FromType(args, type.returns);
|
|
18
|
+
return type;
|
|
19
|
+
}
|
|
20
|
+
// prettier-ignore
|
|
21
|
+
function FromFunction(args, type) {
|
|
22
|
+
type.parameters = FromTypes(args, type.parameters);
|
|
23
|
+
type.returns = FromType(args, type.returns);
|
|
24
|
+
return type;
|
|
25
|
+
}
|
|
26
|
+
// prettier-ignore
|
|
27
|
+
function FromIntersect(args, type) {
|
|
28
|
+
type.allOf = FromTypes(args, type.allOf);
|
|
29
|
+
return type;
|
|
30
|
+
}
|
|
31
|
+
// prettier-ignore
|
|
32
|
+
function FromUnion(args, type) {
|
|
33
|
+
type.anyOf = FromTypes(args, type.anyOf);
|
|
34
|
+
return type;
|
|
35
|
+
}
|
|
36
|
+
// prettier-ignore
|
|
37
|
+
function FromTuple(args, type) {
|
|
38
|
+
if (ValueGuard.IsUndefined(type.items))
|
|
39
|
+
return type;
|
|
40
|
+
type.items = FromTypes(args, type.items);
|
|
41
|
+
return type;
|
|
42
|
+
}
|
|
43
|
+
// prettier-ignore
|
|
44
|
+
function FromArray(args, type) {
|
|
45
|
+
type.items = FromType(args, type.items);
|
|
46
|
+
return type;
|
|
47
|
+
}
|
|
48
|
+
// prettier-ignore
|
|
49
|
+
function FromAsyncIterator(args, type) {
|
|
50
|
+
type.items = FromType(args, type.items);
|
|
51
|
+
return type;
|
|
52
|
+
}
|
|
53
|
+
// prettier-ignore
|
|
54
|
+
function FromIterator(args, type) {
|
|
55
|
+
type.items = FromType(args, type.items);
|
|
56
|
+
return type;
|
|
57
|
+
}
|
|
58
|
+
// prettier-ignore
|
|
59
|
+
function FromPromise(args, type) {
|
|
60
|
+
type.item = FromType(args, type.item);
|
|
61
|
+
return type;
|
|
62
|
+
}
|
|
63
|
+
// prettier-ignore
|
|
64
|
+
function FromObject(args, type) {
|
|
65
|
+
type.properties = FromProperties(args, type.properties);
|
|
66
|
+
return type;
|
|
67
|
+
}
|
|
68
|
+
// prettier-ignore
|
|
69
|
+
function FromRecord(args, type) {
|
|
70
|
+
const mappedKey = FromType(args, (0, index_5.RecordKey)(type));
|
|
71
|
+
const mappedValue = FromType(args, (0, index_5.RecordValue)(type));
|
|
72
|
+
const result = (0, index_5.Record)(mappedKey, mappedValue);
|
|
73
|
+
return { ...type, ...result }; // retain options
|
|
74
|
+
}
|
|
75
|
+
// prettier-ignore
|
|
76
|
+
function FromArgument(args, argument) {
|
|
77
|
+
return argument.index in args ? args[argument.index] : (0, index_1.Never)();
|
|
78
|
+
}
|
|
79
|
+
// prettier-ignore
|
|
80
|
+
function FromProperty(args, type) {
|
|
81
|
+
const isReadonly = KindGuard.IsReadonly(type);
|
|
82
|
+
const isOptional = KindGuard.IsOptional(type);
|
|
83
|
+
const mapped = FromType(args, type);
|
|
84
|
+
return (isReadonly && isOptional ? (0, index_2.ReadonlyOptional)(mapped) :
|
|
85
|
+
isReadonly && !isOptional ? (0, index_3.Readonly)(mapped) :
|
|
86
|
+
!isReadonly && isOptional ? (0, index_4.Optional)(mapped) :
|
|
87
|
+
mapped);
|
|
88
|
+
}
|
|
89
|
+
// prettier-ignore
|
|
90
|
+
function FromProperties(args, properties) {
|
|
91
|
+
return globalThis.Object.getOwnPropertyNames(properties).reduce((result, key) => {
|
|
92
|
+
return { ...result, [key]: FromProperty(args, properties[key]) };
|
|
93
|
+
}, {});
|
|
94
|
+
}
|
|
95
|
+
// prettier-ignore
|
|
96
|
+
function FromTypes(args, types) {
|
|
97
|
+
return types.map(type => FromType(args, type));
|
|
98
|
+
}
|
|
99
|
+
// prettier-ignore
|
|
100
|
+
function FromType(args, type) {
|
|
101
|
+
return (KindGuard.IsConstructor(type) ? FromConstructor(args, type) :
|
|
102
|
+
KindGuard.IsFunction(type) ? FromFunction(args, type) :
|
|
103
|
+
KindGuard.IsIntersect(type) ? FromIntersect(args, type) :
|
|
104
|
+
KindGuard.IsUnion(type) ? FromUnion(args, type) :
|
|
105
|
+
KindGuard.IsTuple(type) ? FromTuple(args, type) :
|
|
106
|
+
KindGuard.IsArray(type) ? FromArray(args, type) :
|
|
107
|
+
KindGuard.IsAsyncIterator(type) ? FromAsyncIterator(args, type) :
|
|
108
|
+
KindGuard.IsIterator(type) ? FromIterator(args, type) :
|
|
109
|
+
KindGuard.IsPromise(type) ? FromPromise(args, type) :
|
|
110
|
+
KindGuard.IsObject(type) ? FromObject(args, type) :
|
|
111
|
+
KindGuard.IsRecord(type) ? FromRecord(args, type) :
|
|
112
|
+
KindGuard.IsArgument(type) ? FromArgument(args, type) :
|
|
113
|
+
type);
|
|
19
114
|
}
|
|
20
115
|
/** `[JavaScript]` Instantiates a type with the given parameters */
|
|
21
116
|
// prettier-ignore
|
|
22
117
|
function Instantiate(type, args) {
|
|
23
|
-
return (0,
|
|
24
|
-
return KindGuard.IsArgument(type)
|
|
25
|
-
? type.index in args
|
|
26
|
-
? KindGuard.IsSchema(args[type.index])
|
|
27
|
-
? InstantiateArgument(type, args[type.index])
|
|
28
|
-
: (0, index_1.Never)()
|
|
29
|
-
: (0, index_1.Never)()
|
|
30
|
-
: type;
|
|
31
|
-
});
|
|
118
|
+
return FromType(args, (0, type_1.CloneType)(type));
|
|
32
119
|
}
|
|
@@ -43,7 +43,6 @@ export { Record } from '../record/index';
|
|
|
43
43
|
export { Recursive } from '../recursive/index';
|
|
44
44
|
export { Ref } from '../ref/index';
|
|
45
45
|
export { RegExp } from '../regexp/index';
|
|
46
|
-
export { Remap } from '../remap/index';
|
|
47
46
|
export { Required } from '../required/index';
|
|
48
47
|
export { Rest } from '../rest/index';
|
|
49
48
|
export { ReturnType } from '../return-type/index';
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
exports.
|
|
5
|
-
exports.Void = exports.Unsafe = exports.Unknown = exports.Union = exports.Undefined = exports.Uint8Array = exports.Tuple = exports.Transform = exports.TemplateLiteral = exports.Symbol = exports.String = exports.ReturnType =
|
|
4
|
+
exports.Rest = exports.Required = exports.RegExp = exports.Ref = exports.Recursive = exports.Record = exports.ReadonlyOptional = exports.Readonly = exports.Promise = exports.Pick = exports.Partial = exports.Parameters = exports.Optional = exports.Omit = exports.Object = exports.Number = exports.Null = exports.Not = exports.Never = exports.Module = exports.Mapped = exports.Literal = exports.KeyOf = exports.Iterator = exports.Uppercase = exports.Lowercase = exports.Uncapitalize = exports.Capitalize = exports.Intersect = exports.Integer = exports.Instantiate = exports.InstanceType = exports.Index = exports.Function = exports.Extract = exports.Extends = exports.Exclude = exports.Enum = exports.Date = exports.ConstructorParameters = exports.Constructor = exports.Const = exports.Composite = exports.Boolean = exports.BigInt = exports.Awaited = exports.AsyncIterator = exports.Array = exports.Argument = exports.Any = void 0;
|
|
5
|
+
exports.Void = exports.Unsafe = exports.Unknown = exports.Union = exports.Undefined = exports.Uint8Array = exports.Tuple = exports.Transform = exports.TemplateLiteral = exports.Symbol = exports.String = exports.ReturnType = void 0;
|
|
6
6
|
// ------------------------------------------------------------------
|
|
7
7
|
// Type: Module
|
|
8
8
|
// ------------------------------------------------------------------
|
|
@@ -99,33 +99,31 @@ var index_44 = require("../ref/index");
|
|
|
99
99
|
Object.defineProperty(exports, "Ref", { enumerable: true, get: function () { return index_44.Ref; } });
|
|
100
100
|
var index_45 = require("../regexp/index");
|
|
101
101
|
Object.defineProperty(exports, "RegExp", { enumerable: true, get: function () { return index_45.RegExp; } });
|
|
102
|
-
var index_46 = require("../
|
|
103
|
-
Object.defineProperty(exports, "
|
|
104
|
-
var index_47 = require("../
|
|
105
|
-
Object.defineProperty(exports, "
|
|
106
|
-
var index_48 = require("../
|
|
107
|
-
Object.defineProperty(exports, "
|
|
108
|
-
var index_49 = require("../
|
|
109
|
-
Object.defineProperty(exports, "
|
|
110
|
-
var index_50 = require("../
|
|
111
|
-
Object.defineProperty(exports, "
|
|
112
|
-
var index_51 = require("../
|
|
113
|
-
Object.defineProperty(exports, "
|
|
114
|
-
var index_52 = require("../
|
|
115
|
-
Object.defineProperty(exports, "
|
|
116
|
-
var index_53 = require("../
|
|
117
|
-
Object.defineProperty(exports, "
|
|
118
|
-
var index_54 = require("../
|
|
119
|
-
Object.defineProperty(exports, "
|
|
120
|
-
var index_55 = require("../
|
|
121
|
-
Object.defineProperty(exports, "
|
|
122
|
-
var index_56 = require("../
|
|
123
|
-
Object.defineProperty(exports, "
|
|
124
|
-
var index_57 = require("../
|
|
125
|
-
Object.defineProperty(exports, "
|
|
126
|
-
var index_58 = require("../
|
|
127
|
-
Object.defineProperty(exports, "
|
|
128
|
-
var index_59 = require("../
|
|
129
|
-
Object.defineProperty(exports, "
|
|
130
|
-
var index_60 = require("../void/index");
|
|
131
|
-
Object.defineProperty(exports, "Void", { enumerable: true, get: function () { return index_60.Void; } });
|
|
102
|
+
var index_46 = require("../required/index");
|
|
103
|
+
Object.defineProperty(exports, "Required", { enumerable: true, get: function () { return index_46.Required; } });
|
|
104
|
+
var index_47 = require("../rest/index");
|
|
105
|
+
Object.defineProperty(exports, "Rest", { enumerable: true, get: function () { return index_47.Rest; } });
|
|
106
|
+
var index_48 = require("../return-type/index");
|
|
107
|
+
Object.defineProperty(exports, "ReturnType", { enumerable: true, get: function () { return index_48.ReturnType; } });
|
|
108
|
+
var index_49 = require("../string/index");
|
|
109
|
+
Object.defineProperty(exports, "String", { enumerable: true, get: function () { return index_49.String; } });
|
|
110
|
+
var index_50 = require("../symbol/index");
|
|
111
|
+
Object.defineProperty(exports, "Symbol", { enumerable: true, get: function () { return index_50.Symbol; } });
|
|
112
|
+
var index_51 = require("../template-literal/index");
|
|
113
|
+
Object.defineProperty(exports, "TemplateLiteral", { enumerable: true, get: function () { return index_51.TemplateLiteral; } });
|
|
114
|
+
var index_52 = require("../transform/index");
|
|
115
|
+
Object.defineProperty(exports, "Transform", { enumerable: true, get: function () { return index_52.Transform; } });
|
|
116
|
+
var index_53 = require("../tuple/index");
|
|
117
|
+
Object.defineProperty(exports, "Tuple", { enumerable: true, get: function () { return index_53.Tuple; } });
|
|
118
|
+
var index_54 = require("../uint8array/index");
|
|
119
|
+
Object.defineProperty(exports, "Uint8Array", { enumerable: true, get: function () { return index_54.Uint8Array; } });
|
|
120
|
+
var index_55 = require("../undefined/index");
|
|
121
|
+
Object.defineProperty(exports, "Undefined", { enumerable: true, get: function () { return index_55.Undefined; } });
|
|
122
|
+
var index_56 = require("../union/index");
|
|
123
|
+
Object.defineProperty(exports, "Union", { enumerable: true, get: function () { return index_56.Union; } });
|
|
124
|
+
var index_57 = require("../unknown/index");
|
|
125
|
+
Object.defineProperty(exports, "Unknown", { enumerable: true, get: function () { return index_57.Unknown; } });
|
|
126
|
+
var index_58 = require("../unsafe/index");
|
|
127
|
+
Object.defineProperty(exports, "Unsafe", { enumerable: true, get: function () { return index_58.Unsafe; } });
|
|
128
|
+
var index_59 = require("../void/index");
|
|
129
|
+
Object.defineProperty(exports, "Void", { enumerable: true, get: function () { return index_59.Void; } });
|
package/build/esm/index.d.mts
CHANGED
|
@@ -52,7 +52,6 @@ export * from './type/record/index.mjs';
|
|
|
52
52
|
export * from './type/recursive/index.mjs';
|
|
53
53
|
export * from './type/ref/index.mjs';
|
|
54
54
|
export * from './type/regexp/index.mjs';
|
|
55
|
-
export * from './type/remap/index.mjs';
|
|
56
55
|
export * from './type/required/index.mjs';
|
|
57
56
|
export * from './type/rest/index.mjs';
|
|
58
57
|
export * from './type/return-type/index.mjs';
|
package/build/esm/index.mjs
CHANGED
|
@@ -58,7 +58,6 @@ export * from './type/record/index.mjs';
|
|
|
58
58
|
export * from './type/recursive/index.mjs';
|
|
59
59
|
export * from './type/ref/index.mjs';
|
|
60
60
|
export * from './type/regexp/index.mjs';
|
|
61
|
-
export * from './type/remap/index.mjs';
|
|
62
61
|
export * from './type/required/index.mjs';
|
|
63
62
|
export * from './type/rest/index.mjs';
|
|
64
63
|
export * from './type/return-type/index.mjs';
|
|
@@ -39,10 +39,17 @@ const Dereference = (context, key) => {
|
|
|
39
39
|
// GenericReference
|
|
40
40
|
// ------------------------------------------------------------------
|
|
41
41
|
function GenericReferenceMapping(results, context) {
|
|
42
|
-
const
|
|
43
|
-
|
|
42
|
+
const type = Dereference(context, results[0]);
|
|
43
|
+
const args = results[2];
|
|
44
|
+
return t.Instantiate(type, args);
|
|
44
45
|
}
|
|
45
|
-
|
|
46
|
+
// prettier-ignore
|
|
47
|
+
const GenericReference = Runtime.Tuple([
|
|
48
|
+
Runtime.Ident(),
|
|
49
|
+
Runtime.Const(LAngle),
|
|
50
|
+
Runtime.Ref('Elements'),
|
|
51
|
+
Runtime.Const(RAngle)
|
|
52
|
+
], (results, context) => GenericReferenceMapping(results, context));
|
|
46
53
|
// ------------------------------------------------------------------
|
|
47
54
|
// Reference
|
|
48
55
|
// ------------------------------------------------------------------
|
|
@@ -43,7 +43,7 @@ type Delimit<Parser extends Static.IParser, Delimiter extends Static.IParser> =
|
|
|
43
43
|
], DelimitMapping>);
|
|
44
44
|
type Dereference<Context extends t.TProperties, Ref extends string> = (Ref extends keyof Context ? Context[Ref] : t.TRef<Ref>);
|
|
45
45
|
interface GenericReferenceMapping extends Static.IMapping {
|
|
46
|
-
output: this['context'] extends t.TProperties ? this['input'] extends [infer Reference extends string, LAngle, infer
|
|
46
|
+
output: this['context'] extends t.TProperties ? this['input'] extends [infer Reference extends string, LAngle, infer Args extends t.TSchema[], RAngle] ? t.TInstantiate<Dereference<this['context'], Reference>, Args> : never : never;
|
|
47
47
|
}
|
|
48
48
|
type GenericReference = Static.Tuple<[
|
|
49
49
|
Static.Ident,
|
|
@@ -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
|
}
|
|
@@ -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';
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
4
|
-
if (k2 === undefined) k2 = k;
|
|
5
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
6
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
7
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
8
|
-
}
|
|
9
|
-
Object.defineProperty(o, k2, desc);
|
|
10
|
-
}) : (function(o, m, k, k2) {
|
|
11
|
-
if (k2 === undefined) k2 = k;
|
|
12
|
-
o[k2] = m[k];
|
|
13
|
-
}));
|
|
14
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
15
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
16
|
-
};
|
|
17
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
-
__exportStar(require("./remap"), exports);
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import { TSchema } from '../schema/index';
|
|
2
|
-
import { TObject, TProperties } from '../object/index';
|
|
3
|
-
import { TConstructor } from '../constructor/index';
|
|
4
|
-
import { TFunction } from '../function/index';
|
|
5
|
-
import { TIntersect } from '../intersect/index';
|
|
6
|
-
import { TUnion } from '../union/index';
|
|
7
|
-
import { TTuple } from '../tuple/index';
|
|
8
|
-
import { TArray } from '../array/index';
|
|
9
|
-
import { TAsyncIterator } from '../async-iterator/index';
|
|
10
|
-
import { TIterator } from '../iterator/index';
|
|
11
|
-
import { TPromise } from '../promise/index';
|
|
12
|
-
import { TRecord, TRecordOrObject } from '../record/index';
|
|
13
|
-
export type TCallback = (schema: TSchema) => TSchema;
|
|
14
|
-
export interface TMapping {
|
|
15
|
-
input: unknown;
|
|
16
|
-
output: unknown;
|
|
17
|
-
}
|
|
18
|
-
type TApply<Type extends TSchema, Mapping extends TMapping, Mapped = (Mapping & {
|
|
19
|
-
input: Type;
|
|
20
|
-
})['output'], Result = Mapped extends TSchema ? Mapped : never> = Result;
|
|
21
|
-
type TFromProperties<Properties extends TProperties, Mapping extends TMapping, Result extends TProperties = {
|
|
22
|
-
[Key in keyof Properties]: TRemap<Properties[Key], Mapping>;
|
|
23
|
-
}> = Result;
|
|
24
|
-
type TFromTypes<Types extends TSchema[], Mapping extends TMapping, Result extends TSchema[] = []> = (Types extends [infer Left extends TSchema, ...infer Right extends TSchema[]] ? TFromTypes<Right, Mapping, [...Result, TRemap<Left, Mapping>]> : Result);
|
|
25
|
-
type TFromType<Type extends TSchema, Mapping extends TMapping, Result extends TSchema = (TApply<Type, Mapping>)> = Result;
|
|
26
|
-
/** `[Internal]` Applies a recursive conditional remapping of a type and its sub type constituents */
|
|
27
|
-
export type TRemap<Type extends TSchema, Mapping extends TMapping, Mapped extends TSchema = TFromType<Type, Mapping>, Result extends TSchema = (Mapped extends TConstructor<infer Parameters extends TSchema[], infer ReturnType extends TSchema> ? TConstructor<TFromTypes<Parameters, Mapping>, TFromType<ReturnType, Mapping>> : Mapped extends TFunction<infer Parameters extends TSchema[], infer ReturnType extends TSchema> ? TFunction<TFromTypes<Parameters, Mapping>, TFromType<ReturnType, Mapping>> : Mapped extends TIntersect<infer Types extends TSchema[]> ? TIntersect<TFromTypes<Types, Mapping>> : Mapped extends TUnion<infer Types extends TSchema[]> ? TUnion<TFromTypes<Types, Mapping>> : Mapped extends TTuple<infer Types extends TSchema[]> ? TTuple<TFromTypes<Types, Mapping>> : Mapped extends TArray<infer Type extends TSchema> ? TArray<TFromType<Type, Mapping>> : Mapped extends TAsyncIterator<infer Type extends TSchema> ? TAsyncIterator<TFromType<Type, Mapping>> : Mapped extends TIterator<infer Type extends TSchema> ? TIterator<TFromType<Type, Mapping>> : Mapped extends TPromise<infer Type extends TSchema> ? TPromise<TFromType<Type, Mapping>> : Mapped extends TObject<infer Properties extends TProperties> ? TObject<TFromProperties<Properties, Mapping>> : Mapped extends TRecord<infer Key extends TSchema, infer Value extends TSchema> ? TRecordOrObject<TFromType<Key, Mapping>, TFromType<Value, Mapping>> : Mapped)> = Result;
|
|
28
|
-
/** `[Internal]` Applies a recursive conditional remapping of a type and its sub type constituents */
|
|
29
|
-
export declare function Remap(type: TSchema, callback: TCallback): TSchema;
|
|
30
|
-
export {};
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
exports.Remap = Remap;
|
|
5
|
-
const index_1 = require("../object/index");
|
|
6
|
-
const index_2 = require("../constructor/index");
|
|
7
|
-
const index_3 = require("../function/index");
|
|
8
|
-
const index_4 = require("../intersect/index");
|
|
9
|
-
const index_5 = require("../union/index");
|
|
10
|
-
const index_6 = require("../tuple/index");
|
|
11
|
-
const index_7 = require("../array/index");
|
|
12
|
-
const index_8 = require("../async-iterator/index");
|
|
13
|
-
const index_9 = require("../iterator/index");
|
|
14
|
-
const index_10 = require("../promise/index");
|
|
15
|
-
const index_11 = require("../record/index");
|
|
16
|
-
const KindGuard = require("../guard/kind");
|
|
17
|
-
const type_1 = require("../clone/type");
|
|
18
|
-
function FromProperties(properties, func) {
|
|
19
|
-
return globalThis.Object.getOwnPropertyNames(properties).reduce((result, key) => {
|
|
20
|
-
return { ...result, [key]: Remap(properties[key], func) };
|
|
21
|
-
}, {});
|
|
22
|
-
}
|
|
23
|
-
function FromTypes(types, callback) {
|
|
24
|
-
return types.map((type) => Remap(type, callback));
|
|
25
|
-
}
|
|
26
|
-
function FromType(type, callback) {
|
|
27
|
-
return callback(type);
|
|
28
|
-
}
|
|
29
|
-
/** `[Internal]` Applies a recursive conditional remapping of a type and its sub type constituents */
|
|
30
|
-
// prettier-ignore
|
|
31
|
-
function Remap(type, callback) {
|
|
32
|
-
// Map incoming type
|
|
33
|
-
const mapped = (0, type_1.CloneType)(FromType(type, callback));
|
|
34
|
-
// Return remapped interior
|
|
35
|
-
return (KindGuard.IsConstructor(type) ? (0, index_2.Constructor)(FromTypes(type.parameters, callback), FromType(type.returns, callback), mapped) :
|
|
36
|
-
KindGuard.IsFunction(type) ? (0, index_3.Function)(FromTypes(type.parameters, callback), FromType(type.returns, callback), mapped) :
|
|
37
|
-
KindGuard.IsIntersect(type) ? (0, index_4.Intersect)(FromTypes(type.allOf, callback), mapped) :
|
|
38
|
-
KindGuard.IsUnion(type) ? (0, index_5.Union)(FromTypes(type.anyOf, callback), mapped) :
|
|
39
|
-
KindGuard.IsTuple(type) ? (0, index_6.Tuple)(FromTypes(type.items || [], callback), mapped) :
|
|
40
|
-
KindGuard.IsArray(type) ? (0, index_7.Array)(FromType(type.items, callback), mapped) :
|
|
41
|
-
KindGuard.IsAsyncIterator(type) ? (0, index_8.AsyncIterator)(FromType(type.items, callback), mapped) :
|
|
42
|
-
KindGuard.IsIterator(type) ? (0, index_9.Iterator)(FromType(type.items, callback), mapped) :
|
|
43
|
-
KindGuard.IsPromise(type) ? (0, index_10.Promise)(FromType(type.items, callback), mapped) :
|
|
44
|
-
KindGuard.IsObject(type) ? (0, index_1.Object)(FromProperties(type.properties, callback), mapped) :
|
|
45
|
-
KindGuard.IsRecord(type) ? (0, index_11.Record)(FromType((0, index_11.RecordKey)(type), callback), FromType((0, index_11.RecordValue)(type), callback), mapped) :
|
|
46
|
-
(0, type_1.CloneType)(mapped));
|
|
47
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './remap.mjs';
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './remap.mjs';
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import { TSchema } from '../schema/index.mjs';
|
|
2
|
-
import { TObject, TProperties } from '../object/index.mjs';
|
|
3
|
-
import { TConstructor } from '../constructor/index.mjs';
|
|
4
|
-
import { TFunction } from '../function/index.mjs';
|
|
5
|
-
import { TIntersect } from '../intersect/index.mjs';
|
|
6
|
-
import { TUnion } from '../union/index.mjs';
|
|
7
|
-
import { TTuple } from '../tuple/index.mjs';
|
|
8
|
-
import { TArray } from '../array/index.mjs';
|
|
9
|
-
import { TAsyncIterator } from '../async-iterator/index.mjs';
|
|
10
|
-
import { TIterator } from '../iterator/index.mjs';
|
|
11
|
-
import { TPromise } from '../promise/index.mjs';
|
|
12
|
-
import { TRecord, TRecordOrObject } from '../record/index.mjs';
|
|
13
|
-
export type TCallback = (schema: TSchema) => TSchema;
|
|
14
|
-
export interface TMapping {
|
|
15
|
-
input: unknown;
|
|
16
|
-
output: unknown;
|
|
17
|
-
}
|
|
18
|
-
type TApply<Type extends TSchema, Mapping extends TMapping, Mapped = (Mapping & {
|
|
19
|
-
input: Type;
|
|
20
|
-
})['output'], Result = Mapped extends TSchema ? Mapped : never> = Result;
|
|
21
|
-
type TFromProperties<Properties extends TProperties, Mapping extends TMapping, Result extends TProperties = {
|
|
22
|
-
[Key in keyof Properties]: TRemap<Properties[Key], Mapping>;
|
|
23
|
-
}> = Result;
|
|
24
|
-
type TFromTypes<Types extends TSchema[], Mapping extends TMapping, Result extends TSchema[] = []> = (Types extends [infer Left extends TSchema, ...infer Right extends TSchema[]] ? TFromTypes<Right, Mapping, [...Result, TRemap<Left, Mapping>]> : Result);
|
|
25
|
-
type TFromType<Type extends TSchema, Mapping extends TMapping, Result extends TSchema = (TApply<Type, Mapping>)> = Result;
|
|
26
|
-
/** `[Internal]` Applies a recursive conditional remapping of a type and its sub type constituents */
|
|
27
|
-
export type TRemap<Type extends TSchema, Mapping extends TMapping, Mapped extends TSchema = TFromType<Type, Mapping>, Result extends TSchema = (Mapped extends TConstructor<infer Parameters extends TSchema[], infer ReturnType extends TSchema> ? TConstructor<TFromTypes<Parameters, Mapping>, TFromType<ReturnType, Mapping>> : Mapped extends TFunction<infer Parameters extends TSchema[], infer ReturnType extends TSchema> ? TFunction<TFromTypes<Parameters, Mapping>, TFromType<ReturnType, Mapping>> : Mapped extends TIntersect<infer Types extends TSchema[]> ? TIntersect<TFromTypes<Types, Mapping>> : Mapped extends TUnion<infer Types extends TSchema[]> ? TUnion<TFromTypes<Types, Mapping>> : Mapped extends TTuple<infer Types extends TSchema[]> ? TTuple<TFromTypes<Types, Mapping>> : Mapped extends TArray<infer Type extends TSchema> ? TArray<TFromType<Type, Mapping>> : Mapped extends TAsyncIterator<infer Type extends TSchema> ? TAsyncIterator<TFromType<Type, Mapping>> : Mapped extends TIterator<infer Type extends TSchema> ? TIterator<TFromType<Type, Mapping>> : Mapped extends TPromise<infer Type extends TSchema> ? TPromise<TFromType<Type, Mapping>> : Mapped extends TObject<infer Properties extends TProperties> ? TObject<TFromProperties<Properties, Mapping>> : Mapped extends TRecord<infer Key extends TSchema, infer Value extends TSchema> ? TRecordOrObject<TFromType<Key, Mapping>, TFromType<Value, Mapping>> : Mapped)> = Result;
|
|
28
|
-
/** `[Internal]` Applies a recursive conditional remapping of a type and its sub type constituents */
|
|
29
|
-
export declare function Remap(type: TSchema, callback: TCallback): TSchema;
|
|
30
|
-
export {};
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import { Object } from '../object/index.mjs';
|
|
2
|
-
import { Constructor } from '../constructor/index.mjs';
|
|
3
|
-
import { Function } from '../function/index.mjs';
|
|
4
|
-
import { Intersect } from '../intersect/index.mjs';
|
|
5
|
-
import { Union } from '../union/index.mjs';
|
|
6
|
-
import { Tuple } from '../tuple/index.mjs';
|
|
7
|
-
import { Array } from '../array/index.mjs';
|
|
8
|
-
import { AsyncIterator } from '../async-iterator/index.mjs';
|
|
9
|
-
import { Iterator } from '../iterator/index.mjs';
|
|
10
|
-
import { Promise } from '../promise/index.mjs';
|
|
11
|
-
import { Record, RecordKey, RecordValue } from '../record/index.mjs';
|
|
12
|
-
import * as KindGuard from '../guard/kind.mjs';
|
|
13
|
-
import { CloneType } from '../clone/type.mjs';
|
|
14
|
-
function FromProperties(properties, func) {
|
|
15
|
-
return globalThis.Object.getOwnPropertyNames(properties).reduce((result, key) => {
|
|
16
|
-
return { ...result, [key]: Remap(properties[key], func) };
|
|
17
|
-
}, {});
|
|
18
|
-
}
|
|
19
|
-
function FromTypes(types, callback) {
|
|
20
|
-
return types.map((type) => Remap(type, callback));
|
|
21
|
-
}
|
|
22
|
-
function FromType(type, callback) {
|
|
23
|
-
return callback(type);
|
|
24
|
-
}
|
|
25
|
-
/** `[Internal]` Applies a recursive conditional remapping of a type and its sub type constituents */
|
|
26
|
-
// prettier-ignore
|
|
27
|
-
export function Remap(type, callback) {
|
|
28
|
-
// Map incoming type
|
|
29
|
-
const mapped = CloneType(FromType(type, callback));
|
|
30
|
-
// Return remapped interior
|
|
31
|
-
return (KindGuard.IsConstructor(type) ? Constructor(FromTypes(type.parameters, callback), FromType(type.returns, callback), mapped) :
|
|
32
|
-
KindGuard.IsFunction(type) ? Function(FromTypes(type.parameters, callback), FromType(type.returns, callback), mapped) :
|
|
33
|
-
KindGuard.IsIntersect(type) ? Intersect(FromTypes(type.allOf, callback), mapped) :
|
|
34
|
-
KindGuard.IsUnion(type) ? Union(FromTypes(type.anyOf, callback), mapped) :
|
|
35
|
-
KindGuard.IsTuple(type) ? Tuple(FromTypes(type.items || [], callback), mapped) :
|
|
36
|
-
KindGuard.IsArray(type) ? Array(FromType(type.items, callback), mapped) :
|
|
37
|
-
KindGuard.IsAsyncIterator(type) ? AsyncIterator(FromType(type.items, callback), mapped) :
|
|
38
|
-
KindGuard.IsIterator(type) ? Iterator(FromType(type.items, callback), mapped) :
|
|
39
|
-
KindGuard.IsPromise(type) ? Promise(FromType(type.items, callback), mapped) :
|
|
40
|
-
KindGuard.IsObject(type) ? Object(FromProperties(type.properties, callback), mapped) :
|
|
41
|
-
KindGuard.IsRecord(type) ? Record(FromType(RecordKey(type), callback), FromType(RecordValue(type), callback), mapped) :
|
|
42
|
-
CloneType(mapped));
|
|
43
|
-
}
|