@sinclair/typebox 0.34.24 → 0.34.26

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.
Files changed (36) hide show
  1. package/build/cjs/parser/runtime/guard.d.ts +12 -6
  2. package/build/cjs/parser/runtime/guard.js +31 -20
  3. package/build/cjs/parser/runtime/module.d.ts +2 -2
  4. package/build/cjs/parser/runtime/module.js +4 -4
  5. package/build/cjs/parser/runtime/parse.d.ts +8 -8
  6. package/build/cjs/parser/runtime/parse.js +71 -43
  7. package/build/cjs/parser/runtime/types.d.ts +56 -26
  8. package/build/cjs/parser/runtime/types.js +34 -8
  9. package/build/cjs/parser/static/parse.d.ts +6 -3
  10. package/build/cjs/parser/static/types.d.ts +41 -18
  11. package/build/cjs/syntax/runtime.d.ts +2 -2
  12. package/build/cjs/syntax/runtime.js +13 -9
  13. package/build/cjs/syntax/static.d.ts +8 -5
  14. package/build/cjs/syntax/syntax.d.ts +7 -9
  15. package/build/cjs/syntax/syntax.js +3 -10
  16. package/build/cjs/type/instantiate/instantiate.d.ts +1 -1
  17. package/build/cjs/type/instantiate/instantiate.js +2 -2
  18. package/build/esm/parser/runtime/guard.d.mts +12 -6
  19. package/build/esm/parser/runtime/guard.mjs +26 -18
  20. package/build/esm/parser/runtime/module.d.mts +2 -2
  21. package/build/esm/parser/runtime/module.mjs +4 -4
  22. package/build/esm/parser/runtime/parse.d.mts +8 -8
  23. package/build/esm/parser/runtime/parse.mjs +71 -43
  24. package/build/esm/parser/runtime/types.d.mts +56 -26
  25. package/build/esm/parser/runtime/types.mjs +29 -6
  26. package/build/esm/parser/static/parse.d.mts +6 -3
  27. package/build/esm/parser/static/types.d.mts +41 -18
  28. package/build/esm/syntax/runtime.d.mts +2 -2
  29. package/build/esm/syntax/runtime.mjs +13 -9
  30. package/build/esm/syntax/static.d.mts +8 -5
  31. package/build/esm/syntax/syntax.d.mts +7 -9
  32. package/build/esm/syntax/syntax.mjs +3 -10
  33. package/build/esm/type/instantiate/instantiate.d.mts +1 -1
  34. package/build/esm/type/instantiate/instantiate.mjs +2 -2
  35. package/package.json +1 -1
  36. package/readme.md +19 -1
@@ -1,4 +1,6 @@
1
1
  export type IModuleProperties = Record<PropertyKey, IParser>;
2
+ /** Force output static type evaluation for Arrays */
3
+ export type StaticEnsure<T> = T extends infer R ? R : never;
2
4
  /** Infers the Output Parameter for a Parser */
3
5
  export type StaticParser<Parser extends IParser> = Parser extends IParser<infer Output extends unknown> ? Output : unknown;
4
6
  export type IMapping<Input extends unknown = any, Output extends unknown = unknown> = (input: Input, context: any) => Output;
@@ -10,59 +12,87 @@ export interface IParser<Output extends unknown = unknown> {
10
12
  type: string;
11
13
  mapping: IMapping<any, Output>;
12
14
  }
