@sinclair/typebox 0.34.23 → 0.34.24
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/syntax/runtime.d.ts +2 -0
- package/build/cjs/syntax/runtime.js +43 -0
- package/build/cjs/syntax/static.d.ts +21 -0
- package/build/cjs/syntax/syntax.d.ts +18 -20
- package/build/cjs/syntax/syntax.js +14 -9
- package/build/cjs/type/instantiate/instantiate.d.ts +3 -3
- package/build/cjs/type/instantiate/instantiate.js +9 -8
- package/build/esm/syntax/runtime.d.mts +2 -0
- package/build/esm/syntax/runtime.mjs +43 -0
- package/build/esm/syntax/static.d.mts +21 -0
- package/build/esm/syntax/syntax.d.mts +18 -20
- package/build/esm/syntax/syntax.mjs +14 -9
- package/build/esm/type/instantiate/instantiate.d.mts +3 -3
- package/build/esm/type/instantiate/instantiate.mjs +5 -4
- package/package.json +1 -1
- package/readme.md +29 -27
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { Runtime } from '../parser/index';
|
|
2
2
|
import * as t from '../type/index';
|
|
3
3
|
export declare const Module: Runtime.Module<{
|
|
4
|
+
GenericArgumentList: Runtime.IUnion<unknown[]>;
|
|
5
|
+
GenericArguments: Runtime.ITuple<{}>;
|
|
4
6
|
Literal: Runtime.IUnion<t.TLiteral<string> | t.TLiteral<number> | t.TLiteral<boolean>>;
|
|
5
7
|
Keyword: Runtime.IUnion<t.TAny | t.TBoolean | t.TBigInt | t.TNever | t.TString | t.TNumber | t.TInteger | t.TNull | t.TSymbol | t.TUndefined | t.TUnknown | t.TVoid>;
|
|
6
8
|
KeyOf: Runtime.IUnion<boolean>;
|
|
@@ -40,6 +40,44 @@ const Dereference = (context, key) => {
|
|
|
40
40
|
return key in context ? context[key] : t.Ref(key);
|
|
41
41
|
};
|
|
42
42
|
// ------------------------------------------------------------------
|
|
43
|
+
// GenericArgumentList
|
|
44
|
+
// ------------------------------------------------------------------
|
|
45
|
+
// prettier-ignore
|
|
46
|
+
const GenericArgumentListMapping = (results) => {
|
|
47
|
+
return (results.length === 3 ? [results[0], ...results[2]] :
|
|
48
|
+
results.length === 2 ? [results[0]] :
|
|
49
|
+
results.length === 1 ? [results[0]] :
|
|
50
|
+
[]);
|
|
51
|
+
};
|
|
52
|
+
// prettier-ignore
|
|
53
|
+
const GenericArgumentList = index_1.Runtime.Union([
|
|
54
|
+
index_1.Runtime.Tuple([index_1.Runtime.Ident(), index_1.Runtime.Const(Comma), index_1.Runtime.Ref('GenericArgumentList')]),
|
|
55
|
+
index_1.Runtime.Tuple([index_1.Runtime.Ident(), index_1.Runtime.Const(Comma)]),
|
|
56
|
+
index_1.Runtime.Tuple([index_1.Runtime.Ident()]),
|
|
57
|
+
index_1.Runtime.Tuple([]),
|
|
58
|
+
], (results) => GenericArgumentListMapping(results));
|
|
59
|
+
// ------------------------------------------------------------------
|
|
60
|
+
// GenericArguments
|
|
61
|
+
// ------------------------------------------------------------------
|
|
62
|
+
// prettier-ignore
|
|
63
|
+
const GenericArgumentsContext = (args) => {
|
|
64
|
+
return args.reduce((result, arg, index) => {
|
|
65
|
+
return { ...result, [arg]: t.Argument(index) };
|
|
66
|
+
}, {});
|
|
67
|
+
};
|
|
68
|
+
// prettier-ignore
|
|
69
|
+
const GenericArgumentsMapping = (results) => {
|
|
70
|
+
return results.length === 3
|
|
71
|
+
? GenericArgumentsContext(results[1])
|
|
72
|
+
: {};
|
|
73
|
+
};
|
|
74
|
+
// prettier-ignore
|
|
75
|
+
const GenericArguments = index_1.Runtime.Tuple([
|
|
76
|
+
index_1.Runtime.Const(LAngle),
|
|
77
|
+
index_1.Runtime.Ref('GenericArgumentList'),
|
|
78
|
+
index_1.Runtime.Const(RAngle),
|
|
79
|
+
], results => GenericArgumentsMapping(results));
|
|
80
|
+
// ------------------------------------------------------------------
|
|
43
81
|
// GenericReference
|
|
44
82
|
// ------------------------------------------------------------------
|
|
45
83
|
function GenericReferenceMapping(results, context) {
|
|
@@ -614,6 +652,11 @@ const Uint8Array = index_1.Runtime.Const('Uint8Array', index_1.Runtime.As(t.Uint
|
|
|
614
652
|
// ------------------------------------------------------------------
|
|
615
653
|
// prettier-ignore
|
|
616
654
|
exports.Module = new index_1.Runtime.Module({
|
|
655
|
+
// ----------------------------------------------------------------
|
|
656
|
+
// Generic Arguments
|
|
657
|
+
// ----------------------------------------------------------------
|
|
658
|
+
GenericArgumentList,
|
|
659
|
+
GenericArguments,
|
|
617
660
|
// ----------------------------------------------------------------
|
|
618
661
|
// Type Expressions
|
|
619
662
|
// ----------------------------------------------------------------
|
|
@@ -42,6 +42,27 @@ type Delimit<Parser extends Static.IParser, Delimiter extends Static.IParser> =
|
|
|
42
42
|
Static.Tuple<[]>
|
|
43
43
|
], DelimitMapping>);
|
|
44
44
|
type Dereference<Context extends t.TProperties, Ref extends string> = (Ref extends keyof Context ? Context[Ref] : t.TRef<Ref>);
|
|
45
|
+
interface GenericArgumentListMapping extends Static.IMapping {
|
|
46
|
+
output: (this['input'] extends [infer Ident extends string, Comma, infer Rest extends unknown[]] ? [Ident, ...Rest] : this['input'] extends [infer Ident extends string, Comma] ? [Ident] : this['input'] extends [infer Ident extends string] ? [Ident] : [
|
|
47
|
+
]);
|
|
48
|
+
}
|
|
49
|
+
type GenericArgumentList = Static.Union<[
|
|
50
|
+
Static.Tuple<[Static.Ident, Static.Const<Comma>, GenericArgumentList]>,
|
|
51
|
+
Static.Tuple<[Static.Ident, Static.Const<Comma>]>,
|
|
52
|
+
Static.Tuple<[Static.Ident]>,
|
|
53
|
+
Static.Tuple<[]>
|
|
54
|
+
], GenericArgumentListMapping>;
|
|
55
|
+
type GenericArgumentsContext<Args extends string[], Result extends t.TProperties = {}> = (Args extends [...infer Left extends string[], infer Right extends string] ? GenericArgumentsContext<Left, Result & {
|
|
56
|
+
[_ in Right]: t.TArgument<Left['length']>;
|
|
57
|
+
}> : t.Evaluate<Result>);
|
|
58
|
+
interface GenericArgumentsMapping extends Static.IMapping {
|
|
59
|
+
output: this['input'] extends [LAngle, infer Args extends string[], RAngle] ? GenericArgumentsContext<Args> : never;
|
|
60
|
+
}
|
|
61
|
+
export type GenericArguments = Static.Tuple<[
|
|
62
|
+
Static.Const<LAngle>,
|
|
63
|
+
GenericArgumentList,
|
|
64
|
+
Static.Const<RAngle>
|
|
65
|
+
], GenericArgumentsMapping>;
|
|
45
66
|
interface GenericReferenceMapping extends Static.IMapping {
|
|
46
67
|
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
68
|
}
|
|
@@ -1,33 +1,31 @@
|
|
|
1
|
-
import * as
|
|
1
|
+
import * as t from '../type/index';
|
|
2
2
|
import { Static } from '../parser/index';
|
|
3
|
-
import { Type } from './static';
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
3
|
+
import { Type, GenericArguments } from './static';
|
|
4
|
+
type TParseSyntax<Context extends Record<PropertyKey, t.TSchema>, Code extends string> = (Static.Parse<GenericArguments, Code, {}> extends [infer Args extends t.TProperties, infer Rest extends string] ? Static.Parse<Type, Rest, Context & Args> : Static.Parse<Type, Code, Context>);
|
|
5
|
+
/** Parses a TypeScript annotation into a TypeBox type but does not infer schematics */
|
|
6
|
+
export declare function NoInfer<Context extends Record<PropertyKey, t.TSchema>, Code extends string>(context: Context, code: Code, options?: t.SchemaOptions): t.TSchema;
|
|
7
|
+
/** Parses a TypeScript annotation into a TypeBox type but does not infer schematics */
|
|
8
|
+
export declare function NoInfer<Code extends string>(code: Code, options?: t.SchemaOptions): t.TSchema;
|
|
9
|
+
/** Parses a TypeScript annotation into a TypeBox type */
|
|
10
|
+
export type TSyntax<Context extends Record<PropertyKey, t.TSchema>, Code extends string> = (TParseSyntax<Context, Code> extends [infer Type extends t.TSchema, string] ? Type : t.TNever);
|
|
11
|
+
/** Parses a TypeScript annotation into a TypeBox type */
|
|
12
|
+
export declare function Syntax<Context extends Record<PropertyKey, t.TSchema>, Annotation extends string>(context: Context, annotation: Annotation, options?: t.SchemaOptions): TSyntax<Context, Annotation>;
|
|
13
|
+
/** Parses a TypeScript annotation into a TypeBox type */
|
|
14
|
+
export declare function Syntax<Annotation extends string>(annotation: Annotation, options?: t.SchemaOptions): TSyntax<{}, Annotation>;
|
|
14
15
|
/**
|
|
15
|
-
* Parses a TSchema type from Syntax.
|
|
16
16
|
* @deprecated Use Syntax() function
|
|
17
17
|
*/
|
|
18
|
-
export declare function Parse<Context extends Record<PropertyKey,
|
|
18
|
+
export declare function Parse<Context extends Record<PropertyKey, t.TSchema>, Annotation extends string>(context: Context, annotation: Annotation, options?: t.SchemaOptions): TSyntax<Context, Annotation>;
|
|
19
19
|
/**
|
|
20
|
-
* Parses a TSchema type from Syntax.
|
|
21
20
|
* @deprecated Use Syntax() function
|
|
22
21
|
*/
|
|
23
|
-
export declare function Parse<
|
|
22
|
+
export declare function Parse<Annotation extends string>(annotation: Annotation, options?: t.SchemaOptions): TSyntax<{}, Annotation>;
|
|
24
23
|
/**
|
|
25
|
-
* Parses a TSchema from TypeScript Syntax
|
|
26
24
|
* @deprecated Use NoInfer() function
|
|
27
25
|
*/
|
|
28
|
-
export declare function ParseOnly<Context extends Record<PropertyKey,
|
|
26
|
+
export declare function ParseOnly<Context extends Record<PropertyKey, t.TSchema>, Code extends string>(context: Context, code: Code, options?: t.SchemaOptions): t.TSchema | undefined;
|
|
29
27
|
/**
|
|
30
|
-
* Parses a TSchema from TypeScript Syntax
|
|
31
28
|
* @deprecated Use NoInfer() function
|
|
32
29
|
*/
|
|
33
|
-
export declare function ParseOnly<Code extends string>(code: Code, options?:
|
|
30
|
+
export declare function ParseOnly<Code extends string>(code: Code, options?: t.SchemaOptions): t.TSchema | undefined;
|
|
31
|
+
export {};
|
|
@@ -5,31 +5,36 @@ exports.NoInfer = NoInfer;
|
|
|
5
5
|
exports.Syntax = Syntax;
|
|
6
6
|
exports.Parse = Parse;
|
|
7
7
|
exports.ParseOnly = ParseOnly;
|
|
8
|
-
const
|
|
8
|
+
const t = require("../type/index");
|
|
9
9
|
const runtime_1 = require("./runtime");
|
|
10
|
-
|
|
10
|
+
// prettier-ignore
|
|
11
|
+
function ParseSyntax(context, code) {
|
|
12
|
+
const results = runtime_1.Module.Parse('GenericArguments', code, {}); // [ArgumentContext, Rest]
|
|
13
|
+
return (results.length === 2
|
|
14
|
+
? runtime_1.Module.Parse('Type', results[1], { ...context, ...results[0] })
|
|
15
|
+
: runtime_1.Module.Parse('Type', code, context));
|
|
16
|
+
}
|
|
17
|
+
/** Parses a TypeScript annotation into a TypeBox type but does not infer schematics */
|
|
11
18
|
// prettier-ignore
|
|
12
19
|
function NoInfer(...args) {
|
|
13
20
|
const withContext = typeof args[0] === 'string' ? false : true;
|
|
14
21
|
const [context, code, options] = withContext ? [args[0], args[1], args[2] || {}] : [{}, args[0], args[1] || {}];
|
|
15
|
-
const
|
|
16
|
-
return
|
|
17
|
-
?
|
|
18
|
-
:
|
|
22
|
+
const result = ParseSyntax(context, code)[0];
|
|
23
|
+
return t.KindGuard.IsSchema(result)
|
|
24
|
+
? t.CloneType(result, options)
|
|
25
|
+
: t.Never(options);
|
|
19
26
|
}
|
|
20
|
-
/** Parses a
|
|
27
|
+
/** Parses a TypeScript annotation into a TypeBox type */
|
|
21
28
|
function Syntax(...args) {
|
|
22
29
|
return NoInfer.apply(null, args);
|
|
23
30
|
}
|
|
24
31
|
/**
|
|
25
|
-
* Parses a TSchema type from Syntax.
|
|
26
32
|
* @deprecated Use Syntax() function
|
|
27
33
|
*/
|
|
28
34
|
function Parse(...args) {
|
|
29
35
|
return NoInfer.apply(null, args);
|
|
30
36
|
}
|
|
31
37
|
/**
|
|
32
|
-
* Parses a TSchema from TypeScript Syntax
|
|
33
38
|
* @deprecated Use NoInfer() function
|
|
34
39
|
*/
|
|
35
40
|
function ParseOnly(...args) {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { type TSchema } from '../schema/index';
|
|
2
2
|
import { type TArgument } from '../argument/index';
|
|
3
|
-
import { type
|
|
3
|
+
import { type TUnknown } from '../unknown/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';
|
|
@@ -24,9 +24,9 @@ type TFromArray<Args extends TSchema[], Type extends TSchema, Result extends TAr
|
|
|
24
24
|
type TFromAsyncIterator<Args extends TSchema[], Type extends TSchema, Result extends TAsyncIterator = TAsyncIterator<TFromType<Args, Type>>> = Result;
|
|
25
25
|
type TFromIterator<Args extends TSchema[], Type extends TSchema, Result extends TIterator = TIterator<TFromType<Args, Type>>> = Result;
|
|
26
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
|
|
27
|
+
type TFromObject<Args extends TSchema[], Properties extends TProperties, Result extends TProperties = TFromProperties<Args, Properties>> = TObject<Result>;
|
|
28
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
|
|
29
|
+
type TFromArgument<Args extends TSchema[], Index extends number, Result extends TSchema = Index extends keyof Args[Index] ? Args[Index] : TUnknown> = Result;
|
|
30
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
31
|
IsReadonly,
|
|
32
32
|
IsOptional
|
|
@@ -4,11 +4,12 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
4
4
|
exports.FromTypes = FromTypes;
|
|
5
5
|
exports.Instantiate = Instantiate;
|
|
6
6
|
const type_1 = require("../clone/type");
|
|
7
|
-
const index_1 = require("../
|
|
7
|
+
const index_1 = require("../unknown/index");
|
|
8
8
|
const index_2 = require("../readonly-optional/index");
|
|
9
9
|
const index_3 = require("../readonly/index");
|
|
10
10
|
const index_4 = require("../optional/index");
|
|
11
|
-
const index_5 = require("../
|
|
11
|
+
const index_5 = require("../object/index");
|
|
12
|
+
const index_6 = require("../record/index");
|
|
12
13
|
const ValueGuard = require("../guard/value");
|
|
13
14
|
const KindGuard = require("../guard/kind");
|
|
14
15
|
// prettier-ignore
|
|
@@ -62,19 +63,19 @@ function FromPromise(args, type) {
|
|
|
62
63
|
}
|
|
63
64
|
// prettier-ignore
|
|
64
65
|
function FromObject(args, type) {
|
|
65
|
-
|
|
66
|
-
return type;
|
|
66
|
+
const properties = FromProperties(args, type.properties);
|
|
67
|
+
return { ...type, ...(0, index_5.Object)(properties) }; // retain options
|
|
67
68
|
}
|
|
68
69
|
// prettier-ignore
|
|
69
70
|
function FromRecord(args, type) {
|
|
70
|
-
const mappedKey = FromType(args, (0,
|
|
71
|
-
const mappedValue = FromType(args, (0,
|
|
72
|
-
const result = (0,
|
|
71
|
+
const mappedKey = FromType(args, (0, index_6.RecordKey)(type));
|
|
72
|
+
const mappedValue = FromType(args, (0, index_6.RecordValue)(type));
|
|
73
|
+
const result = (0, index_6.Record)(mappedKey, mappedValue);
|
|
73
74
|
return { ...type, ...result }; // retain options
|
|
74
75
|
}
|
|
75
76
|
// prettier-ignore
|
|
76
77
|
function FromArgument(args, argument) {
|
|
77
|
-
return argument.index in args ? args[argument.index] : (0, index_1.
|
|
78
|
+
return argument.index in args ? args[argument.index] : (0, index_1.Unknown)();
|
|
78
79
|
}
|
|
79
80
|
// prettier-ignore
|
|
80
81
|
function FromProperty(args, type) {
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { Runtime } from '../parser/index.mjs';
|
|
2
2
|
import * as t from '../type/index.mjs';
|
|
3
3
|
export declare const Module: Runtime.Module<{
|
|
4
|
+
GenericArgumentList: Runtime.IUnion<unknown[]>;
|
|
5
|
+
GenericArguments: Runtime.ITuple<{}>;
|
|
4
6
|
Literal: Runtime.IUnion<t.TLiteral<string> | t.TLiteral<number> | t.TLiteral<boolean>>;
|
|
5
7
|
Keyword: Runtime.IUnion<t.TAny | t.TBoolean | t.TBigInt | t.TNever | t.TString | t.TNumber | t.TInteger | t.TNull | t.TSymbol | t.TUndefined | t.TUnknown | t.TVoid>;
|
|
6
8
|
KeyOf: Runtime.IUnion<boolean>;
|
|
@@ -36,6 +36,44 @@ const Dereference = (context, key) => {
|
|
|
36
36
|
return key in context ? context[key] : t.Ref(key);
|
|
37
37
|
};
|
|
38
38
|
// ------------------------------------------------------------------
|
|
39
|
+
// GenericArgumentList
|
|
40
|
+
// ------------------------------------------------------------------
|
|
41
|
+
// prettier-ignore
|
|
42
|
+
const GenericArgumentListMapping = (results) => {
|
|
43
|
+
return (results.length === 3 ? [results[0], ...results[2]] :
|
|
44
|
+
results.length === 2 ? [results[0]] :
|
|
45
|
+
results.length === 1 ? [results[0]] :
|
|
46
|
+
[]);
|
|
47
|
+
};
|
|
48
|
+
// prettier-ignore
|
|
49
|
+
const GenericArgumentList = Runtime.Union([
|
|
50
|
+
Runtime.Tuple([Runtime.Ident(), Runtime.Const(Comma), Runtime.Ref('GenericArgumentList')]),
|
|
51
|
+
Runtime.Tuple([Runtime.Ident(), Runtime.Const(Comma)]),
|
|
52
|
+
Runtime.Tuple([Runtime.Ident()]),
|
|
53
|
+
Runtime.Tuple([]),
|
|
54
|
+
], (results) => GenericArgumentListMapping(results));
|
|
55
|
+
// ------------------------------------------------------------------
|
|
56
|
+
// GenericArguments
|
|
57
|
+
// ------------------------------------------------------------------
|
|
58
|
+
// prettier-ignore
|
|
59
|
+
const GenericArgumentsContext = (args) => {
|
|
60
|
+
return args.reduce((result, arg, index) => {
|
|
61
|
+
return { ...result, [arg]: t.Argument(index) };
|
|
62
|
+
}, {});
|
|
63
|
+
};
|
|
64
|
+
// prettier-ignore
|
|
65
|
+
const GenericArgumentsMapping = (results) => {
|
|
66
|
+
return results.length === 3
|
|
67
|
+
? GenericArgumentsContext(results[1])
|
|
68
|
+
: {};
|
|
69
|
+
};
|
|
70
|
+
// prettier-ignore
|
|
71
|
+
const GenericArguments = Runtime.Tuple([
|
|
72
|
+
Runtime.Const(LAngle),
|
|
73
|
+
Runtime.Ref('GenericArgumentList'),
|
|
74
|
+
Runtime.Const(RAngle),
|
|
75
|
+
], results => GenericArgumentsMapping(results));
|
|
76
|
+
// ------------------------------------------------------------------
|
|
39
77
|
// GenericReference
|
|
40
78
|
// ------------------------------------------------------------------
|
|
41
79
|
function GenericReferenceMapping(results, context) {
|
|
@@ -610,6 +648,11 @@ const Uint8Array = Runtime.Const('Uint8Array', Runtime.As(t.Uint8Array()));
|
|
|
610
648
|
// ------------------------------------------------------------------
|
|
611
649
|
// prettier-ignore
|
|
612
650
|
export const Module = new Runtime.Module({
|
|
651
|
+
// ----------------------------------------------------------------
|
|
652
|
+
// Generic Arguments
|
|
653
|
+
// ----------------------------------------------------------------
|
|
654
|
+
GenericArgumentList,
|
|
655
|
+
GenericArguments,
|
|
613
656
|
// ----------------------------------------------------------------
|
|
614
657
|
// Type Expressions
|
|
615
658
|
// ----------------------------------------------------------------
|
|
@@ -42,6 +42,27 @@ type Delimit<Parser extends Static.IParser, Delimiter extends Static.IParser> =
|
|
|
42
42
|
Static.Tuple<[]>
|
|
43
43
|
], DelimitMapping>);
|
|
44
44
|
type Dereference<Context extends t.TProperties, Ref extends string> = (Ref extends keyof Context ? Context[Ref] : t.TRef<Ref>);
|
|
45
|
+
interface GenericArgumentListMapping extends Static.IMapping {
|
|
46
|
+
output: (this['input'] extends [infer Ident extends string, Comma, infer Rest extends unknown[]] ? [Ident, ...Rest] : this['input'] extends [infer Ident extends string, Comma] ? [Ident] : this['input'] extends [infer Ident extends string] ? [Ident] : [
|
|
47
|
+
]);
|
|
48
|
+
}
|
|
49
|
+
type GenericArgumentList = Static.Union<[
|
|
50
|
+
Static.Tuple<[Static.Ident, Static.Const<Comma>, GenericArgumentList]>,
|
|
51
|
+
Static.Tuple<[Static.Ident, Static.Const<Comma>]>,
|
|
52
|
+
Static.Tuple<[Static.Ident]>,
|
|
53
|
+
Static.Tuple<[]>
|
|
54
|
+
], GenericArgumentListMapping>;
|
|
55
|
+
type GenericArgumentsContext<Args extends string[], Result extends t.TProperties = {}> = (Args extends [...infer Left extends string[], infer Right extends string] ? GenericArgumentsContext<Left, Result & {
|
|
56
|
+
[_ in Right]: t.TArgument<Left['length']>;
|
|
57
|
+
}> : t.Evaluate<Result>);
|
|
58
|
+
interface GenericArgumentsMapping extends Static.IMapping {
|
|
59
|
+
output: this['input'] extends [LAngle, infer Args extends string[], RAngle] ? GenericArgumentsContext<Args> : never;
|
|
60
|
+
}
|
|
61
|
+
export type GenericArguments = Static.Tuple<[
|
|
62
|
+
Static.Const<LAngle>,
|
|
63
|
+
GenericArgumentList,
|
|
64
|
+
Static.Const<RAngle>
|
|
65
|
+
], GenericArgumentsMapping>;
|
|
45
66
|
interface GenericReferenceMapping extends Static.IMapping {
|
|
46
67
|
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
68
|
}
|
|
@@ -1,33 +1,31 @@
|
|
|
1
|
-
import * as
|
|
1
|
+
import * as t from '../type/index.mjs';
|
|
2
2
|
import { Static } from '../parser/index.mjs';
|
|
3
|
-
import { Type } from './static.mjs';
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
3
|
+
import { Type, GenericArguments } from './static.mjs';
|
|
4
|
+
type TParseSyntax<Context extends Record<PropertyKey, t.TSchema>, Code extends string> = (Static.Parse<GenericArguments, Code, {}> extends [infer Args extends t.TProperties, infer Rest extends string] ? Static.Parse<Type, Rest, Context & Args> : Static.Parse<Type, Code, Context>);
|
|
5
|
+
/** Parses a TypeScript annotation into a TypeBox type but does not infer schematics */
|
|
6
|
+
export declare function NoInfer<Context extends Record<PropertyKey, t.TSchema>, Code extends string>(context: Context, code: Code, options?: t.SchemaOptions): t.TSchema;
|
|
7
|
+
/** Parses a TypeScript annotation into a TypeBox type but does not infer schematics */
|
|
8
|
+
export declare function NoInfer<Code extends string>(code: Code, options?: t.SchemaOptions): t.TSchema;
|
|
9
|
+
/** Parses a TypeScript annotation into a TypeBox type */
|
|
10
|
+
export type TSyntax<Context extends Record<PropertyKey, t.TSchema>, Code extends string> = (TParseSyntax<Context, Code> extends [infer Type extends t.TSchema, string] ? Type : t.TNever);
|
|
11
|
+
/** Parses a TypeScript annotation into a TypeBox type */
|
|
12
|
+
export declare function Syntax<Context extends Record<PropertyKey, t.TSchema>, Annotation extends string>(context: Context, annotation: Annotation, options?: t.SchemaOptions): TSyntax<Context, Annotation>;
|
|
13
|
+
/** Parses a TypeScript annotation into a TypeBox type */
|
|
14
|
+
export declare function Syntax<Annotation extends string>(annotation: Annotation, options?: t.SchemaOptions): TSyntax<{}, Annotation>;
|
|
14
15
|
/**
|
|
15
|
-
* Parses a TSchema type from Syntax.
|
|
16
16
|
* @deprecated Use Syntax() function
|
|
17
17
|
*/
|
|
18
|
-
export declare function Parse<Context extends Record<PropertyKey,
|
|
18
|
+
export declare function Parse<Context extends Record<PropertyKey, t.TSchema>, Annotation extends string>(context: Context, annotation: Annotation, options?: t.SchemaOptions): TSyntax<Context, Annotation>;
|
|
19
19
|
/**
|
|
20
|
-
* Parses a TSchema type from Syntax.
|
|
21
20
|
* @deprecated Use Syntax() function
|
|
22
21
|
*/
|
|
23
|
-
export declare function Parse<
|
|
22
|
+
export declare function Parse<Annotation extends string>(annotation: Annotation, options?: t.SchemaOptions): TSyntax<{}, Annotation>;
|
|
24
23
|
/**
|
|
25
|
-
* Parses a TSchema from TypeScript Syntax
|
|
26
24
|
* @deprecated Use NoInfer() function
|
|
27
25
|
*/
|
|
28
|
-
export declare function ParseOnly<Context extends Record<PropertyKey,
|
|
26
|
+
export declare function ParseOnly<Context extends Record<PropertyKey, t.TSchema>, Code extends string>(context: Context, code: Code, options?: t.SchemaOptions): t.TSchema | undefined;
|
|
29
27
|
/**
|
|
30
|
-
* Parses a TSchema from TypeScript Syntax
|
|
31
28
|
* @deprecated Use NoInfer() function
|
|
32
29
|
*/
|
|
33
|
-
export declare function ParseOnly<Code extends string>(code: Code, options?:
|
|
30
|
+
export declare function ParseOnly<Code extends string>(code: Code, options?: t.SchemaOptions): t.TSchema | undefined;
|
|
31
|
+
export {};
|
|
@@ -1,28 +1,33 @@
|
|
|
1
|
-
import * as
|
|
1
|
+
import * as t from '../type/index.mjs';
|
|
2
2
|
import { Module } from './runtime.mjs';
|
|
3
|
-
|
|
3
|
+
// prettier-ignore
|
|
4
|
+
function ParseSyntax(context, code) {
|
|
5
|
+
const results = Module.Parse('GenericArguments', code, {}); // [ArgumentContext, Rest]
|
|
6
|
+
return (results.length === 2
|
|
7
|
+
? Module.Parse('Type', results[1], { ...context, ...results[0] })
|
|
8
|
+
: Module.Parse('Type', code, context));
|
|
9
|
+
}
|
|
10
|
+
/** Parses a TypeScript annotation into a TypeBox type but does not infer schematics */
|
|
4
11
|
// prettier-ignore
|
|
5
12
|
export function NoInfer(...args) {
|
|
6
13
|
const withContext = typeof args[0] === 'string' ? false : true;
|
|
7
14
|
const [context, code, options] = withContext ? [args[0], args[1], args[2] || {}] : [{}, args[0], args[1] || {}];
|
|
8
|
-
const
|
|
9
|
-
return
|
|
10
|
-
?
|
|
11
|
-
:
|
|
15
|
+
const result = ParseSyntax(context, code)[0];
|
|
16
|
+
return t.KindGuard.IsSchema(result)
|
|
17
|
+
? t.CloneType(result, options)
|
|
18
|
+
: t.Never(options);
|
|
12
19
|
}
|
|
13
|
-
/** Parses a
|
|
20
|
+
/** Parses a TypeScript annotation into a TypeBox type */
|
|
14
21
|
export function Syntax(...args) {
|
|
15
22
|
return NoInfer.apply(null, args);
|
|
16
23
|
}
|
|
17
24
|
/**
|
|
18
|
-
* Parses a TSchema type from Syntax.
|
|
19
25
|
* @deprecated Use Syntax() function
|
|
20
26
|
*/
|
|
21
27
|
export function Parse(...args) {
|
|
22
28
|
return NoInfer.apply(null, args);
|
|
23
29
|
}
|
|
24
30
|
/**
|
|
25
|
-
* Parses a TSchema from TypeScript Syntax
|
|
26
31
|
* @deprecated Use NoInfer() function
|
|
27
32
|
*/
|
|
28
33
|
export function ParseOnly(...args) {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { type TSchema } from '../schema/index.mjs';
|
|
2
2
|
import { type TArgument } from '../argument/index.mjs';
|
|
3
|
-
import { type
|
|
3
|
+
import { type TUnknown } from '../unknown/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';
|
|
@@ -24,9 +24,9 @@ type TFromArray<Args extends TSchema[], Type extends TSchema, Result extends TAr
|
|
|
24
24
|
type TFromAsyncIterator<Args extends TSchema[], Type extends TSchema, Result extends TAsyncIterator = TAsyncIterator<TFromType<Args, Type>>> = Result;
|
|
25
25
|
type TFromIterator<Args extends TSchema[], Type extends TSchema, Result extends TIterator = TIterator<TFromType<Args, Type>>> = Result;
|
|
26
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
|
|
27
|
+
type TFromObject<Args extends TSchema[], Properties extends TProperties, Result extends TProperties = TFromProperties<Args, Properties>> = TObject<Result>;
|
|
28
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
|
|
29
|
+
type TFromArgument<Args extends TSchema[], Index extends number, Result extends TSchema = Index extends keyof Args[Index] ? Args[Index] : TUnknown> = Result;
|
|
30
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
31
|
IsReadonly,
|
|
32
32
|
IsOptional
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { CloneType } from '../clone/type.mjs';
|
|
2
|
-
import {
|
|
2
|
+
import { Unknown } from '../unknown/index.mjs';
|
|
3
3
|
import { ReadonlyOptional } from '../readonly-optional/index.mjs';
|
|
4
4
|
import { Readonly } from '../readonly/index.mjs';
|
|
5
5
|
import { Optional } from '../optional/index.mjs';
|
|
6
|
+
import { Object } from '../object/index.mjs';
|
|
6
7
|
import { Record, RecordKey, RecordValue } from '../record/index.mjs';
|
|
7
8
|
import * as ValueGuard from '../guard/value.mjs';
|
|
8
9
|
import * as KindGuard from '../guard/kind.mjs';
|
|
@@ -57,8 +58,8 @@ function FromPromise(args, type) {
|
|
|
57
58
|
}
|
|
58
59
|
// prettier-ignore
|
|
59
60
|
function FromObject(args, type) {
|
|
60
|
-
|
|
61
|
-
return type;
|
|
61
|
+
const properties = FromProperties(args, type.properties);
|
|
62
|
+
return { ...type, ...Object(properties) }; // retain options
|
|
62
63
|
}
|
|
63
64
|
// prettier-ignore
|
|
64
65
|
function FromRecord(args, type) {
|
|
@@ -69,7 +70,7 @@ function FromRecord(args, type) {
|
|
|
69
70
|
}
|
|
70
71
|
// prettier-ignore
|
|
71
72
|
function FromArgument(args, argument) {
|
|
72
|
-
return argument.index in args ? args[argument.index] :
|
|
73
|
+
return argument.index in args ? args[argument.index] : Unknown();
|
|
73
74
|
}
|
|
74
75
|
// prettier-ignore
|
|
75
76
|
function FromProperty(args, type) {
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -98,8 +98,8 @@ License MIT
|
|
|
98
98
|
- [Syntax](#syntax)
|
|
99
99
|
- [Create](#syntax-create)
|
|
100
100
|
- [Parameters](#syntax-parameters)
|
|
101
|
-
- [Options](#syntax-options)
|
|
102
101
|
- [Generics](#syntax-generics)
|
|
102
|
+
- [Options](#syntax-options)
|
|
103
103
|
- [TypeRegistry](#typeregistry)
|
|
104
104
|
- [Type](#typeregistry-type)
|
|
105
105
|
- [Format](#typeregistry-format)
|
|
@@ -1306,9 +1306,9 @@ ValuePointer.Set(A, '/z', 1) // A' = { x: 1, y: 1, z: 1
|
|
|
1306
1306
|
|
|
1307
1307
|
## Syntax Types
|
|
1308
1308
|
|
|
1309
|
-
TypeBox
|
|
1309
|
+
TypeBox includes support for parsing TypeScript annotation syntax into TypeBox schematics.
|
|
1310
1310
|
|
|
1311
|
-
|
|
1311
|
+
This feature is provided via optional import.
|
|
1312
1312
|
|
|
1313
1313
|
```typescript
|
|
1314
1314
|
import { Syntax } from '@sinclair/typebox/syntax'
|
|
@@ -1318,20 +1318,25 @@ import { Syntax } from '@sinclair/typebox/syntax'
|
|
|
1318
1318
|
|
|
1319
1319
|
### Create
|
|
1320
1320
|
|
|
1321
|
-
Use the Syntax function to create TypeBox types from TypeScript syntax
|
|
1321
|
+
Use the Syntax function to create TypeBox types from TypeScript syntax ([Example](https://www.typescriptlang.org/play/?moduleResolution=99&module=199&ts=5.8.0-beta#code/JYWwDg9gTgLgBAbzgZQJ4DsYEMAecC+cAZlBCHAOQACAzsOgMYA2WwUA9DKmAKYBGEHOxoZsOCgChQkWIhTYYwBgWKly1OoxZtO3foMkSGEdDXgAVOAF4Uo3AAoABkhwAuOOgCuIPjygAaOFR3Lx8-AkcASjgY2Jj2djhjUwt3cwB5PgArHgYYAB4ECTiS0rLyisrYhNi3OHMAOW9fAOKq9o7OuBqY4PqmsKg2rpHR+MT8AD4JCS5eeut5LEUGfLmeCCJ6ybHKmvWFmyLdk86euDrQlv9h07uy876rv1v7t-GCIA))
|
|
1322
1322
|
|
|
1323
1323
|
```typescript
|
|
1324
1324
|
const T = Syntax(`{ x: number, y: number }`) // const T: TObject<{
|
|
1325
1325
|
// x: TNumber,
|
|
1326
1326
|
// y: TNumber
|
|
1327
1327
|
// }>
|
|
1328
|
+
|
|
1329
|
+
type T = Static<typeof T> // type T = {
|
|
1330
|
+
// x: number,
|
|
1331
|
+
// y: number
|
|
1332
|
+
// }
|
|
1328
1333
|
```
|
|
1329
1334
|
|
|
1330
1335
|
<a name="syntax-parameters"></a>
|
|
1331
1336
|
|
|
1332
1337
|
### Parameters
|
|
1333
1338
|
|
|
1334
|
-
Syntax types can be parameterized to receive exterior types.
|
|
1339
|
+
Syntax types can be parameterized to receive exterior types ([Example](https://www.typescriptlang.org/play/?moduleResolution=99&module=199&ts=5.8.0-beta#code/JYWwDg9gTgLgBAbzgZQJ4DsYEMAecC+cAZlBCHAOQACAzsOgMYA2WwUA9DKmAKYBGEHOxoZsOCgCgJDCOhrwAKnAC8KUbgAUAAyQ4AXHHQBXEHx5QANHFQHjp8wS0BKOK7ev27ODLmKDCgHk+ACseBhgAHgQJd1i4+ITEpLdPN304BQA5EzNLGOSCwqK4VNcbDOz7KHzi2rqPL3wAPikfeRQVNUxNJCV8Ky0ABSxYYCwmCIUm52LUtvhkfyDQ8Kia+o2C0rh0wLAYYFlxycrcpot1zav47fK9g6OJrJzzFuv3m8amoA))
|
|
1335
1340
|
|
|
1336
1341
|
```typescript
|
|
1337
1342
|
const T = Syntax(`{ x: number, y: number }`) // const T: TObject<{
|
|
@@ -1345,35 +1350,19 @@ const S = Syntax({ T }, `Partial<T>`) // const S: TObject<{
|
|
|
1345
1350
|
// }>
|
|
1346
1351
|
```
|
|
1347
1352
|
|
|
1348
|
-
<a name='syntax-options'></a>
|
|
1349
|
-
|
|
1350
|
-
### Options
|
|
1351
1353
|
|
|
1352
|
-
Options can be passed via the last parameter
|
|
1353
|
-
|
|
1354
|
-
```typescript
|
|
1355
|
-
const T = Syntax(`number`, { // const T = {
|
|
1356
|
-
minimum: 0, // type: 'number',
|
|
1357
|
-
maximum: 10 // minimum: 0,
|
|
1358
|
-
}) // maximum: 10
|
|
1359
|
-
// }
|
|
1360
|
-
```
|
|
1361
1354
|
|
|
1362
1355
|
<a name="syntax-generics"></a>
|
|
1363
1356
|
|
|
1364
1357
|
### Generics
|
|
1365
1358
|
|
|
1366
|
-
|
|
1359
|
+
Syntax types support generic parameters in the following way ([Example](https://www.typescriptlang.org/play/?moduleResolution=99&module=199&ts=5.8.0-beta#code/JYWwDg9gTgLgBAbzgZQJ4DsYEMAecC+cAZlBCHAOQACAzsOgMYA2WwUA9DKmAKYBGEHOxoZsOCgChQkWIhTYYwBgWKly1OoxZtO3foMkSGEdDXgA1HgxjQ4AXhSjcACgAGAHgAaAGjgBNXwAtAD45CTg4HAAuOB84cLhUGID4iIAvGMD4-FcASgkjEzM4ACEsOhpLa2gae0dMFyQqmygCX1cEBOi4Zuh3AEZfAAZh4O8EpJ6rFvcRuEG4IbGEjKnqqFnh337lnPyJLl5S8uBK6Zq65AUld0OeCCJjit6oGlCIiPZ2ODun05fag5Oh8QaCweCIZCoV8Pt0kN0FpM5qshm0ElCMZisSCYRFJvCYnNJgsUWjseSKeDcXBVgTFr4kb5Vv0COjKezsTD8EA))
|
|
1367
1360
|
|
|
1368
1361
|
```typescript
|
|
1369
|
-
const
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
const Vector = Syntax({ T0, T1, T2 }, `{
|
|
1374
|
-
x: T0,
|
|
1375
|
-
y: T1,
|
|
1376
|
-
z: T2
|
|
1362
|
+
const Vector = Syntax(`<X, Y, Z> {
|
|
1363
|
+
x: X,
|
|
1364
|
+
y: Y,
|
|
1365
|
+
z: Z
|
|
1377
1366
|
}`)
|
|
1378
1367
|
|
|
1379
1368
|
const BasisVectors = Syntax({ Vector }, `{
|
|
@@ -1386,7 +1375,20 @@ type BasisVectors = Static<typeof BasisVectors> // type BasisVectors = {
|
|
|
1386
1375
|
// x: { x: 1, y: 0, z: 0 },
|
|
1387
1376
|
// y: { x: 0, y: 1, z: 0 },
|
|
1388
1377
|
// z: { x: 0, y: 0, z: 1 }
|
|
1389
|
-
// }
|
|
1378
|
+
// }
|
|
1379
|
+
```
|
|
1380
|
+
|
|
1381
|
+
<a name='syntax-options'></a>
|
|
1382
|
+
|
|
1383
|
+
### Options
|
|
1384
|
+
|
|
1385
|
+
Options can be passed via the last parameter
|
|
1386
|
+
|
|
1387
|
+
```typescript
|
|
1388
|
+
const T = Syntax(`number`, { minimum: 42 }) // const T = {
|
|
1389
|
+
// type: 'number',
|
|
1390
|
+
// minimum: 42
|
|
1391
|
+
// }
|
|
1390
1392
|
```
|
|
1391
1393
|
|
|
1392
1394
|
<a name='typeregistry'></a>
|