typebox 1.1.7 → 1.1.8
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/type/engine/call/distribute-arguments.d.mts +10 -0
- package/build/type/engine/call/distribute-arguments.mjs +27 -0
- package/build/type/engine/call/instantiate.d.mts +9 -6
- package/build/type/engine/call/instantiate.mjs +33 -23
- package/build/type/engine/evaluate/broaden.d.mts +1 -1
- package/build/type/engine/evaluate/broaden.mjs +7 -8
- package/package.json +1 -1
- package/readme.md +15 -45
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { type TSchema } from '../../types/schema.mjs';
|
|
2
|
+
import { type TUnion } from '../../types/union.mjs';
|
|
3
|
+
import { type TDeferred } from '../../types/deferred.mjs';
|
|
4
|
+
type TExpand<Type extends TSchema> = (Type extends TUnion<infer Types extends TSchema[]> ? [...Types] : [Type]);
|
|
5
|
+
type TAppend<Current extends TSchema[][], Type extends TSchema, Result extends TSchema[][] = []> = (Current extends [infer Left extends TSchema[], ...infer Right extends TSchema[][]] ? TAppend<Right, Type, [...Result, [...Left, Type]]> : Result);
|
|
6
|
+
type TCross<Current extends TSchema[][], Variants extends TSchema[], Result extends TSchema[][] = []> = (Variants extends [infer Left extends TSchema, ...infer Right extends TSchema[]] ? TCross<Current, Right, [...Result, ...TAppend<Current, Left>]> : Result);
|
|
7
|
+
type TDistribute<Types extends TSchema[], Result extends TSchema[][] = [[]]> = (Types extends [infer Left extends TSchema, ...infer Right extends TSchema[]] ? TDistribute<Right, TCross<Result, TExpand<Left>>> : Result);
|
|
8
|
+
export type TDistributeArguments<Arguments extends TSchema[], Expression extends TSchema, Result extends TSchema[][] = Expression extends TDeferred<'Conditional', TSchema[]> ? TDistribute<Arguments> : [Arguments]> = Result;
|
|
9
|
+
export declare function DistributedArguments<Arguments extends TSchema[], Expression extends TSchema>(arguments_: [...Arguments], expression: Expression): TDistributeArguments<Arguments, Expression>;
|
|
10
|
+
export {};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// deno-fmt-ignore-file
|
|
2
|
+
import { Guard } from '../../../guard/index.mjs';
|
|
3
|
+
import { IsUnion } from '../../types/union.mjs';
|
|
4
|
+
import { IsDeferred } from '../../types/deferred.mjs';
|
|
5
|
+
function Expand(type) {
|
|
6
|
+
return (IsUnion(type)
|
|
7
|
+
? [...type.anyOf]
|
|
8
|
+
: [type]);
|
|
9
|
+
}
|
|
10
|
+
function Append(current, type) {
|
|
11
|
+
return current.reduce((result, left) => [...result, [...left, type]], []);
|
|
12
|
+
}
|
|
13
|
+
function Cross(current, variants) {
|
|
14
|
+
return variants.reduce((result, left) => {
|
|
15
|
+
return [...result, ...Append(current, left)];
|
|
16
|
+
}, []);
|
|
17
|
+
}
|
|
18
|
+
function Distribute(types) {
|
|
19
|
+
return types.reduce((result, type) => {
|
|
20
|
+
return Cross(result, Expand(type));
|
|
21
|
+
}, [[]]);
|
|
22
|
+
}
|
|
23
|
+
export function DistributedArguments(arguments_, expression) {
|
|
24
|
+
return (IsDeferred(expression) && Guard.IsEqual(expression.action, 'Conditional')
|
|
25
|
+
? Distribute(arguments_)
|
|
26
|
+
: [arguments_]);
|
|
27
|
+
}
|
|
@@ -4,17 +4,20 @@ import { type TCallConstruct } from '../../types/call.mjs';
|
|
|
4
4
|
import { type TRef } from '../../types/ref.mjs';
|
|
5
5
|
import { type TGeneric } from '../../types/generic.mjs';
|
|
6
6
|
import { type TProperties } from '../../types/properties.mjs';
|
|
7
|
+
import { type TEvaluateUnion } from '../evaluate/index.mjs';
|
|
7
8
|
import { type TState } from '../instantiate.mjs';
|
|
8
9
|
import { type TInstantiateType } from '../instantiate.mjs';
|
|
9
10
|
import { type TInstantiateTypes } from '../instantiate.mjs';
|
|
11
|
+
import { type TDistributeArguments } from './distribute-arguments.mjs';
|
|
10
12
|
import { type TResolveTarget } from './resolve-target.mjs';
|
|
11
13
|
import { type TResolveArgumentsContext } from './resolve-arguments.mjs';
|
|
12
|
-
type TPeek<
|
|
13
|
-
type
|
|
14
|
-
type
|
|
15
|
-
|
|
16
|
-
callstack: [...State['callstack'], Name];
|
|
14
|
+
type TPeek<State extends TState, Result extends string = State['callstack'] extends [...infer _ extends string[], infer Top extends string] ? Top : ''> = Result;
|
|
15
|
+
type TIsTailCall<State extends TState, Name extends string, Result extends boolean = TPeek<State> extends Name ? true : false> = Result;
|
|
16
|
+
type TCallDispatch<Context extends TProperties, State extends TState, Target extends TRef, Parameters extends TParameter[], Expression extends TSchema, Arguments extends TSchema[], ArgumentsContext extends TProperties = TResolveArgumentsContext<Context, State, Parameters, Arguments>, ReturnType extends TSchema = TInstantiateType<ArgumentsContext, {
|
|
17
|
+
callstack: [...State['callstack'], Target['$ref']];
|
|
17
18
|
}, Expression>> = TInstantiateType<Context, State, ReturnType>;
|
|
18
|
-
|
|
19
|
+
type TCallDistributed<Context extends TProperties, State extends TState, Target extends TRef, Parameters extends TParameter[], Expression extends TSchema, DistributedArguments extends TSchema[][], Result extends TSchema[] = []> = (DistributedArguments extends [infer Arguments extends TSchema[], ...infer DistributedArguments extends TSchema[][]] ? TCallDispatch<Context, State, Target, Parameters, Expression, Arguments> extends infer ReturnType extends TSchema ? TCallDistributed<Context, State, Target, Parameters, Expression, DistributedArguments, [...Result, ReturnType]> : never : Result);
|
|
20
|
+
type TCallImmediate<Context extends TProperties, State extends TState, Target extends TRef, Parameters extends TParameter[], Expression extends TSchema, InstantiatedArguments extends TSchema[], DistributedArguments extends TSchema[][] = TDistributeArguments<InstantiatedArguments, Expression>, ReturnTypes extends TSchema[] = TCallDistributed<Context, State, Target, Parameters, Expression, DistributedArguments>, Result extends TSchema = ReturnTypes['length'] extends 1 ? ReturnTypes[0] : TEvaluateUnion<ReturnTypes>> = Result;
|
|
21
|
+
export type TCallInstantiate<Context extends TProperties, State extends TState, Target extends TSchema, Arguments extends TSchema[], InstantiatedArguments extends TSchema[] = TInstantiateTypes<Context, State, Arguments>, Resolved extends [string, TSchema] = TResolveTarget<Context, Target, Arguments>, Name extends string = Resolved[0], Type extends TSchema = Resolved[1], Result extends TSchema = (Type extends TGeneric<infer Parameters extends TParameter[], infer Expression extends TSchema> ? TIsTailCall<State, Name> extends true ? TCallConstruct<TRef<Name>, InstantiatedArguments> : TCallImmediate<Context, State, TRef<Name>, Parameters, Expression, InstantiatedArguments> : TCallConstruct<Target, InstantiatedArguments>)> = Result;
|
|
19
22
|
export declare function CallInstantiate<Context extends TProperties, State extends TState, Target extends TSchema, Arguments extends TSchema[]>(context: Context, state: State, target: Target, arguments_: [...Arguments]): TCallInstantiate<Context, State, Target, Arguments>;
|
|
20
23
|
export {};
|
|
@@ -3,36 +3,46 @@ import { Guard } from '../../../guard/index.mjs';
|
|
|
3
3
|
import { CallConstruct } from '../../types/call.mjs';
|
|
4
4
|
import { Ref } from '../../types/ref.mjs';
|
|
5
5
|
import { IsGeneric } from '../../types/generic.mjs';
|
|
6
|
+
import { EvaluateUnion } from '../evaluate/index.mjs';
|
|
6
7
|
import { InstantiateType } from '../instantiate.mjs';
|
|
7
8
|
import { InstantiateTypes } from '../instantiate.mjs';
|
|
8
|
-
//
|
|
9
|
-
//
|
|
10
|
-
//
|
|
9
|
+
// ------------------------------------------------------------------
|
|
10
|
+
// Infrastructure
|
|
11
|
+
// ------------------------------------------------------------------
|
|
12
|
+
import { DistributedArguments } from './distribute-arguments.mjs';
|
|
11
13
|
import { ResolveTarget } from './resolve-target.mjs';
|
|
12
14
|
import { ResolveArgumentsContext } from './resolve-arguments.mjs';
|
|
13
|
-
function Peek(
|
|
14
|
-
|
|
15
|
+
function Peek(state) {
|
|
16
|
+
const result = Guard.IsGreaterThan(state.callstack.length, 0) ? state.callstack[state.callstack.length - 1] : '';
|
|
17
|
+
return result;
|
|
15
18
|
}
|
|
16
|
-
function
|
|
17
|
-
const
|
|
18
|
-
|
|
19
|
-
return deferredCall;
|
|
20
|
-
}
|
|
21
|
-
function TailCall(context, state, name, arguments_) {
|
|
22
|
-
const deferredCall = DeferredCall(context, state, Ref(name), arguments_);
|
|
23
|
-
return deferredCall;
|
|
19
|
+
function IsTailCall(state, name) {
|
|
20
|
+
const result = Guard.IsEqual(Peek(state), name);
|
|
21
|
+
return result;
|
|
24
22
|
}
|
|
25
|
-
function
|
|
26
|
-
const
|
|
27
|
-
const
|
|
28
|
-
const returnType = InstantiateType(argumentsContext, { callstack: [...state.callstack, name] }, expression);
|
|
23
|
+
function CallDispatch(context, state, target, parameters, expression, arguments_) {
|
|
24
|
+
const argumentsContext = ResolveArgumentsContext(context, state, parameters, arguments_);
|
|
25
|
+
const returnType = InstantiateType(argumentsContext, { callstack: [...state.callstack, target.$ref] }, expression);
|
|
29
26
|
return InstantiateType(context, state, returnType);
|
|
30
27
|
}
|
|
28
|
+
function CallDistributed(context, state, target, parameters, expression, distributedArguments) {
|
|
29
|
+
return distributedArguments.reduce((result, arguments_) => [...result, CallDispatch(context, state, target, parameters, expression, arguments_)], []);
|
|
30
|
+
}
|
|
31
|
+
function CallImmediate(context, state, target, parameters, expression, arguments_) {
|
|
32
|
+
const distributedArguments = DistributedArguments(arguments_, expression);
|
|
33
|
+
const returnTypes = CallDistributed(context, state, target, parameters, expression, distributedArguments);
|
|
34
|
+
const result = returnTypes.length === 1 ? returnTypes[0] : EvaluateUnion(returnTypes);
|
|
35
|
+
return result;
|
|
36
|
+
}
|
|
31
37
|
export function CallInstantiate(context, state, target, arguments_) {
|
|
32
|
-
const
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
+
const instantiatedArguments = InstantiateTypes(context, state, arguments_);
|
|
39
|
+
const resolved = ResolveTarget(context, target, arguments_);
|
|
40
|
+
const name = resolved[0];
|
|
41
|
+
const type = resolved[1];
|
|
42
|
+
const result = (IsGeneric(type)
|
|
43
|
+
? IsTailCall(state, name)
|
|
44
|
+
? CallConstruct(Ref(name), instantiatedArguments)
|
|
45
|
+
: CallImmediate(context, state, Ref(name), type.parameters, type.expression, instantiatedArguments)
|
|
46
|
+
: CallConstruct(target, instantiatedArguments));
|
|
47
|
+
return result;
|
|
38
48
|
}
|
|
@@ -9,7 +9,7 @@ import { type TEvaluateType } from './evaluate.mjs';
|
|
|
9
9
|
type TBroadFilter<Type extends TSchema, Types extends TSchema[], Result extends TSchema[] = []> = (Types extends [infer Left extends TSchema, ...infer Right extends TSchema[]] ? TCompare<Type, Left> extends typeof ResultRightInside ? TBroadFilter<Type, Right, [...Result]> : TBroadFilter<Type, Right, [...Result, Left]> : Result);
|
|
10
10
|
type TIsBroadestType<Type extends TSchema, Types extends TSchema[]> = (Types extends [infer Left extends TSchema, ...infer Right extends TSchema[]] ? TCompare<Type, Left> extends typeof ResultLeftInside | typeof ResultEqual ? false : TIsBroadestType<Type, Right> : true);
|
|
11
11
|
type TBroadenType<Type extends TSchema, Types extends TSchema[], Evaluated extends TSchema = TEvaluateType<Type>, Result extends TSchema[] = (Evaluated extends TAny ? [Evaluated] : TIsBroadestType<Evaluated, Types> extends true ? [...TBroadFilter<Evaluated, Types>, Evaluated] : Types)> = Result;
|
|
12
|
-
type TBroadenTypes<Types extends TSchema[], Result extends TSchema[] = []> = (Types extends [infer Left extends TSchema, ...infer Right extends TSchema[]] ? Left extends TObject ? TBroadenTypes<Right, [...Result, Left]> : TBroadenTypes<Right, TBroadenType<Left, Result>> : Result);
|
|
12
|
+
type TBroadenTypes<Types extends TSchema[], Result extends TSchema[] = []> = (Types extends [infer Left extends TSchema, ...infer Right extends TSchema[]] ? (Left extends TObject ? TBroadenTypes<Right, [...Result, Left]> : Left extends TNever ? TBroadenTypes<Right, Result> : TBroadenTypes<Right, TBroadenType<Left, Result>>) : Result);
|
|
13
13
|
export type TBroaden<Types extends TSchema[], Broadened extends TSchema[] = TBroadenTypes<Types>, Flattened extends TSchema[] = TFlatten<Broadened>, Result extends TSchema = (Flattened extends [] ? TNever : Flattened extends [infer Type extends TSchema] ? Type : TUnion<Flattened>)> = Result;
|
|
14
14
|
/** Broadens a set of types and returns either the most broad type, or union or disjoint types. */
|
|
15
15
|
export declare function Broaden<Types extends TSchema[]>(types: [...Types]): TBroaden<Types>;
|
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
// deno-fmt-ignore-file
|
|
2
2
|
import { Guard } from '../../../guard/index.mjs';
|
|
3
|
-
import { IsSchema } from '../../types/schema.mjs';
|
|
4
3
|
import { IsAny } from '../../types/any.mjs';
|
|
5
|
-
import { Never } from '../../types/never.mjs';
|
|
4
|
+
import { Never, IsNever } from '../../types/never.mjs';
|
|
6
5
|
import { IsObject } from '../../types/object.mjs';
|
|
7
6
|
import { Union } from '../../types/union.mjs';
|
|
8
7
|
import { Compare, ResultRightInside, ResultLeftInside, ResultEqual } from './compare.mjs';
|
|
@@ -30,12 +29,12 @@ function BroadenType(type, types) {
|
|
|
30
29
|
: types);
|
|
31
30
|
}
|
|
32
31
|
function BroadenTypes(types, result = []) {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
32
|
+
return types.reduce((result, left) => {
|
|
33
|
+
return (IsObject(left) ? [...result, left] : // push
|
|
34
|
+
IsNever(left) ? result : // ignore
|
|
35
|
+
BroadenType(left, result) // broaden
|
|
36
|
+
);
|
|
37
|
+
}, []);
|
|
39
38
|
}
|
|
40
39
|
/** Broadens a set of types and returns either the most broad type, or union or disjoint types. */
|
|
41
40
|
export function Broaden(types) {
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -57,12 +57,11 @@ License: MIT
|
|
|
57
57
|
|
|
58
58
|
## Contents
|
|
59
59
|
|
|
60
|
-
|
|
61
60
|
- [Type](#Type)
|
|
62
61
|
- [Value](#Value)
|
|
63
62
|
- [Script](#Script)
|
|
64
63
|
- [Schema](#Schema)
|
|
65
|
-
- [
|
|
64
|
+
- [Versions](#Versions)
|
|
66
65
|
- [Contribute](#Contribute)
|
|
67
66
|
|
|
68
67
|
|
|
@@ -223,60 +222,31 @@ const R = C.Parse({ x: 0, y: 0, z: 0 }) // const R: {
|
|
|
223
222
|
// } = ...
|
|
224
223
|
```
|
|
225
224
|
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
If upgrading from `@sinclair/typebox` 0.34.x refer to the 1.0 migration guide at the following URL.
|
|
225
|
+
<a name="Versions"></a>
|
|
229
226
|
|
|
230
|
-
|
|
227
|
+
## Versions
|
|
231
228
|
|
|
232
|
-
|
|
229
|
+
TypeBox provides two distinct versions that span two generations of the TypeScript compiler.
|
|
233
230
|
|
|
234
|
-
|
|
235
|
-
import { Type } from '@sinclair/typebox' // TB: 0.34.x
|
|
236
|
-
import { Static } from 'typebox' // TB: 1.0.0
|
|
231
|
+
### Version 0.x
|
|
237
232
|
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
const A = Type.Object({
|
|
242
|
-
x: Type.Number(),
|
|
243
|
-
y: Type.Number(),
|
|
244
|
-
z: Type.Number()
|
|
245
|
-
})
|
|
246
|
-
|
|
247
|
-
const B = Type.Object({
|
|
248
|
-
a: Type.Number(),
|
|
249
|
-
b: Type.Number(),
|
|
250
|
-
c: Type.Number()
|
|
251
|
-
})
|
|
252
|
-
|
|
253
|
-
const C = Type.Composite([A, B])
|
|
233
|
+
```bash
|
|
234
|
+
$ npm install @sinclair/typebox # 0.x - LTS | TS 4-6
|
|
235
|
+
```
|
|
254
236
|
|
|
255
|
-
|
|
256
|
-
// Modern Inference
|
|
257
|
-
// ----------------------------------------------------------
|
|
258
|
-
type C = Static<typeof C> // type C = {
|
|
259
|
-
// x: number;
|
|
260
|
-
// y: number;
|
|
261
|
-
// z: number;
|
|
262
|
-
// a: number;
|
|
263
|
-
// b: number;
|
|
264
|
-
// c: number;
|
|
265
|
-
// }
|
|
237
|
+
Developed against TypeScript 4-6 and maintained under Long Term Support (LTS) for existing infrastructure on the 0.x revision line. ESM and CJS compatible.
|
|
266
238
|
|
|
267
|
-
|
|
268
|
-
// Modern Compile
|
|
269
|
-
// ----------------------------------------------------------
|
|
270
|
-
import Schema from 'typebox/schema'
|
|
239
|
+
### Version 1.x
|
|
271
240
|
|
|
272
|
-
|
|
241
|
+
```bash
|
|
242
|
+
$ npm install typebox # 1.x - Latest | TS 7 Native
|
|
273
243
|
```
|
|
274
244
|
|
|
275
|
-
|
|
245
|
+
Developed against the TypeScript 7 native compiler with advanced type inference and JSON Schema 2020-12 compliant validation, with backwards compatibility for `0.x` types. ESM only.
|
|
276
246
|
|
|
277
|
-
|
|
247
|
+
### Additional
|
|
278
248
|
|
|
279
|
-
|
|
249
|
+
The `1.x` version is recommended for most new projects and is the active development line that targets optimizations enabled by the TypeScript 7 native compiler. The `0.x` version is maintained under LTS for environments requiring CJS and ESM compatibility as well as support for older TypeScript compiler versions. For issues relating to `0.x` please submit them to the [TypeBox 0.x](https://github.com/sinclairzx81/sinclair-typebox) repository.
|
|
280
250
|
|
|
281
251
|
## Contribute
|
|
282
252
|
|