13
- export type TupleParameter<Parsers extends IParser[], Result extends unknown[] = []> = Parsers extends [infer L extends IParser, ...infer R extends IParser[]] ? TupleParameter<R, [...Result, StaticParser<L>]> : Result;
14
- export interface ITuple<Output extends unknown = unknown> extends IParser<Output> {
15
- type: 'Tuple';
16
- parsers: IParser[];
15
+ export type ContextParameter<_Left extends IParser, Right extends IParser> = (StaticParser<Right>);
16
+ export interface IContext<Output extends unknown = unknown> extends IParser<Output> {
17
+ type: 'Context';
18
+ left: IParser;
19
+ right: IParser;
17
20
  }
18
- /** Creates a Tuple parser */
19
- export declare function Tuple<Parsers extends IParser[], Mapping extends IMapping = IMapping<TupleParameter<Parsers>>>(parsers: [...Parsers], mapping: Mapping): ITuple<ReturnType<Mapping>>;
20
- /** Creates a Tuple parser */
21
- export declare function Tuple<Parsers extends IParser[]>(parsers: [...Parsers]): ITuple<TupleParameter<Parsers>>;
22
- export type UnionParameter<Parsers extends IParser[], Result extends unknown = never> = Parsers extends [infer L extends IParser, ...infer R extends IParser[]] ? UnionParameter<R, Result | StaticParser<L>> : Result;
23
- export interface IUnion<Output extends unknown = unknown> extends IParser<Output> {
24
- type: 'Union';
25
- parsers: IParser[];
21
+ /** `[Context]` Creates a Context Parser */
22
+ export declare function Context<Left extends IParser, Right extends IParser, Mapping extends IMapping = IMapping<ContextParameter<Left, Right>>>(left: Left, right: Right, mapping: Mapping): IContext<ReturnType<Mapping>>;
23
+ /** `[Context]` Creates a Context Parser */
24
+ export declare function Context<Left extends IParser, Right extends IParser>(left: Left, right: Right): IContext<ContextParameter<Left, Right>>;
25
+ export type ArrayParameter<Parser extends IParser> = StaticEnsure<StaticParser<Parser>[]>;
26
+ export interface IArray<Output extends unknown = unknown> extends IParser<Output> {
27
+ type: 'Array';
28
+ parser: IParser;
26
29
  }
27
- /** Creates a Union parser */
28
- export declare function Union<Parsers extends IParser[], Mapping extends IMapping = IMapping<UnionParameter<Parsers>>>(parsers: [...Parsers], mapping: Mapping): IUnion<ReturnType<Mapping>>;
29
- /** Creates a Union parser */
30
- export declare function Union<Parsers extends IParser[]>(parsers: [...Parsers]): IUnion<UnionParameter<Parsers>>;
30
+ /** `[EBNF]` Creates an Array Parser */
31
+ export declare function Array<Parser extends IParser, Mapping extends IMapping = IMapping<ArrayParameter<Parser>>>(parser: Parser, mapping: Mapping): IArray<ReturnType<Mapping>>;
32
+ /** `[EBNF]` Creates an Array Parser */
33
+ export declare function Array<Parser extends IParser>(parser: Parser): IArray<ArrayParameter<Parser>>;
31
34
  export interface IConst<Output extends unknown = unknown> extends IParser<Output> {
32
35
  type: 'Const';
33
36
  value: string;
34
37
  }
35
- /** Creates a Const parser */
38
+ /** `[TERM]` Creates a Const Parser */
36
39
  export declare function Const<Value extends string, Mapping extends IMapping<Value>>(value: Value, mapping: Mapping): IConst<ReturnType<Mapping>>;
37
- /** Creates a Const parser */
40
+ /** `[TERM]` Creates a Const Parser */
38
41
  export declare function Const<Value extends string>(value: Value): IConst<Value>;
39
42
  export interface IRef<Output extends unknown = unknown> extends IParser<Output> {
40
43
  type: 'Ref';
41
44
  ref: string;
42
45
  }
43
- /** Creates a Ref parser */
46
+ /** `[BNF]` Creates a Ref Parser. This Parser can only be used in the context of a Module */
44
47
  export declare function Ref<Type extends unknown, Mapping extends IMapping<Type>>(ref: string, mapping: Mapping): IRef<ReturnType<Mapping>>;
45
- /** Creates a Ref parser */
48
+ /** `[BNF]` Creates a Ref Parser. This Parser can only be used in the context of a Module */
46
49
  export declare function Ref<Type extends unknown>(ref: string): IRef<Type>;
47
50
  export interface IString<Output extends unknown = unknown> extends IParser<Output> {
48
51
  type: 'String';
49
52
  options: string[];
50
53
  }
51
- /** Creates a String Parser. Options are an array of permissable quote characters */
54
+ /** `[TERM]` Creates a String Parser. Options are an array of permissable quote characters */
52
55
  export declare function String<Mapping extends IMapping<string>>(options: string[], mapping: Mapping): IString<ReturnType<Mapping>>;
53
- /** Creates a String Parser. Options are an array of permissable quote characters */
56
+ /** `[TERM]` Creates a String Parser. Options are an array of permissable quote characters */
54
57
  export declare function String(options: string[]): IString<string>;
55
58
  export interface IIdent<Output extends unknown = unknown> extends IParser<Output> {
56
59
  type: 'Ident';
57
60
  }
58
- /** Creates an Ident parser */
61
+ /** `[TERM]` Creates an Ident Parser where Ident matches any valid JavaScript identifier */
59
62
  export declare function Ident<Mapping extends IMapping<string>>(mapping: Mapping): IIdent<ReturnType<Mapping>>;
60
- /** Creates an Ident parser */
63
+ /** `[TERM]` Creates an Ident Parser where Ident matches any valid JavaScript identifier */
61
64
  export declare function Ident(): IIdent<string>;
62
65
  export interface INumber<Output extends unknown = unknown> extends IParser<Output> {
63
66
  type: 'Number';
64
67
  }
65
- /** Creates a Number parser */
68
+ /** `[TERM]` Creates an Number Parser */
66
69
  export declare function Number<Mapping extends IMapping<string>>(mapping: Mapping): INumber<ReturnType<Mapping>>;
67
- /** Creates a Number parser */
70
+ /** `[TERM]` Creates an Number Parser */
68
71
  export declare function Number(): INumber<string>;
72
+ export type OptionalParameter<Parser extends IParser, Result extends unknown = [StaticParser<Parser>] | []> = (Result);
73
+ export interface IOptional<Output extends unknown = unknown> extends IParser<Output> {
74
+ type: 'Optional';
75
+ parser: IParser;
76
+ }
77
+ /** `[EBNF]` Creates an Optional Parser */
78
+ export declare function Optional<Parser extends IParser, Mapping extends IMapping = IMapping<OptionalParameter<Parser>>>(parser: Parser, mapping: Mapping): IOptional<ReturnType<Mapping>>;
79
+ /** `[EBNF]` Creates an Optional Parser */
80
+ export declare function Optional<Parser extends IParser>(parser: Parser): IOptional<OptionalParameter<Parser>>;
81
+ export type TupleParameter<Parsers extends IParser[], Result extends unknown[] = []> = StaticEnsure<Parsers extends [infer Left extends IParser, ...infer Right extends IParser[]] ? TupleParameter<Right, [...Result, StaticEnsure<StaticParser<Left>>]> : Result>;
82
+ export interface ITuple<Output extends unknown = unknown> extends IParser<Output> {
83
+ type: 'Tuple';
84
+ parsers: IParser[];
85
+ }
86
+ /** `[BNF]` Creates a Tuple Parser */
87
+ export declare function Tuple<Parsers extends IParser[], Mapping extends IMapping = IMapping<TupleParameter<Parsers>>>(parsers: [...Parsers], mapping: Mapping): ITuple<ReturnType<Mapping>>;
88
+ /** `[BNF]` Creates a Tuple Parser */
89
+ export declare function Tuple<Parsers extends IParser[]>(parsers: [...Parsers]): ITuple<TupleParameter<Parsers>>;
90
+ export type UnionParameter<Parsers extends IParser[], Result extends unknown = never> = StaticEnsure<Parsers extends [infer Left extends IParser, ...infer Right extends IParser[]] ? UnionParameter<Right, Result | StaticParser<Left>> : Result>;
91
+ export interface IUnion<Output extends unknown = unknown> extends IParser<Output> {
92
+ type: 'Union';
93
+ parsers: IParser[];
94
+ }
95
+ /** `[BNF]` Creates a Union parser */
96
+ export declare function Union<Parsers extends IParser[], Mapping extends IMapping = IMapping<UnionParameter<Parsers>>>(parsers: [...Parsers], mapping: Mapping): IUnion<ReturnType<Mapping>>;
97
+ /** `[BNF]` Creates a Union parser */
98
+ export declare function Union<Parsers extends IParser[]>(parsers: [...Parsers]): IUnion<UnionParameter<Parsers>>;
@@ -1,32 +1,55 @@
1
1
  /** Maps input to output. This is the default Mapping */
2
2
  export const Identity = (value) => value;
3
3
  /** Maps the output as the given parameter T */
4
+ // prettier-ignore
4
5
  export const As = (mapping) => (_) => mapping;
5
- export function Tuple(...args) {
6
- const [parsers, mapping] = args.length === 2 ? [args[0], args[1]] : [args[0], Identity];
7
- return { type: 'Tuple', parsers, mapping };
6
+ /** `[Context]` Creates a Context Parser */
7
+ export function Context(...args) {
8
+ const [left, right, mapping] = args.length === 3 ? [args[0], args[1], args[2]] : [args[0], args[1], Identity];
9
+ return { type: 'Context', left, right, mapping };
8
10
  }
9
- export function Union(...args) {
10
- const [parsers, mapping] = args.length === 2 ? [args[0], args[1]] : [args[0], Identity];
11
- return { type: 'Union', parsers, mapping };
11
+ /** `[EBNF]` Creates an Array Parser */
12
+ export function Array(...args) {
13
+ const [parser, mapping] = args.length === 2 ? [args[0], args[1]] : [args[0], Identity];
14
+ return { type: 'Array', parser, mapping };
12
15
  }
16
+ /** `[TERM]` Creates a Const Parser */
13
17
  export function Const(...args) {
14
18
  const [value, mapping] = args.length === 2 ? [args[0], args[1]] : [args[0], Identity];
15
19
  return { type: 'Const', value, mapping };
16
20
  }
21
+ /** `[BNF]` Creates a Ref Parser. This Parser can only be used in the context of a Module */
17
22
  export function Ref(...args) {
18
23
  const [ref, mapping] = args.length === 2 ? [args[0], args[1]] : [args[0], Identity];
19
24
  return { type: 'Ref', ref, mapping };
20
25
  }
26
+ /** `[TERM]` Creates a String Parser. Options are an array of permissable quote characters */
21
27
  export function String(...params) {
22
28
  const [options, mapping] = params.length === 2 ? [params[0], params[1]] : [params[0], Identity];
23
29
  return { type: 'String', options, mapping };
24
30
  }
31
+ /** `[TERM]` Creates an Ident Parser where Ident matches any valid JavaScript identifier */
25
32
  export function Ident(...params) {
26
33
  const mapping = params.length === 1 ? params[0] : Identity;
27
34
  return { type: 'Ident', mapping };
28
35
  }
36
+ /** `[TERM]` Creates an Number Parser */
29
37
  export function Number(...params) {
30
38
  const mapping = params.length === 1 ? params[0] : Identity;
31
39
  return { type: 'Number', mapping };
32
40
  }
41
+ /** `[EBNF]` Creates an Optional Parser */
42
+ export function Optional(...args) {
43
+ const [parser, mapping] = args.length === 2 ? [args[0], args[1]] : [args[0], Identity];
44
+ return { type: 'Optional', parser, mapping };
45
+ }
46
+ /** `[BNF]` Creates a Tuple Parser */
47
+ export function Tuple(...args) {
48
+ const [parsers, mapping] = args.length === 2 ? [args[0], args[1]] : [args[0], Identity];
49
+ return { type: 'Tuple', parsers, mapping };
50
+ }
51
+ /** `[BNF]` Creates a Union parser */
52
+ export function Union(...args) {
53
+ const [parsers, mapping] = args.length === 2 ? [args[0], args[1]] : [args[0], Identity];
54
+ return { type: 'Union', parsers, mapping };
55
+ }
@@ -1,12 +1,15 @@
1
1
  import * as Tokens from './token.mjs';
2
2
  import * as Types from './types.mjs';
3
- type TupleParser<Parsers extends Types.IParser[], Code extends string, Context extends unknown, Result extends unknown[] = []> = (Parsers extends [infer Left extends Types.IParser, ...infer Right extends Types.IParser[]] ? Parse<Left, Code, Context> extends [infer Value extends unknown, infer Rest extends string] ? TupleParser<Right, Rest, Context, [...Result, Value]> : [] : [Result, Code]);
4
- type UnionParser<Parsers extends Types.IParser[], Code extends string, Context extends unknown> = (Parsers extends [infer Left extends Types.IParser, ...infer Right extends Types.IParser[]] ? Parse<Left, Code, Context> extends [infer Value extends unknown, infer Rest extends string] ? [Value, Rest] : UnionParser<Right, Code, Context> : []);
3
+ type ContextParser<Left extends Types.IParser, Right extends Types.IParser, Code extends string, Context extends unknown> = (Parse<Left, Code, Context> extends [infer Context extends unknown, infer Rest extends string] ? Parse<Right, Rest, Context> : []);
4
+ type ArrayParser<Parser extends Types.IParser, Code extends string, Context extends unknown, Result extends unknown[] = []> = (Parse<Parser, Code, Context> extends [infer Value1 extends unknown, infer Rest extends string] ? ArrayParser<Parser, Rest, Context, [...Result, Value1]> : [Result, Code]);
5
5
  type ConstParser<Value extends string, Code extends string, _Context extends unknown> = (Tokens.Const<Value, Code> extends [infer Match extends Value, infer Rest extends string] ? [Match, Rest] : []);
6
6
  type IdentParser<Code extends string, _Context extends unknown> = (Tokens.Ident<Code> extends [infer Match extends string, infer Rest extends string] ? [Match, Rest] : []);
7
7
  type NumberParser<Code extends string, _Context extends unknown> = (Tokens.Number<Code> extends [infer Match extends string, infer Rest extends string] ? [Match, Rest] : []);
8
+ type OptionalParser<Parser extends Types.IParser, Code extends string, Context extends unknown> = (Parse<Parser, Code, Context> extends [infer Value extends unknown, infer Rest extends string] ? [[Value], Rest] : [[], Code]);
8
9
  type StringParser<Options extends string[], Code extends string, _Context extends unknown> = (Tokens.String<Options, Code> extends [infer Match extends string, infer Rest extends string] ? [Match, Rest] : []);
9
- type ParseCode<Type extends Types.IParser, Code extends string, Context extends unknown = unknown> = (Type extends Types.Union<infer S extends Types.IParser[]> ? UnionParser<S, Code, Context> : Type extends Types.Tuple<infer S extends Types.IParser[]> ? TupleParser<S, Code, Context> : Type extends Types.Const<infer S extends string> ? ConstParser<S, Code, Context> : Type extends Types.String<infer S extends string[]> ? StringParser<S, Code, Context> : Type extends Types.Ident ? IdentParser<Code, Context> : Type extends Types.Number ? NumberParser<Code, Context> : [
10
+ type TupleParser<Parsers extends Types.IParser[], Code extends string, Context extends unknown, Result extends unknown[] = []> = (Parsers extends [infer Left extends Types.IParser, ...infer Right extends Types.IParser[]] ? Parse<Left, Code, Context> extends [infer Value extends unknown, infer Rest extends string] ? TupleParser<Right, Rest, Context, [...Result, Value]> : [] : [Result, Code]);
11
+ type UnionParser<Parsers extends Types.IParser[], Code extends string, Context extends unknown> = (Parsers extends [infer Left extends Types.IParser, ...infer Right extends Types.IParser[]] ? Parse<Left, Code, Context> extends [infer Value extends unknown, infer Rest extends string] ? [Value, Rest] : UnionParser<Right, Code, Context> : []);
12
+ type ParseCode<Type extends Types.IParser, Code extends string, Context extends unknown = unknown> = (Type extends Types.Context<infer Left extends Types.IParser, infer Right extends Types.IParser> ? ContextParser<Left, Right, Code, Context> : Type extends Types.Array<infer Parser extends Types.IParser> ? ArrayParser<Parser, Code, Context> : Type extends Types.Const<infer Value extends string> ? ConstParser<Value, Code, Context> : Type extends Types.Ident ? IdentParser<Code, Context> : Type extends Types.Number ? NumberParser<Code, Context> : Type extends Types.Optional<infer Parser extends Types.IParser> ? OptionalParser<Parser, Code, Context> : Type extends Types.String<infer Options extends string[]> ? StringParser<Options, Code, Context> : Type extends Types.Tuple<infer Parsers extends Types.IParser[]> ? TupleParser<Parsers, Code, Context> : Type extends Types.Union<infer Parsers extends Types.IParser[]> ? UnionParser<Parsers, Code, Context> : [
10
13
  ]);
11
14
  type ParseMapping<Parser extends Types.IParser, Result extends unknown, Context extends unknown = unknown> = ((Parser['mapping'] & {
12
15
  input: Result;
@@ -1,13 +1,20 @@
1
+ /**
2
+ * `[ACTION]` Inference mapping base type. Used to specify semantic actions for
3
+ * Parser productions. This type is implemented as a higher-kinded type where
4
+ * productions are received on the `input` property with mapping assigned
5
+ * the `output` property. The parsing context is available on the `context`
6
+ * property.
7
+ */
1
8
  export interface IMapping {
2
9
  context: unknown;
3
10
  input: unknown;
4
11
  output: unknown;
5
12
  }
6
- /** Maps input to output. This is the default Mapping */
13
+ /** `[ACTION]` Default inference mapping. */
7
14
  export interface Identity extends IMapping {
8
15
  output: this['input'];
9
16
  }
10
- /** Maps the output as the given parameter T */
17
+ /** `[ACTION]` Maps the given argument `T` as the mapping output */
11
18
  export interface As<T> extends IMapping {
12
19
  output: T;
13
20
  }
@@ -16,31 +23,47 @@ export interface IParser<Mapping extends IMapping = Identity> {
16
23
  type: string;
17
24
  mapping: Mapping;
18
25
  }
19
- /** Creates a Tuple Parser */
20
- export interface Tuple<Parsers extends IParser[] = [], Mapping extends IMapping = Identity> extends IParser<Mapping> {
21
- type: 'Tuple';
22
- parsers: [...Parsers];
26
+ /** `[Context]` Creates a Context Parser */
27
+ export interface Context<Left extends IParser = IParser, Right extends IParser = IParser, Mapping extends IMapping = Identity> extends IParser<Mapping> {
28
+ type: 'Context';
29
+ left: Left;
30
+ right: Right;
23
31
  }
24
- /** Creates a Union Parser */
25
- export interface Union<Parsers extends IParser[] = [], Mapping extends IMapping = Identity> extends IParser<Mapping> {
26
- type: 'Union';
27
- parsers: [...Parsers];
32
+ /** `[EBNF]` Creates an Array Parser */
33
+ export interface Array<Parser extends IParser = IParser, Mapping extends IMapping = Identity> extends IParser<Mapping> {
34
+ type: 'Array';
35
+ parser: Parser;
28
36
  }
29
- /** Creates a Const Parser */
37
+ /** `[TERM]` Creates a Const Parser */
30
38
  export interface Const<Value extends string = string, Mapping extends IMapping = Identity> extends IParser<Mapping> {
31
39
  type: 'Const';
32
40
  value: Value;
33
41
  }
34
- /** Creates a String Parser. Options are an array of permissable quote characters */
35
- export interface String<Options extends string[], Mapping extends IMapping = Identity> extends IParser<Mapping> {
36
- type: 'String';
37
- quote: Options;
38
- }
39
- /** Creates an Ident Parser. */
42
+ /** `[TERM]` Creates an Ident Parser. */
40
43
  export interface Ident<Mapping extends IMapping = Identity> extends IParser<Mapping> {
41
44
  type: 'Ident';
42
45
  }
43
- /** Creates a Number Parser. */
46
+ /** `[TERM]` Creates a Number Parser. */
44
47
  export interface Number<Mapping extends IMapping = Identity> extends IParser<Mapping> {
45
48
  type: 'Number';
46
49
  }
50
+ /** `[EBNF]` Creates a Optional Parser */
51
+ export interface Optional<Parser extends IParser = IParser, Mapping extends IMapping = Identity> extends IParser<Mapping> {
52
+ type: 'Optional';
53
+ parser: Parser;
54
+ }
55
+ /** `[TERM]` Creates a String Parser. Options are an array of permissable quote characters */
56
+ export interface String<Options extends string[], Mapping extends IMapping = Identity> extends IParser<Mapping> {
57
+ type: 'String';
58
+ quote: Options;
59
+ }
60
+ /** `[BNF]` Creates a Tuple Parser */
61
+ export interface Tuple<Parsers extends IParser[] = [], Mapping extends IMapping = Identity> extends IParser<Mapping> {
62
+ type: 'Tuple';
63
+ parsers: [...Parsers];
64
+ }
65
+ /** `[BNF]` Creates a Union Parser */
66
+ export interface Union<Parsers extends IParser[] = [], Mapping extends IMapping = Identity> extends IParser<Mapping> {
67
+ type: 'Union';
68
+ parsers: [...Parsers];
69
+ }
@@ -2,7 +2,7 @@ import { Runtime } from '../parser/index.mjs';
2
2
  import * as t from '../type/index.mjs';
3
3
  export declare const Module: Runtime.Module<{
4
4
  GenericArgumentList: Runtime.IUnion<unknown[]>;
5
- GenericArguments: Runtime.ITuple<{}>;
5
+ GenericArguments: Runtime.ITuple<t.TProperties>;
6
6
  Literal: Runtime.IUnion<t.TLiteral<string> | t.TLiteral<number> | t.TLiteral<boolean>>;
7
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>;
8
8
  KeyOf: Runtime.IUnion<boolean>;
@@ -14,7 +14,7 @@ export declare const Module: Runtime.Module<{
14
14
  ExprTerm: Runtime.ITuple<t.TSchema>;
15
15
  ExprTail: Runtime.IUnion<[] | ["|", unknown, unknown]>;
16
16
  Expr: Runtime.ITuple<t.TSchema>;
17
- Type: Runtime.IRef<unknown>;
17
+ Type: Runtime.IUnion<unknown>;
18
18
  PropertyKey: Runtime.IUnion<string>;
19
19
  Readonly: Runtime.IUnion<boolean>;
20
20
  Optional: Runtime.IUnion<boolean>;
@@ -56,15 +56,15 @@ const GenericArgumentList = Runtime.Union([
56
56
  // GenericArguments
57
57
  // ------------------------------------------------------------------
58
58
  // prettier-ignore
59
- const GenericArgumentsContext = (args) => {
59
+ const GenericArgumentsContext = (args, context) => {
60
60
  return args.reduce((result, arg, index) => {
61
61
  return { ...result, [arg]: t.Argument(index) };
62
- }, {});
62
+ }, context);
63
63
  };
64
64
  // prettier-ignore
65
- const GenericArgumentsMapping = (results) => {
65
+ const GenericArgumentsMapping = (results, context) => {
66
66
  return results.length === 3
67
- ? GenericArgumentsContext(results[1])
67
+ ? GenericArgumentsContext(results[1], context)
68
68
  : {};
69
69
  };
70
70
  // prettier-ignore
@@ -72,7 +72,7 @@ const GenericArguments = Runtime.Tuple([
72
72
  Runtime.Const(LAngle),
73
73
  Runtime.Ref('GenericArgumentList'),
74
74
  Runtime.Const(RAngle),
75
- ], results => GenericArgumentsMapping(results));
75
+ ], (results, context) => GenericArgumentsMapping(results, context));
76
76
  // ------------------------------------------------------------------
77
77
  // GenericReference
78
78
  // ------------------------------------------------------------------
@@ -282,7 +282,11 @@ const Expr = Runtime.Tuple([
282
282
  // ------------------------------------------------------------------
283
283
  // Type
284
284
  // ------------------------------------------------------------------
285
- const Type = Runtime.Ref('Expr');
285
+ // prettier-ignore
286
+ const Type = Runtime.Union([
287
+ Runtime.Context(Runtime.Ref('GenericArguments'), Runtime.Ref('Expr')),
288
+ Runtime.Ref('Expr')
289
+ ]);
286
290
  // ------------------------------------------------------------------
287
291
  // Properties
288
292
  // ------------------------------------------------------------------
@@ -649,12 +653,12 @@ const Uint8Array = Runtime.Const('Uint8Array', Runtime.As(t.Uint8Array()));
649
653
  // prettier-ignore
650
654
  export const Module = new Runtime.Module({
651
655
  // ----------------------------------------------------------------
652
- // Generic Arguments
656
+ // Generics
653
657
  // ----------------------------------------------------------------
654
658
  GenericArgumentList,
655
659
  GenericArguments,
656
660
  // ----------------------------------------------------------------
657
- // Type Expressions
661
+ // Type
658
662
  // ----------------------------------------------------------------
659
663
  Literal,
660
664
  Keyword,
@@ -667,7 +671,7 @@ export const Module = new Runtime.Module({
667
671
  ExprTerm,
668
672
  ExprTail,
669
673
  Expr,
670
- Type, // Alias for Expr
674
+ Type,
671
675
  PropertyKey,
672
676
  Readonly,
673
677
  Optional,
@@ -52,13 +52,13 @@ type GenericArgumentList = Static.Union<[
52
52
  Static.Tuple<[Static.Ident]>,
53
53
  Static.Tuple<[]>
54
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 & {
55
+ type GenericArgumentsContext<Args extends string[], Context extends t.TProperties, Result extends t.TProperties = {}> = (Args extends [...infer Left extends string[], infer Right extends string] ? GenericArgumentsContext<Left, Context, Result & {
56
56
  [_ in Right]: t.TArgument<Left['length']>;
57
- }> : t.Evaluate<Result>);
57
+ }> : t.Evaluate<Result & Context>);
58
58
  interface GenericArgumentsMapping extends Static.IMapping {
59
- output: this['input'] extends [LAngle, infer Args extends string[], RAngle] ? GenericArgumentsContext<Args> : never;
59
+ output: this['input'] extends [LAngle, infer Args extends string[], RAngle] ? this['context'] extends infer Context extends t.TProperties ? GenericArgumentsContext<Args, Context> : never : never;
60
60
  }
61
- export type GenericArguments = Static.Tuple<[
61
+ type GenericArguments = Static.Tuple<[
62
62
  Static.Const<LAngle>,
63
63
  GenericArgumentList,
64
64
  Static.Const<RAngle>
@@ -204,7 +204,10 @@ type Expr = Static.Tuple<[
204
204
  ExprTerm,
205
205
  ExprTail
206
206
  ], ExprBinaryMapping>;
207
- export type Type = Expr;
207
+ export type Type = Static.Union<[
208
+ Static.Context<GenericArguments, Expr>,
209
+ Expr
210
+ ]>;
208
211
  interface PropertyKeyStringMapping extends Static.IMapping {
209
212
  output: this['input'];
210
213
  }
@@ -1,16 +1,15 @@
1
1
  import * as t from '../type/index.mjs';
2
2
  import { Static } from '../parser/index.mjs';
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 */
3
+ import { Type } from './static.mjs';
4
+ /** `[Experimental]` Parses a TypeScript annotation into a TypeBox type but does not infer schematics */
6
5
  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 */
6
+ /** `[Experimental]` Parses a TypeScript annotation into a TypeBox type but does not infer schematics */
8
7
  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 */
8
+ /** `[Experimental]` Parses a TypeScript annotation into a TypeBox type */
9
+ export type TSyntax<Context extends Record<PropertyKey, t.TSchema>, Code extends string> = (Static.Parse<Type, Code, Context> extends [infer Type extends t.TSchema, string] ? Type : t.TNever);
10
+ /** `[Experimental]` Parses a TypeScript annotation into a TypeBox type */
12
11
  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 */
12
+ /** `[Experimental]` Parses a TypeScript annotation into a TypeBox type */
14
13
  export declare function Syntax<Annotation extends string>(annotation: Annotation, options?: t.SchemaOptions): TSyntax<{}, Annotation>;
15
14
  /**
16
15
  * @deprecated Use Syntax() function
@@ -28,4 +27,3 @@ export declare function ParseOnly<Context extends Record<PropertyKey, t.TSchema>
28
27
  * @deprecated Use NoInfer() function
29
28
  */
30
29
  export declare function ParseOnly<Code extends string>(code: Code, options?: t.SchemaOptions): t.TSchema | undefined;
31
- export {};
@@ -1,23 +1,16 @@
1
1
  import * as t from '../type/index.mjs';
2
2
  import { Module } from './runtime.mjs';
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 */
3
+ /** `[Experimental]` Parses a TypeScript annotation into a TypeBox type but does not infer schematics */
11
4
  // prettier-ignore
12
5
  export function NoInfer(...args) {
13
6
  const withContext = typeof args[0] === 'string' ? false : true;
14
7
  const [context, code, options] = withContext ? [args[0], args[1], args[2] || {}] : [{}, args[0], args[1] || {}];
15
- const result = ParseSyntax(context, code)[0];
8
+ const result = Module.Parse('Type', code, context)[0];
16
9
  return t.KindGuard.IsSchema(result)
17
10
  ? t.CloneType(result, options)
18
11
  : t.Never(options);
19
12
  }
20
- /** Parses a TypeScript annotation into a TypeBox type */
13
+ /** `[Experimental]` Parses a TypeScript annotation into a TypeBox type */
21
14
  export function Syntax(...args) {
22
15
  return NoInfer.apply(null, args);
23
16
  }
@@ -24,7 +24,7 @@ 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 TProperties = TFromProperties<Args, Properties>> = TObject<Result>;
27
+ type TFromObject<Args extends TSchema[], Properties extends TProperties, MappedProperties extends TProperties = TFromProperties<Args, Properties>, Result extends TObject = TObject<MappedProperties>> = 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
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 = ([
@@ -58,8 +58,8 @@ function FromPromise(args, type) {
58
58
  }
59
59
  // prettier-ignore
60
60
  function FromObject(args, type) {
61
- const properties = FromProperties(args, type.properties);
62
- return { ...type, ...Object(properties) }; // retain options
61
+ const mappedProperties = FromProperties(args, type.properties);
62
+ return { ...type, ...Object(mappedProperties) }; // retain options
63
63
  }
64
64
  // prettier-ignore
65
65
  function FromRecord(args, type) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sinclair/typebox",
3
- "version": "0.34.24",
3
+ "version": "0.34.26",
4
4
  "description": "Json Schema Type Builder with Static Type Resolution for TypeScript",
5
5
  "keywords": [
6
6
  "typescript",
package/readme.md CHANGED
@@ -100,6 +100,7 @@ License MIT
100
100
  - [Parameters](#syntax-parameters)
101
101
  - [Generics](#syntax-generics)
102
102
  - [Options](#syntax-options)
103
+ - [NoInfer](#syntax-no-infer)
103
104
  - [TypeRegistry](#typeregistry)
104
105
  - [Type](#typeregistry-type)
105
106
  - [Format](#typeregistry-format)
@@ -1306,7 +1307,7 @@ ValuePointer.Set(A, '/z', 1) // A' = { x: 1, y: 1, z: 1
1306
1307
 
1307
1308
  ## Syntax Types
1308
1309
 
1309
- TypeBox includes support for parsing TypeScript annotation syntax into TypeBox schematics.
1310
+ TypeBox provides experimental support for parsing TypeScript annotation syntax into TypeBox types.
1310
1311
 
1311
1312
  This feature is provided via optional import.
1312
1313
 
@@ -1391,6 +1392,23 @@ const T = Syntax(`number`, { minimum: 42 }) // const T = {
1391
1392
  // }
1392
1393
  ```
1393
1394
 
1395
+ <a name='syntax-no-infer'></a>
1396
+
1397
+ ### NoInfer
1398
+
1399
+ Syntax parsing is an expensive type level operation and can impact on language service performance. Use the NoInfer function parse syntax at runtime only.
1400
+
1401
+ ```typescript
1402
+ import { NoInfer } from '@sinclair/typebox/syntax'
1403
+
1404
+ const T = NoInfer(`number | string`) // const T: TSchema = {
1405
+ // anyOf: [
1406
+ // { type: 'number' },
1407
+ // { type: 'string' }
1408
+ // ]
1409
+ // }
1410
+ ```
1411
+
1394
1412
  <a name='typeregistry'></a>
1395
1413
 
1396
1414
  ## TypeRegistry