arktype 2.0.0-rc.3 → 2.0.0-rc.30
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/out/attributes.d.ts +94 -0
- package/out/attributes.js +3 -0
- package/out/config.d.ts +1 -1
- package/out/generic.d.ts +55 -58
- package/out/generic.js +6 -8
- package/out/index.d.ts +6 -4
- package/out/index.js +4 -3
- package/out/keywords/{constructors/Array.d.ts → Array.d.ts} +3 -3
- package/out/keywords/{constructors/Array.js → Array.js} +7 -7
- package/out/keywords/{constructors/FormData.d.ts → FormData.d.ts} +3 -3
- package/out/keywords/{constructors/FormData.js → FormData.js} +8 -8
- package/out/keywords/{constructors/TypedArray.d.ts → TypedArray.d.ts} +1 -1
- package/out/keywords/{constructors/TypedArray.js → TypedArray.js} +2 -2
- package/out/keywords/builtins.js +2 -2
- package/out/keywords/{constructors/constructors.d.ts → constructors.d.ts} +1 -2
- package/out/keywords/{constructors/constructors.js → constructors.js} +2 -2
- package/out/keywords/keywords.d.ts +61 -0
- package/out/keywords/keywords.js +29 -0
- package/out/keywords/number.d.ts +23 -0
- package/out/keywords/{number/epoch.js → number.js} +24 -2
- package/out/keywords/string.d.ts +327 -0
- package/out/keywords/string.js +398 -0
- package/out/keywords/ts.d.ts +42 -4
- package/out/keywords/ts.js +24 -7
- package/out/methods/array.d.ts +12 -0
- package/out/methods/base.d.ts +178 -0
- package/out/methods/date.d.ts +10 -0
- package/out/methods/instantiate.d.ts +17 -0
- package/out/methods/morph.d.ts +12 -0
- package/out/methods/number.d.ts +11 -0
- package/out/methods/object.d.ts +106 -0
- package/out/methods/string.d.ts +12 -0
- package/out/module.d.ts +2 -2
- package/out/parser/{semantic → ast}/bounds.d.ts +3 -3
- package/out/parser/ast/default.d.ts +8 -0
- package/out/parser/ast/generic.d.ts +17 -0
- package/out/parser/ast/infer.d.ts +25 -0
- package/out/parser/{semantic → ast}/utils.d.ts +1 -1
- package/out/parser/ast/validate.d.ts +30 -0
- package/out/parser/ast/validate.js +3 -0
- package/out/parser/definition.d.ts +21 -13
- package/out/parser/definition.js +20 -2
- package/out/parser/objectLiteral.d.ts +41 -67
- package/out/parser/objectLiteral.js +79 -71
- package/out/parser/property.d.ts +36 -0
- package/out/parser/property.js +16 -0
- package/out/parser/{string/reduce → reduce}/dynamic.d.ts +10 -13
- package/out/parser/{string/reduce → reduce}/dynamic.js +5 -6
- package/out/parser/{string/reduce → reduce}/shared.d.ts +1 -1
- package/out/parser/{string/reduce → reduce}/static.d.ts +4 -4
- package/out/parser/{string/shift → shift}/operand/date.d.ts +3 -1
- package/out/parser/{string/shift → shift}/operand/enclosed.d.ts +4 -4
- package/out/parser/{string/shift → shift}/operand/enclosed.js +7 -3
- package/out/parser/shift/operand/operand.d.ts +9 -0
- package/out/parser/{string/shift → shift}/operand/operand.js +2 -2
- package/out/parser/{string/shift → shift}/operand/unenclosed.d.ts +15 -7
- package/out/parser/{string/shift → shift}/operand/unenclosed.js +12 -7
- package/out/parser/{string/shift → shift}/operator/bounds.d.ts +7 -7
- package/out/parser/{string/shift → shift}/operator/bounds.js +5 -4
- package/out/parser/shift/operator/brand.d.ts +6 -0
- package/out/parser/shift/operator/brand.js +5 -0
- package/out/parser/{string/shift → shift}/operator/default.d.ts +4 -6
- package/out/parser/{string/shift → shift}/operator/default.js +4 -6
- package/out/parser/{string/shift → shift}/operator/divisor.d.ts +2 -2
- package/out/parser/shift/operator/operator.d.ts +14 -0
- package/out/parser/{string/shift → shift}/operator/operator.js +7 -5
- package/out/parser/shift/scanner.d.ts +47 -0
- package/out/parser/shift/scanner.js +43 -0
- package/out/parser/{string/string.d.ts → string.d.ts} +9 -9
- package/out/parser/{string/string.js → string.js} +19 -0
- package/out/parser/tupleExpressions.d.ts +47 -0
- package/out/parser/tupleExpressions.js +67 -0
- package/out/parser/tupleLiteral.d.ts +95 -0
- package/out/parser/tupleLiteral.js +122 -0
- package/out/scope.d.ts +41 -34
- package/out/scope.js +54 -62
- package/out/type.d.ts +46 -22
- package/out/type.js +15 -11
- package/package.json +3 -3
- package/out/intersect.d.ts +0 -28
- package/out/keys.d.ts +0 -16
- package/out/keywords/ark.d.ts +0 -39
- package/out/keywords/ark.js +0 -24
- package/out/keywords/ast.d.ts +0 -193
- package/out/keywords/ast.js +0 -2
- package/out/keywords/number/epoch.d.ts +0 -10
- package/out/keywords/number/integer.d.ts +0 -3
- package/out/keywords/number/integer.js +0 -5
- package/out/keywords/number/number.d.ts +0 -28
- package/out/keywords/number/number.js +0 -9
- package/out/keywords/string/alpha.d.ts +0 -7
- package/out/keywords/string/alpha.js +0 -2
- package/out/keywords/string/alphanumeric.d.ts +0 -7
- package/out/keywords/string/alphanumeric.js +0 -2
- package/out/keywords/string/capitalize.d.ts +0 -15
- package/out/keywords/string/capitalize.js +0 -12
- package/out/keywords/string/creditCard.d.ts +0 -8
- package/out/keywords/string/creditCard.js +0 -37
- package/out/keywords/string/date.d.ts +0 -59
- package/out/keywords/string/date.js +0 -98
- package/out/keywords/string/digits.d.ts +0 -7
- package/out/keywords/string/digits.js +0 -2
- package/out/keywords/string/email.d.ts +0 -7
- package/out/keywords/string/email.js +0 -4
- package/out/keywords/string/integer.d.ts +0 -16
- package/out/keywords/string/integer.js +0 -16
- package/out/keywords/string/ip.d.ts +0 -20
- package/out/keywords/string/ip.js +0 -23
- package/out/keywords/string/json.d.ts +0 -15
- package/out/keywords/string/json.js +0 -45
- package/out/keywords/string/lower.d.ts +0 -15
- package/out/keywords/string/lower.js +0 -12
- package/out/keywords/string/normalize.d.ts +0 -92
- package/out/keywords/string/normalize.js +0 -43
- package/out/keywords/string/numeric.d.ts +0 -15
- package/out/keywords/string/numeric.js +0 -13
- package/out/keywords/string/semver.d.ts +0 -7
- package/out/keywords/string/semver.js +0 -4
- package/out/keywords/string/string.d.ts +0 -112
- package/out/keywords/string/string.js +0 -41
- package/out/keywords/string/trim.d.ts +0 -15
- package/out/keywords/string/trim.js +0 -14
- package/out/keywords/string/upper.d.ts +0 -15
- package/out/keywords/string/upper.js +0 -12
- package/out/keywords/string/url.d.ts +0 -15
- package/out/keywords/string/url.js +0 -37
- package/out/keywords/string/utils.d.ts +0 -2
- package/out/keywords/string/utils.js +0 -10
- package/out/keywords/string/uuid.d.ts +0 -42
- package/out/keywords/string/uuid.js +0 -19
- package/out/keywords/utils.d.ts +0 -1
- package/out/keywords/utils.js +0 -2
- package/out/parser/semantic/default.d.ts +0 -9
- package/out/parser/semantic/default.js +0 -1
- package/out/parser/semantic/infer.d.ts +0 -39
- package/out/parser/semantic/validate.d.ts +0 -26
- package/out/parser/semantic/validate.js +0 -1
- package/out/parser/string/shift/operand/operand.d.ts +0 -9
- package/out/parser/string/shift/operator/operator.d.ts +0 -13
- package/out/parser/string/shift/scanner.d.ts +0 -69
- package/out/parser/string/shift/scanner.js +0 -100
- package/out/parser/tuple.d.ts +0 -132
- package/out/parser/tuple.js +0 -158
- package/out/subtypes/array.d.ts +0 -13
- package/out/subtypes/base.d.ts +0 -85
- package/out/subtypes/date.d.ts +0 -11
- package/out/subtypes/instantiate.d.ts +0 -16
- package/out/subtypes/morph.d.ts +0 -13
- package/out/subtypes/number.d.ts +0 -12
- package/out/subtypes/object.d.ts +0 -33
- package/out/subtypes/string.d.ts +0 -13
- package/out/subtypes/validator.d.ts +0 -11
- package/out/subtypes/validator.js +0 -1
- /package/out/{subtypes → methods}/array.js +0 -0
- /package/out/{subtypes → methods}/base.js +0 -0
- /package/out/{subtypes → methods}/date.js +0 -0
- /package/out/{subtypes → methods}/instantiate.js +0 -0
- /package/out/{subtypes → methods}/morph.js +0 -0
- /package/out/{subtypes → methods}/number.js +0 -0
- /package/out/{subtypes → methods}/object.js +0 -0
- /package/out/{subtypes → methods}/string.js +0 -0
- /package/out/parser/{semantic → ast}/bounds.js +0 -0
- /package/out/{intersect.js → parser/ast/default.js} +0 -0
- /package/out/parser/{semantic → ast}/divisor.d.ts +0 -0
- /package/out/parser/{semantic → ast}/divisor.js +0 -0
- /package/out/{keys.js → parser/ast/generic.js} +0 -0
- /package/out/parser/{semantic → ast}/infer.js +0 -0
- /package/out/parser/{semantic → ast}/keyof.d.ts +0 -0
- /package/out/parser/{semantic → ast}/keyof.js +0 -0
- /package/out/parser/{semantic → ast}/utils.js +0 -0
- /package/out/parser/{string/reduce → reduce}/shared.js +0 -0
- /package/out/parser/{string/reduce → reduce}/static.js +0 -0
- /package/out/parser/{string/shift → shift}/operand/date.js +0 -0
- /package/out/parser/{string/shift → shift}/operand/genericArgs.d.ts +0 -0
- /package/out/parser/{string/shift → shift}/operand/genericArgs.js +0 -0
- /package/out/parser/{string/shift → shift}/operator/divisor.js +0 -0
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import type { ArkErrors, BaseRoot, Disjoint, JsonSchema, MetaSchema, Morph, Predicate, PredicateCast, StandardSchemaV1, UndeclaredKeyBehavior } from "@ark/schema";
|
|
2
|
+
import type { anyOrNever, array, Callable, ErrorMessage, inferred, JsonStructure, unset } from "@ark/util";
|
|
3
|
+
import type { defaultFor, distill, inferIntersection, inferMorphOut, inferPipes, InferredMorph, Out, To } from "../attributes.ts";
|
|
4
|
+
import type { ArkAmbient } from "../config.ts";
|
|
5
|
+
import type { type } from "../keywords/keywords.ts";
|
|
6
|
+
import type { Scope } from "../scope.ts";
|
|
7
|
+
import type { ArrayType } from "./array.ts";
|
|
8
|
+
import type { instantiateType } from "./instantiate.ts";
|
|
9
|
+
/** @ts-ignore cast variance */
|
|
10
|
+
interface Type<out t = unknown, $ = {}> extends Callable<(data: unknown) => distill.Out<t> | ArkErrors> {
|
|
11
|
+
[inferred]: t;
|
|
12
|
+
t: t;
|
|
13
|
+
infer: this["inferOut"];
|
|
14
|
+
inferIntrospectableOut: distill.introspectable.Out<t>;
|
|
15
|
+
inferOut: distill.Out<t>;
|
|
16
|
+
inferIn: distill.In<t>;
|
|
17
|
+
inferredOutIsIntrospectable: t extends InferredMorph<any, infer o> ? [
|
|
18
|
+
o
|
|
19
|
+
] extends [anyOrNever] ? true : o extends To ? true : false : unknown extends t ? boolean : true;
|
|
20
|
+
/** Internal JSON representation of this `Type` */
|
|
21
|
+
json: JsonStructure;
|
|
22
|
+
toJSON(): JsonStructure;
|
|
23
|
+
meta: ArkAmbient.meta;
|
|
24
|
+
precompilation: string | undefined;
|
|
25
|
+
toJsonSchema(): JsonSchema;
|
|
26
|
+
description: string;
|
|
27
|
+
expression: string;
|
|
28
|
+
internal: BaseRoot;
|
|
29
|
+
$: Scope<$>;
|
|
30
|
+
/**
|
|
31
|
+
* Validate data, throwing `type.errors` instance on failure
|
|
32
|
+
* @returns a valid value
|
|
33
|
+
* @example const validData = T.assert(rawData)
|
|
34
|
+
*/
|
|
35
|
+
assert(data: unknown): this["infer"];
|
|
36
|
+
/**
|
|
37
|
+
* Check if data matches the input shape.
|
|
38
|
+
* Doesn't process any morphs, but does check narrows.
|
|
39
|
+
* @example type({ foo: "number" }).allows({ foo: "bar" }) // false
|
|
40
|
+
*/
|
|
41
|
+
allows(data: unknown): data is this["inferIn"];
|
|
42
|
+
traverse(data: unknown): this["infer"] | ArkErrors;
|
|
43
|
+
configure(meta: MetaSchema): this;
|
|
44
|
+
describe(description: string): this;
|
|
45
|
+
/**
|
|
46
|
+
* Create a copy of this `Type` with updated unknown key behavior
|
|
47
|
+
* - `ignore`: ignore unknown properties (default)
|
|
48
|
+
* - 'reject': disallow objects with unknown properties
|
|
49
|
+
* - 'delete': clone the object and keep only known properties
|
|
50
|
+
*/
|
|
51
|
+
onUndeclaredKey(behavior: UndeclaredKeyBehavior): this;
|
|
52
|
+
/**
|
|
53
|
+
* Create a copy of this `Type` with updated unknown key behavior\
|
|
54
|
+
* The behavior applies to the whole object tree, not just the immediate properties.
|
|
55
|
+
* - `ignore`: ignore unknown properties (default)
|
|
56
|
+
* - 'reject': disallow objects with unknown properties
|
|
57
|
+
* - 'delete': clone the object and keep only known properties
|
|
58
|
+
*/
|
|
59
|
+
onDeepUndeclaredKey(behavior: UndeclaredKeyBehavior): this;
|
|
60
|
+
/**
|
|
61
|
+
* Identical to `assert`, but with a typed input as a convenience for providing a typed value.
|
|
62
|
+
* @example const ConfigT = type({ foo: "string" }); export const config = ConfigT.from({ foo: "bar" })
|
|
63
|
+
*/
|
|
64
|
+
from(literal: this["inferIn"]): this["infer"];
|
|
65
|
+
/**
|
|
66
|
+
* Cast the way this `Type` is inferred (has no effect at runtime).
|
|
67
|
+
* const branded = type(/^a/).as<`a${string}`>() // Type<`a${string}`>
|
|
68
|
+
*/
|
|
69
|
+
as<castTo = unset>(...args: validateChainedAsArgs<castTo>): instantiateType<castTo, $>;
|
|
70
|
+
brand<const name extends string, r = type.brand<t, name>>(name: name): instantiateType<r, $>;
|
|
71
|
+
/**
|
|
72
|
+
* A `Type` representing the deeply-extracted input of the `Type` (before morphs are applied).
|
|
73
|
+
* @example const inputT = T.in
|
|
74
|
+
*/
|
|
75
|
+
get in(): instantiateType<this["inferIn"], $>;
|
|
76
|
+
/**
|
|
77
|
+
* A `Type` representing the deeply-extracted output of the `Type` (after morphs are applied).\
|
|
78
|
+
* **IMPORTANT**: If your type includes morphs, their output will likely be unknown
|
|
79
|
+
* unless they were defined with an explicit output validator via `.to(outputType)`, `.pipe(morph, outputType)`, etc.
|
|
80
|
+
* @example const outputT = T.out
|
|
81
|
+
*/
|
|
82
|
+
get out(): instantiateType<this["inferIntrospectableOut"], $>;
|
|
83
|
+
/**
|
|
84
|
+
* Intersect another `Type` definition, returning an introspectable `Disjoint` if the result is unsatisfiable.
|
|
85
|
+
* @example const intersection = type({ foo: "number" }).intersect({ bar: "string" }) // Type<{ foo: number; bar: string }>
|
|
86
|
+
* @example const intersection = type({ foo: "number" }).intersect({ foo: "string" }) // Disjoint
|
|
87
|
+
*/
|
|
88
|
+
intersect<const def, r = type.infer<def, $>>(def: type.validate<def, $>): instantiateType<inferIntersection<t, r>, $> | Disjoint;
|
|
89
|
+
/**
|
|
90
|
+
* Intersect another `Type` definition, throwing an error if the result is unsatisfiable.
|
|
91
|
+
* @example const intersection = type({ foo: "number" }).intersect({ bar: "string" }) // Type<{ foo: number; bar: string }>
|
|
92
|
+
*/
|
|
93
|
+
and<const def, r = type.infer<def, $>>(def: type.validate<def, $>): instantiateType<inferIntersection<t, r>, $>;
|
|
94
|
+
/**
|
|
95
|
+
* Union another `Type` definition.\
|
|
96
|
+
* If the types contain morphs, input shapes should be distinct. Otherwise an error will be thrown.
|
|
97
|
+
* @example const union = type({ foo: "number" }).or({ foo: "string" }) // Type<{ foo: number } | { foo: string }>
|
|
98
|
+
* @example const union = type("string.numeric.parse").or("number") // Type<((In: string) => Out<number>) | number>
|
|
99
|
+
*/
|
|
100
|
+
or<const def, r = type.infer<def, $>>(def: type.validate<def, $>): instantiateType<t | r, $>;
|
|
101
|
+
/**
|
|
102
|
+
* Add a custom predicate to this `Type`.
|
|
103
|
+
* @example const nan = type('number').narrow(n => Number.isNaN(n)) // Type<number>
|
|
104
|
+
* @example const foo = type("string").narrow((s): s is `foo${string}` => s.startsWith('foo') || ctx.mustBe('string starting with "foo"')) // Type<"foo${string}">
|
|
105
|
+
* @example const unique = type('string[]').narrow((a, ctx) => new Set(a).size === a.length || ctx.mustBe('array with unique elements'))
|
|
106
|
+
*/
|
|
107
|
+
narrow<narrowed extends this["infer"] = never, r = [narrowed] extends [never] ? t : t extends InferredMorph<infer i, infer o> ? o extends To ? (In: i) => To<narrowed> : (In: i) => Out<narrowed> : narrowed>(predicate: Predicate<this["infer"]> | PredicateCast<this["infer"], narrowed>): instantiateType<r, $>;
|
|
108
|
+
satisfying<narrowed extends this["inferIn"] = never, r = [narrowed] extends [never] ? t : t extends InferredMorph<any, infer o> ? (In: narrowed) => o : narrowed>(predicate: Predicate<this["inferIn"]> | PredicateCast<this["inferIn"], narrowed>): instantiateType<r, $>;
|
|
109
|
+
/**
|
|
110
|
+
* Create a `Type` for array with elements of this `Type`
|
|
111
|
+
* @example const T = type(/^foo/); const array = T.array() // Type<string[]>
|
|
112
|
+
*/
|
|
113
|
+
array(): ArrayType<t[], $>;
|
|
114
|
+
/**
|
|
115
|
+
* Morph this `Type` through a chain of morphs.
|
|
116
|
+
* @example const dedupe = type('string[]').pipe(a => Array.from(new Set(a)))
|
|
117
|
+
* @example type({codes: 'string.numeric[]'}).pipe(obj => obj.codes).to('string.numeric.parse[]')
|
|
118
|
+
*/
|
|
119
|
+
pipe: ChainedPipes<t, $>;
|
|
120
|
+
equals<const def>(def: type.validate<def, $>): boolean;
|
|
121
|
+
ifEquals<const def, r = type.infer<def, $>>(def: type.validate<def, $>): instantiateType<r, $> | undefined;
|
|
122
|
+
extends<const def>(other: type.validate<def, $>): boolean;
|
|
123
|
+
ifExtends<const def, r = type.infer<def, $>>(other: type.validate<def, $>): instantiateType<r, $> | undefined;
|
|
124
|
+
overlaps<const def>(r: type.validate<def, $>): boolean;
|
|
125
|
+
extract<const def, r = type.infer<def, $>>(r: type.validate<def, $>): instantiateType<Extract<t, r>, $>;
|
|
126
|
+
exclude<const def, r = type.infer<def, $>>(r: type.validate<def, $>): instantiateType<Exclude<t, r>, $>;
|
|
127
|
+
distribute<mapOut, reduceOut = mapOut[]>(mapBranch: (branch: Type, i: number, branches: array<Type>) => mapOut, reduceMapped?: (mappedBranches: mapOut[]) => reduceOut): reduceOut;
|
|
128
|
+
optional(): [this, "?"];
|
|
129
|
+
/**
|
|
130
|
+
* Add a default value for this `Type` when it is used as a property.\
|
|
131
|
+
* Default value should be a valid input value for this `Type, or a function that returns a valid input value.\
|
|
132
|
+
* If the type has a morph, it will be applied to the default value.
|
|
133
|
+
* @example const withDefault = type({ foo: type("string").default("bar") }); withDefault({}) // { foo: "bar" }
|
|
134
|
+
* @example const withFactory = type({ foo: type("number[]").default(() => [1])) }); withFactory({baz: 'a'}) // { foo: [1], baz: 'a' }
|
|
135
|
+
* @example const withMorph = type({ foo: type("string.numeric.parse").default("123") }); withMorph({}) // { foo: 123 }
|
|
136
|
+
*/
|
|
137
|
+
default<const value extends defaultFor<this["inferIn"]>>(value: value): [this, "=", value];
|
|
138
|
+
"~standard": StandardSchemaV1.ArkTypeProps<this["inferIn"], this["inferOut"]>;
|
|
139
|
+
/** @deprecated */
|
|
140
|
+
apply: Function["apply"];
|
|
141
|
+
/** @deprecated */
|
|
142
|
+
bind: Function["bind"];
|
|
143
|
+
/** @deprecated */
|
|
144
|
+
call: Function["call"];
|
|
145
|
+
/** @deprecated */
|
|
146
|
+
caller: Function;
|
|
147
|
+
/** @deprecated */
|
|
148
|
+
length: number;
|
|
149
|
+
/** @deprecated */
|
|
150
|
+
name: string;
|
|
151
|
+
/** @deprecated */
|
|
152
|
+
prototype: Function["prototype"];
|
|
153
|
+
/** @deprecated */
|
|
154
|
+
arguments: Function["arguments"];
|
|
155
|
+
/** @deprecated */
|
|
156
|
+
Symbol: never;
|
|
157
|
+
}
|
|
158
|
+
interface ChainedPipeSignature<t, $> {
|
|
159
|
+
<a extends Morph<distill.Out<t>>, r = instantiateType<inferPipes<t, [a]>, $>>(a: a): NoInfer<r> extends infer result ? result : never;
|
|
160
|
+
<a extends Morph<distill.Out<t>>, b extends Morph<inferMorphOut<a>>, r = instantiateType<inferPipes<t, [a, b]>, $>>(a: a, b: b): NoInfer<r> extends infer result ? result : never;
|
|
161
|
+
<a extends Morph<distill.Out<t>>, b extends Morph<inferMorphOut<a>>, c extends Morph<inferMorphOut<b>>, r = instantiateType<inferPipes<t, [a, b, c]>, $>>(a: a, b: b, c: c): NoInfer<r> extends infer result ? result : never;
|
|
162
|
+
<a extends Morph<distill.Out<t>>, b extends Morph<inferMorphOut<a>>, c extends Morph<inferMorphOut<b>>, d extends Morph<inferMorphOut<c>>, r = instantiateType<inferPipes<t, [a, b, c, d]>, $>>(a: a, b: b, c: c, d: d): NoInfer<r> extends infer result ? result : never;
|
|
163
|
+
<a extends Morph<distill.Out<t>>, b extends Morph<inferMorphOut<a>>, c extends Morph<inferMorphOut<b>>, d extends Morph<inferMorphOut<c>>, e extends Morph<inferMorphOut<d>>, r = instantiateType<inferPipes<t, [a, b, c, d, e]>, $>>(a: a, b: b, c: c, d: d, e: e): NoInfer<r> extends infer result ? result : never;
|
|
164
|
+
<a extends Morph<distill.Out<t>>, b extends Morph<inferMorphOut<a>>, c extends Morph<inferMorphOut<b>>, d extends Morph<inferMorphOut<c>>, e extends Morph<inferMorphOut<d>>, f extends Morph<inferMorphOut<e>>, r = instantiateType<inferPipes<t, [a, b, c, d, e, f]>, $>>(a: a, b: b, c: c, d: d, e: e, f: f): NoInfer<r> extends infer result ? result : never;
|
|
165
|
+
<a extends Morph<distill.Out<t>>, b extends Morph<inferMorphOut<a>>, c extends Morph<inferMorphOut<b>>, d extends Morph<inferMorphOut<c>>, e extends Morph<inferMorphOut<d>>, f extends Morph<inferMorphOut<e>>, g extends Morph<inferMorphOut<f>>, r = instantiateType<inferPipes<t, [a, b, c, d, e, f, g]>, $>>(a: a, b: b, c: c, d: d, e: e, f: f, g: g): NoInfer<r> extends infer result ? result : never;
|
|
166
|
+
}
|
|
167
|
+
export interface ChainedPipes<t, $> extends ChainedPipeSignature<t, $> {
|
|
168
|
+
try: ChainedPipeSignature<t, $>;
|
|
169
|
+
}
|
|
170
|
+
type validateChainedAsArgs<t> = [
|
|
171
|
+
t
|
|
172
|
+
] extends [unset] ? [
|
|
173
|
+
t
|
|
174
|
+
] extends [anyOrNever] ? [
|
|
175
|
+
] : [
|
|
176
|
+
ErrorMessage<"as requires an explicit type parameter like myType.as<t>()">
|
|
177
|
+
] : [];
|
|
178
|
+
export type { Type as BaseType };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { ExclusiveDateRangeSchema, InclusiveDateRangeSchema } from "@ark/schema";
|
|
2
|
+
import type { ObjectType } from "./object.ts";
|
|
3
|
+
/** @ts-ignore cast variance */
|
|
4
|
+
interface Type<out t extends globalThis.Date = globalThis.Date, $ = {}> extends ObjectType<t, $> {
|
|
5
|
+
atOrAfter(schema: InclusiveDateRangeSchema): this;
|
|
6
|
+
atOrBefore(schema: InclusiveDateRangeSchema): this;
|
|
7
|
+
laterThan(schema: ExclusiveDateRangeSchema): this;
|
|
8
|
+
earlierThan(schema: ExclusiveDateRangeSchema): this;
|
|
9
|
+
}
|
|
10
|
+
export type { Type as DateType };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { anyOrNever, array } from "@ark/util";
|
|
2
|
+
import type { InferredMorph } from "../attributes.ts";
|
|
3
|
+
import type { ArrayType } from "./array.ts";
|
|
4
|
+
import type { BaseType } from "./base.ts";
|
|
5
|
+
import type { DateType } from "./date.ts";
|
|
6
|
+
import type { MorphType } from "./morph.ts";
|
|
7
|
+
import type { NumberType } from "./number.ts";
|
|
8
|
+
import type { ObjectType } from "./object.ts";
|
|
9
|
+
import type { StringType } from "./string.ts";
|
|
10
|
+
export type instantiateType<t, $> = NoInfer<_instantiateType<t, $>> extends infer r ? r : never;
|
|
11
|
+
export type _instantiateType<t, $> = [
|
|
12
|
+
Extract<t, InferredMorph>
|
|
13
|
+
] extends [anyOrNever] ? [
|
|
14
|
+
t
|
|
15
|
+
] extends [anyOrNever] ? BaseType<t, $> : [t] extends [string] ? StringType<t, $> : [t] extends [number] ? NumberType<t, $> : [t] extends [object] ? [
|
|
16
|
+
t
|
|
17
|
+
] extends [array] ? ArrayType<t, $> : [t] extends [Date] ? DateType<t, $> : ObjectType<t, $> : BaseType<t, $> : MorphType<t, $>;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { inferPipe } from "../attributes.ts";
|
|
2
|
+
import type { type } from "../keywords/keywords.ts";
|
|
3
|
+
import type { BaseType } from "./base.ts";
|
|
4
|
+
/** @ts-ignore cast variance */
|
|
5
|
+
interface Type<out t = unknown, $ = {}> extends BaseType<t, $> {
|
|
6
|
+
/**
|
|
7
|
+
* Append extra validation shape on the pipe output
|
|
8
|
+
* @example type({codes: 'string.numeric[]'}).pipe(obj => obj.codes).to('string.numeric.parse[]')
|
|
9
|
+
*/
|
|
10
|
+
to<const def, r = type.infer<def, $>>(def: type.validate<def, $>): Type<inferPipe<t, r>, $>;
|
|
11
|
+
}
|
|
12
|
+
export type { Type as MorphType };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Divisor, ExclusiveNumericRangeSchema, InclusiveNumericRangeSchema } from "@ark/schema";
|
|
2
|
+
import type { BaseType } from "./base.ts";
|
|
3
|
+
/** @ts-ignore cast variance */
|
|
4
|
+
interface Type<out t extends number = number, $ = {}> extends BaseType<t, $> {
|
|
5
|
+
divisibleBy(schema: Divisor.Schema): this;
|
|
6
|
+
atLeast(schema: InclusiveNumericRangeSchema): this;
|
|
7
|
+
atMost(schema: InclusiveNumericRangeSchema): this;
|
|
8
|
+
moreThan(schema: ExclusiveNumericRangeSchema): this;
|
|
9
|
+
lessThan(schema: ExclusiveNumericRangeSchema): this;
|
|
10
|
+
}
|
|
11
|
+
export type { Type as NumberType };
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import type { BaseMappedPropInner, OptionalMappedPropInner, Prop } from "@ark/schema";
|
|
2
|
+
import type { arkGet, arkIndexableOf, arkKeyOf, array, ErrorType, inferred, intersectUnion, JsonStructure, Key, listable, merge, optionalKeyOf, show, toArkKey } from "@ark/util";
|
|
3
|
+
import type { Default, withDefault } from "../attributes.ts";
|
|
4
|
+
import type { type } from "../keywords/keywords.ts";
|
|
5
|
+
import type { ArrayType } from "./array.ts";
|
|
6
|
+
import type { BaseType } from "./base.ts";
|
|
7
|
+
import type { instantiateType } from "./instantiate.ts";
|
|
8
|
+
/** @ts-ignore cast variance */
|
|
9
|
+
interface Type<out t extends object = object, $ = {}> extends BaseType<t, $> {
|
|
10
|
+
readonly(): t extends array ? ArrayType<{
|
|
11
|
+
readonly [i in keyof t]: t[i];
|
|
12
|
+
}, $> : Type<{
|
|
13
|
+
readonly [k in keyof t]: t[k];
|
|
14
|
+
}, $>;
|
|
15
|
+
keyof(): instantiateType<arkKeyOf<t>, $>;
|
|
16
|
+
/**
|
|
17
|
+
* Get the `Type` of a property of this `Type<object>`.
|
|
18
|
+
* @example type({ foo: "string" }).get("foo") // Type<string>
|
|
19
|
+
*/
|
|
20
|
+
get<const k1 extends arkIndexableOf<t>, r = arkGet<t, k1>>(k1: k1 | type.cast<k1>): instantiateType<r, $>;
|
|
21
|
+
get<const k1 extends arkIndexableOf<t>, const k2 extends arkIndexableOf<arkGet<t, k1>>>(k1: k1 | type.cast<k1>, k2: k2 | type.cast<k2>): instantiateType<arkGet<arkGet<t, k1>, k2>, $> extends infer r ? r : never;
|
|
22
|
+
get<const k1 extends arkIndexableOf<t>, const k2 extends arkIndexableOf<arkGet<t, k1>>, const k3 extends arkIndexableOf<arkGet<arkGet<t, k1>, k2>>, r = arkGet<arkGet<arkGet<t, k1>, k2>, k3>>(k1: k1 | type.cast<k1>, k2: k2 | type.cast<k2>, k3: k3 | type.cast<k3>): instantiateType<r, $>;
|
|
23
|
+
/**
|
|
24
|
+
* Create a copy of this `Type` with only the specified properties.
|
|
25
|
+
* @example type({ foo: "string", bar: "number" }).pick("foo") // Type<{ foo: string }>
|
|
26
|
+
*/
|
|
27
|
+
pick<const key extends arkKeyOf<t> = never>(...keys: (key | type.cast<key>)[]): Type<{
|
|
28
|
+
[k in keyof t as Extract<toArkKey<t, k>, key>]: t[k];
|
|
29
|
+
}, $>;
|
|
30
|
+
/**
|
|
31
|
+
* Create a copy of this `Type` with all properties except the specified ones.
|
|
32
|
+
* @example type({ foo: "string", bar: "number" }).omit("foo") // Type<{ bar: number }>
|
|
33
|
+
*/
|
|
34
|
+
omit<const key extends arkKeyOf<t> = never>(...keys: (key | type.cast<key>)[]): Type<{
|
|
35
|
+
[k in keyof t as Exclude<toArkKey<t, k>, key>]: t[k];
|
|
36
|
+
}, $>;
|
|
37
|
+
/**
|
|
38
|
+
* Merge another `Type` definition, overriding properties of this `Type` with the duplicate keys.
|
|
39
|
+
* @example type({ a: "1", b: "2" }).merge({ b: "3", c: "4" }) // Type<{ a: 1, b: 3, c: 4 }>
|
|
40
|
+
*/
|
|
41
|
+
merge<const def, r = type.infer<def, $>>(def: type.validate<def, $> & (r extends object ? unknown : ErrorType<"Merged type must be an object", [actual: r]>)): Type<merge<t, r & object>, $>;
|
|
42
|
+
/**
|
|
43
|
+
* Create a copy of this `Type` with all properties required.
|
|
44
|
+
* @example const T = type({ "foo?"": "string" }).required() // Type<{ foo: string }>
|
|
45
|
+
*/
|
|
46
|
+
required(): Type<{
|
|
47
|
+
[k in keyof t]-?: t[k];
|
|
48
|
+
}, $>;
|
|
49
|
+
/**
|
|
50
|
+
* Create a copy of this `Type` with all properties optional.
|
|
51
|
+
* @example: const T = type({ foo: "string" }).optional() // Type<{ foo?: string }>
|
|
52
|
+
*/
|
|
53
|
+
partial(): Type<{
|
|
54
|
+
[k in keyof t]?: t[k];
|
|
55
|
+
}, $>;
|
|
56
|
+
map<transformed extends listable<MappedTypeProp>>(flatMapEntry: (entry: typePropOf<t, $>) => transformed): Type<constructMapped<t, transformed>, $>;
|
|
57
|
+
/**
|
|
58
|
+
* List of property info of this `Type<object>`.
|
|
59
|
+
* @example type({ foo: "string = "" }).props // [{ kind: "required", key: "foo", value: Type<string>, default: "" }]
|
|
60
|
+
*/
|
|
61
|
+
props: array<typePropOf<t, $>>;
|
|
62
|
+
}
|
|
63
|
+
type typePropOf<o, $> = keyof o extends infer k ? k extends keyof o ? typeProp<o, k, $> : never : never;
|
|
64
|
+
type typeProp<o, k extends keyof o, $, t = o[k] & ({} | null)> = t extends Default<infer t, infer defaultValue> ? DefaultedTypeProp<k & Key, t, defaultValue, $> : BaseTypeProp<k extends optionalKeyOf<o> ? "optional" : "required", k & Key, t, $>;
|
|
65
|
+
export interface BaseTypeProp<kind extends Prop.Kind = Prop.Kind, k extends Key = Key,
|
|
66
|
+
/** @ts-ignore cast variance */
|
|
67
|
+
out v = unknown, $ = {}> {
|
|
68
|
+
kind: kind;
|
|
69
|
+
key: k;
|
|
70
|
+
value: instantiateType<v, $>;
|
|
71
|
+
meta: ArkEnv.meta;
|
|
72
|
+
toJSON: () => JsonStructure;
|
|
73
|
+
}
|
|
74
|
+
export interface DefaultedTypeProp<k extends Key = Key, v = unknown, defaultValue = v, $ = {}> extends BaseTypeProp<"optional", k, v, $> {
|
|
75
|
+
default: defaultValue;
|
|
76
|
+
}
|
|
77
|
+
type MappedTypeProp<k extends Key = Key, v = unknown> = BaseMappedTypeProp<k, v> | OptionalMappedTypeProp<k, v>;
|
|
78
|
+
type BaseMappedTypeProp<k extends Key, v> = merge<BaseMappedPropInner, {
|
|
79
|
+
key: k;
|
|
80
|
+
value: type.cast<v>;
|
|
81
|
+
}>;
|
|
82
|
+
type OptionalMappedTypeProp<k extends Key, v> = merge<OptionalMappedPropInner, {
|
|
83
|
+
key: k;
|
|
84
|
+
value: type.cast<v>;
|
|
85
|
+
default?: v;
|
|
86
|
+
}>;
|
|
87
|
+
type constructMapped<t, transformed extends listable<MappedTypeProp>> = show<intersectUnion<fromTypeProps<t, transformed extends array ? transformed : [transformed]>>>;
|
|
88
|
+
type fromTypeProps<t, props extends array<MappedTypeProp>> = show<{
|
|
89
|
+
[prop in props[number] as Extract<applyHomomorphicOptionality<t, prop>, {
|
|
90
|
+
kind: "required";
|
|
91
|
+
}>["key"]]: prop["value"][inferred];
|
|
92
|
+
} & {
|
|
93
|
+
[prop in props[number] as Extract<applyHomomorphicOptionality<t, prop>, {
|
|
94
|
+
kind: "optional";
|
|
95
|
+
default?: never;
|
|
96
|
+
}>["key"]]?: prop["value"][inferred];
|
|
97
|
+
} & {
|
|
98
|
+
[prop in props[number] as Extract<applyHomomorphicOptionality<t, prop>, {
|
|
99
|
+
kind: "optional";
|
|
100
|
+
default: unknown;
|
|
101
|
+
}>["key"]]: withDefault<prop["value"][inferred], prop["default" & keyof prop]>;
|
|
102
|
+
}>;
|
|
103
|
+
type applyHomomorphicOptionality<t, prop extends MappedTypeProp> = prop["kind"] extends string ? prop : prop & {
|
|
104
|
+
kind: prop["key"] extends optionalKeyOf<t> ? "optional" : "required";
|
|
105
|
+
};
|
|
106
|
+
export type { Type as ObjectType };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { ExactLength, ExclusiveNumericRangeSchema, InclusiveNumericRangeSchema, Pattern } from "@ark/schema";
|
|
2
|
+
import type { BaseType } from "./base.ts";
|
|
3
|
+
/** @ts-ignore cast variance */
|
|
4
|
+
interface Type<out t extends string = string, $ = {}> extends BaseType<t, $> {
|
|
5
|
+
matching(schema: Pattern.Schema): this;
|
|
6
|
+
atLeastLength(schema: InclusiveNumericRangeSchema): this;
|
|
7
|
+
atMostLength(schema: InclusiveNumericRangeSchema): this;
|
|
8
|
+
moreThanLength(schema: ExclusiveNumericRangeSchema): this;
|
|
9
|
+
lessThanLength(schema: ExclusiveNumericRangeSchema): this;
|
|
10
|
+
exactlyLength(schema: ExactLength.Schema): this;
|
|
11
|
+
}
|
|
12
|
+
export type { Type as StringType };
|
package/out/module.d.ts
CHANGED
|
@@ -12,8 +12,8 @@ export interface BoundModule<exports extends {}, $> extends RootModule<bindExpor
|
|
|
12
12
|
export type bindExportsToScope<exports, $> = {
|
|
13
13
|
[k in keyof exports]: instantiateExport<exports[k], $>;
|
|
14
14
|
} & unknown;
|
|
15
|
-
export type Submodule<exports extends {}> = RootModule<exports & ("
|
|
16
|
-
[inferred]: exports["
|
|
15
|
+
export type Submodule<exports extends {}> = RootModule<exports & ("root" extends keyof exports ? {
|
|
16
|
+
[inferred]: exports["root"];
|
|
17
17
|
} : {})>;
|
|
18
18
|
export type instantiateExport<t, $> = [
|
|
19
19
|
t
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import type { writeUnboundableMessage } from "@ark/schema";
|
|
2
2
|
import type { ErrorMessage, array, typeToString } from "@ark/util";
|
|
3
|
-
import type { LimitLiteral } from "../../
|
|
4
|
-
import type { Comparator } from "../
|
|
5
|
-
import type { BoundExpressionKind, writeInvalidLimitMessage } from "../
|
|
3
|
+
import type { LimitLiteral } from "../../attributes.ts";
|
|
4
|
+
import type { Comparator } from "../reduce/shared.ts";
|
|
5
|
+
import type { BoundExpressionKind, writeInvalidLimitMessage } from "../shift/operator/bounds.ts";
|
|
6
6
|
import type { inferAstIn } from "./infer.ts";
|
|
7
7
|
import type { astToString } from "./utils.ts";
|
|
8
8
|
import type { validateAst } from "./validate.ts";
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { writeUnassignableDefaultValueMessage } from "@ark/schema";
|
|
2
|
+
import type { ErrorMessage } from "@ark/util";
|
|
3
|
+
import type { type } from "../../keywords/keywords.ts";
|
|
4
|
+
import type { UnitLiteral } from "../shift/operator/default.ts";
|
|
5
|
+
import type { inferAstIn } from "./infer.ts";
|
|
6
|
+
import type { astToString } from "./utils.ts";
|
|
7
|
+
import type { validateAst } from "./validate.ts";
|
|
8
|
+
export type validateDefault<baseAst, unitLiteral extends UnitLiteral, $, args> = validateAst<baseAst, $, args> extends infer e extends ErrorMessage ? e : type.infer<unitLiteral> extends inferAstIn<baseAst, $, args> ? undefined : ErrorMessage<writeUnassignableDefaultValueMessage<astToString<baseAst>, unitLiteral>>;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { GenericAst, GenericParamAst, writeUnsatisfiedParameterConstraintMessage } from "@ark/schema";
|
|
2
|
+
import type { array, ErrorMessage, Hkt, typeToString } from "@ark/util";
|
|
3
|
+
import type { UnparsedScope } from "../../scope.ts";
|
|
4
|
+
import type { inferDefinition } from "../definition.ts";
|
|
5
|
+
import type { inferAstRoot, inferExpression } from "./infer.ts";
|
|
6
|
+
import type { astToString } from "./utils.ts";
|
|
7
|
+
import type { validateAst } from "./validate.ts";
|
|
8
|
+
export type GenericInstantiationAst<generic extends GenericAst = GenericAst, argAsts extends unknown[] = unknown[]> = [generic, "<>", argAsts];
|
|
9
|
+
export type inferGenericInstantiation<g extends GenericAst, argAsts extends unknown[], $, args> = g["bodyDef"] extends Hkt ? Hkt.apply<g["bodyDef"], {
|
|
10
|
+
[i in keyof argAsts]: inferExpression<argAsts[i], $, args>;
|
|
11
|
+
}> : inferDefinition<g["bodyDef"], resolveScope<g["$"], $>, {
|
|
12
|
+
[i in keyof g["names"] & `${number}` as g["names"][i]]: inferExpression<argAsts[i & keyof argAsts], resolveScope<g["arg$"], $>, args>;
|
|
13
|
+
}>;
|
|
14
|
+
export type validateGenericInstantiation<g extends GenericAst, argAsts extends unknown[], $, args> = validateGenericArgs<g["paramsAst"], argAsts, $, args, []>;
|
|
15
|
+
type validateGenericArgs<params extends array<GenericParamAst>, argAsts extends array, $, args, indices extends 1[]> = argAsts extends readonly [infer arg, ...infer argsTail] ? validateAst<arg, $, args> extends infer e extends ErrorMessage ? e : inferAstRoot<arg, $, args> extends params[indices["length"]][1] ? validateGenericArgs<params, argsTail, $, args, [...indices, 1]> : ErrorMessage<writeUnsatisfiedParameterConstraintMessage<params[indices["length"]][0], typeToString<params[indices["length"]][1]>, astToString<arg>>> : undefined;
|
|
16
|
+
type resolveScope<g$, $> = g$ extends UnparsedScope ? $ : g$;
|
|
17
|
+
export {};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { arkKeyOf, array } from "@ark/util";
|
|
2
|
+
import type { distill, inferIntersection, LimitLiteral, withDefault } from "../../attributes.ts";
|
|
3
|
+
import type { type } from "../../keywords/keywords.ts";
|
|
4
|
+
import type { inferDefinition } from "../definition.ts";
|
|
5
|
+
import type { Comparator } from "../reduce/shared.ts";
|
|
6
|
+
import type { ArkTypeScanner } from "../shift/scanner.ts";
|
|
7
|
+
import type { GenericInstantiationAst, inferGenericInstantiation } from "./generic.ts";
|
|
8
|
+
export type inferAstRoot<ast, $, args> = ast extends array ? inferExpression<ast, $, args> : never;
|
|
9
|
+
export type inferAstIn<ast, $, args> = distill.In<inferAstRoot<ast, $, args>>;
|
|
10
|
+
export type inferAstOut<ast, $, args> = distill.Out<inferAstRoot<ast, $, args>>;
|
|
11
|
+
export type DefAst<def = unknown, alias extends string = string> = [
|
|
12
|
+
def,
|
|
13
|
+
"def",
|
|
14
|
+
alias
|
|
15
|
+
];
|
|
16
|
+
export type InferredAst<t = unknown, def extends string = string> = [
|
|
17
|
+
t,
|
|
18
|
+
"inferred",
|
|
19
|
+
def
|
|
20
|
+
];
|
|
21
|
+
export type inferExpression<ast, $, args> = ast extends array ? ast extends InferredAst<infer resolution> ? resolution : ast extends DefAst<infer def> ? inferDefinition<def, $, args> : ast extends GenericInstantiationAst<infer g, infer argAsts> ? inferGenericInstantiation<g, argAsts, $, args> : ast[1] extends "[]" ? inferExpression<ast[0], $, args>[] : ast[1] extends "|" ? inferExpression<ast[0], $, args> | inferExpression<ast[2], $, args> : ast[1] extends "&" ? inferIntersection<inferExpression<ast[0], $, args>, inferExpression<ast[2], $, args>> : ast[1] extends "=" ? type.infer<ast[2]> extends infer defaultValue ? withDefault<inferExpression<ast[0], $, args>, defaultValue> : never : ast[1] extends "#" ? type.brand<inferExpression<ast[0], $, args>, ast[2]> : ast[1] extends Comparator ? ast[0] extends LimitLiteral ? inferExpression<ast[2], $, args> : inferExpression<ast[0], $, args> : ast[1] extends "%" ? inferExpression<ast[0], $, args> : ast[1] extends "?" ? inferExpression<ast[0], $, args> : ast[0] extends "keyof" ? arkKeyOf<inferExpression<ast[1], $, args>> : never : never;
|
|
22
|
+
export type PrefixOperator = "keyof" | "instanceof" | "===" | "node";
|
|
23
|
+
export type PrefixExpression<operator extends PrefixOperator = PrefixOperator, operand = unknown> = [operator, operand];
|
|
24
|
+
export type PostfixExpression<operator extends ArkTypeScanner.PostfixToken = ArkTypeScanner.PostfixToken, operand = unknown> = readonly [operand, operator];
|
|
25
|
+
export type InfixExpression<operator extends ArkTypeScanner.InfixToken = ArkTypeScanner.InfixToken, l = unknown, r = unknown> = [l, operator, r];
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import type { Stringifiable } from "@ark/util";
|
|
2
|
-
import type { Comparator } from "../
|
|
2
|
+
import type { Comparator } from "../reduce/shared.ts";
|
|
3
3
|
import type { DefAst, InferredAst, InfixExpression, PostfixExpression } from "./infer.ts";
|
|
4
4
|
export type astToString<ast> = ast extends InferredAst | DefAst ? ast[2] : ast extends PostfixExpression<infer operator, infer operand> ? operator extends "[]" ? `${astToString<operand>}[]` : never : ast extends InfixExpression<infer operator, infer l, infer r> ? operator extends "&" | "|" | "%" | Comparator ? `${astToString<l>} ${operator} ${astToString<r>}` : never : ast extends Stringifiable ? `${ast extends bigint ? `${ast}n` : ast}` : "...";
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { arkKind, PrivateDeclaration, writeMissingSubmoduleAccessMessage } from "@ark/schema";
|
|
2
|
+
import type { anyOrNever, BigintLiteral, Completion, ErrorMessage, NumberLiteral, writeMalformedNumericLiteralMessage } from "@ark/util";
|
|
3
|
+
import type { Generic } from "../../generic.ts";
|
|
4
|
+
import type { Comparator } from "../reduce/shared.ts";
|
|
5
|
+
import type { writeInvalidGenericArgCountMessage } from "../shift/operand/genericArgs.ts";
|
|
6
|
+
import type { UnitLiteral } from "../shift/operator/default.ts";
|
|
7
|
+
import type { parseString } from "../string.ts";
|
|
8
|
+
import type { validateRange } from "./bounds.ts";
|
|
9
|
+
import type { validateDefault } from "./default.ts";
|
|
10
|
+
import type { validateDivisor } from "./divisor.ts";
|
|
11
|
+
import type { GenericInstantiationAst, validateGenericInstantiation } from "./generic.ts";
|
|
12
|
+
import type { DefAst, InferredAst, InfixExpression, PostfixExpression } from "./infer.ts";
|
|
13
|
+
import type { validateKeyof } from "./keyof.ts";
|
|
14
|
+
import type { astToString } from "./utils.ts";
|
|
15
|
+
export type validateAst<ast, $, args> = ast extends ErrorMessage ? ast : ast extends InferredAst ? validateInferredAst<ast[0], ast[2]> : ast extends DefAst ? ast[2] extends PrivateDeclaration<infer name> ? ErrorMessage<writePrefixedPrivateReferenceMessage<name>> : undefined : ast extends PostfixExpression<"[]" | "?", infer operand> ? validateAst<operand, $, args> : ast extends InfixExpression<infer operator, infer l, infer r> ? operator extends "&" | "|" ? validateInfix<ast, $, args> : operator extends Comparator ? validateRange<l, operator, r, $, args> : operator extends "%" ? validateDivisor<l, $, args> : operator extends "=" ? validateDefault<l, r & UnitLiteral, $, args> : operator extends "#" ? validateAst<l, $, args> : ErrorMessage<writeUnexpectedExpressionMessage<astToString<ast>>> : ast extends ["keyof", infer operand] ? validateKeyof<operand, $, args> : ast extends GenericInstantiationAst<infer g, infer argAsts> ? validateGenericInstantiation<g, argAsts, $, args> : ErrorMessage<writeUnexpectedExpressionMessage<astToString<ast>>> & {
|
|
16
|
+
ast: ast;
|
|
17
|
+
};
|
|
18
|
+
type writeUnexpectedExpressionMessage<expression extends string> = `Unexpectedly failed to parse the expression resulting from ${expression}`;
|
|
19
|
+
export declare const writePrefixedPrivateReferenceMessage: <name extends string>(name: name) => writePrefixedPrivateReferenceMessage<name>;
|
|
20
|
+
export type writePrefixedPrivateReferenceMessage<name extends string> = `Private type references should not include '#'. Use '${name}' instead.`;
|
|
21
|
+
type validateInferredAst<inferred, def extends string> = def extends NumberLiteral ? number extends inferred ? ErrorMessage<writeMalformedNumericLiteralMessage<def, "number">> : undefined : def extends BigintLiteral ? bigint extends inferred ? ErrorMessage<writeMalformedNumericLiteralMessage<def, "bigint">> : undefined : [inferred] extends [anyOrNever] ? undefined : def extends PrivateDeclaration<infer name> ? ErrorMessage<writePrefixedPrivateReferenceMessage<name>> : inferred extends Generic ? ErrorMessage<writeInvalidGenericArgCountMessage<def, inferred["names"], []>> : inferred extends {
|
|
22
|
+
[arkKind]: "module";
|
|
23
|
+
} ? "root" extends keyof inferred ? undefined : ErrorMessage<writeMissingSubmoduleAccessMessage<def>> : def extends ErrorMessage ? def : undefined;
|
|
24
|
+
export type validateString<def extends string, $, args> = parseString<def, $, args> extends infer ast ? validateAst<ast, $, args> extends infer result extends ErrorMessage ? result extends Completion<infer text> ? text : result : def : never;
|
|
25
|
+
type validateInfix<ast extends InfixExpression, $, args> = validateAst<ast[0], $, args> extends infer e extends ErrorMessage ? e : validateAst<ast[2], $, args> extends infer e extends ErrorMessage ? e : undefined;
|
|
26
|
+
export declare const shallowOptionalMessage = "Optional definitions like 'string?' are only valid as properties in an object or tuple";
|
|
27
|
+
export type shallowOptionalMessage = typeof shallowOptionalMessage;
|
|
28
|
+
export declare const shallowDefaultableMessage = "Defaultable definitions like 'number = 0' are only valid as properties in an object or tuple";
|
|
29
|
+
export type shallowDefaultableMessage = typeof shallowDefaultableMessage;
|
|
30
|
+
export {};
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export const writePrefixedPrivateReferenceMessage = (name) => `Private type references should not include '#'. Use '${name}' instead.`;
|
|
2
|
+
export const shallowOptionalMessage = "Optional definitions like 'string?' are only valid as properties in an object or tuple";
|
|
3
|
+
export const shallowDefaultableMessage = "Defaultable definitions like 'number = 0' are only valid as properties in an object or tuple";
|
|
@@ -1,18 +1,26 @@
|
|
|
1
|
-
import { type BaseRoot } from "@ark/schema";
|
|
2
|
-
import { type
|
|
3
|
-
import type { type } from "../keywords/
|
|
4
|
-
import type {
|
|
5
|
-
import type {
|
|
1
|
+
import { type BaseParseContext, type BaseRoot } from "@ark/schema";
|
|
2
|
+
import { type anyOrNever, type array, type defined, type equals, type ErrorMessage, type Fn, type ifEmptyObjectLiteral, type objectKindOrDomainOf, type optionalKeyOf, type Primitive, type requiredKeyOf, type show } from "@ark/util";
|
|
3
|
+
import type { type } from "../keywords/keywords.ts";
|
|
4
|
+
import type { InnerParseResult } from "../scope.ts";
|
|
5
|
+
import type { shallowDefaultableMessage, shallowOptionalMessage, validateString } from "./ast/validate.ts";
|
|
6
6
|
import { type inferObjectLiteral, type validateObjectLiteral } from "./objectLiteral.ts";
|
|
7
|
-
import type {
|
|
8
|
-
import type
|
|
9
|
-
import { type
|
|
10
|
-
|
|
7
|
+
import type { isDefaultable, OptionalPropertyDefinition } from "./property.ts";
|
|
8
|
+
import { type BaseCompletions, type inferString } from "./string.ts";
|
|
9
|
+
import { type inferTupleExpression, type maybeValidateTupleExpression, type TupleExpression } from "./tupleExpressions.ts";
|
|
10
|
+
import { type inferTupleLiteral, type validateTupleLiteral } from "./tupleLiteral.ts";
|
|
11
|
+
export declare const parseInnerDefinition: (def: unknown, ctx: BaseParseContext) => InnerParseResult;
|
|
12
|
+
export declare const parseObject: (def: object, ctx: BaseParseContext) => BaseRoot;
|
|
11
13
|
export type inferDefinition<def, $, args> = [
|
|
12
14
|
def
|
|
13
|
-
] extends [anyOrNever] ? def : def extends type.cast<infer t> ? ifEmptyObjectLiteral<
|
|
14
|
-
export type validateDefinition<def, $, args> = null extends undefined ? ErrorMessage<`'strict' or 'strictNullChecks' must be set to true in your tsconfig's 'compilerOptions'`> : [def] extends [
|
|
15
|
-
export type
|
|
15
|
+
] extends [anyOrNever] ? def : def extends type.cast<infer t> ? ifEmptyObjectLiteral<def, object, t> : def extends ThunkCast<infer t> ? t : def extends string ? inferString<def, $, args> : def extends array ? inferTuple<def, $, args> : def extends RegExp ? string : def extends object ? inferObjectLiteral<def, $, args> : never;
|
|
16
|
+
export type validateDefinition<def, $, args> = null extends undefined ? ErrorMessage<`'strict' or 'strictNullChecks' must be set to true in your tsconfig's 'compilerOptions'`> : [def] extends [anyOrNever] ? def : def extends OptionalPropertyDefinition ? ErrorMessage<shallowOptionalMessage> : isDefaultable<def, $, args> extends true ? ErrorMessage<shallowDefaultableMessage> : validateInnerDefinition<def, $, args>;
|
|
17
|
+
export type validateInnerDefinition<def, $, args> = [
|
|
18
|
+
def
|
|
19
|
+
] extends [Terminal] ? def : def extends string ? validateString<def, $, args> : def extends array ? validateTuple<def, $, args> : def extends BadDefinitionType ? ErrorMessage<writeBadDefinitionTypeMessage<objectKindOrDomainOf<def>>> : unknown extends def ? BaseCompletions<$, args> | {} : RegExp extends def ? def : validateObjectLiteral<def, $, args>;
|
|
20
|
+
export declare const parseTuple: (def: array, ctx: BaseParseContext) => BaseRoot;
|
|
21
|
+
export type validateTuple<def extends array, $, args> = maybeValidateTupleExpression<def, $, args> extends infer result ? result extends null ? validateTupleLiteral<def, $, args> : result : never;
|
|
22
|
+
export type inferTuple<def extends array, $, args> = def extends TupleExpression ? inferTupleExpression<def, $, args> : inferTupleLiteral<def, $, args>;
|
|
23
|
+
export type validateDeclared<declared, def, $, args> = def extends type.validate<def, $, args> ? validateInference<def, declared, $, args> : type.validate<def, $, args>;
|
|
16
24
|
type validateInference<def, declared, $, args> = def extends RegExp | type.cast<unknown> | ThunkCast | TupleExpression ? validateShallowInference<def, declared, $, args> : def extends array ? declared extends array ? {
|
|
17
25
|
[i in keyof declared]: i extends keyof def ? validateInference<def[i], declared[i], $, args> : declared[i];
|
|
18
26
|
} : show<declarationMismatch<def, declared, $, args>> : def extends object ? show<{
|
|
@@ -25,7 +33,7 @@ type declarationMismatch<def, declared, $, args> = {
|
|
|
25
33
|
declared: declared;
|
|
26
34
|
inferred: inferDefinition<def, $, args>;
|
|
27
35
|
};
|
|
28
|
-
type Terminal =
|
|
36
|
+
type Terminal = type.cast<unknown> | Fn;
|
|
29
37
|
export type ThunkCast<t = unknown> = () => type.cast<t>;
|
|
30
38
|
type BadDefinitionType = Exclude<Primitive, string>;
|
|
31
39
|
export declare const writeBadDefinitionTypeMessage: <actual extends string>(actual: actual) => writeBadDefinitionTypeMessage<actual>;
|
package/out/parser/definition.js
CHANGED
|
@@ -1,7 +1,24 @@
|
|
|
1
1
|
import { hasArkKind } from "@ark/schema";
|
|
2
|
-
import { isThunk, objectKindOf, printable, throwParseError } from "@ark/util";
|
|
2
|
+
import { domainOf, hasDomain, isThunk, objectKindOf, printable, throwParseError } from "@ark/util";
|
|
3
3
|
import { parseObjectLiteral } from "./objectLiteral.js";
|
|
4
|
-
import {
|
|
4
|
+
import { parseString } from "./string.js";
|
|
5
|
+
import { maybeParseTupleExpression } from "./tupleExpressions.js";
|
|
6
|
+
import { parseTupleLiteral } from "./tupleLiteral.js";
|
|
7
|
+
const parseCache = {};
|
|
8
|
+
export const parseInnerDefinition = (def, ctx) => {
|
|
9
|
+
if (typeof def === "string") {
|
|
10
|
+
if (ctx.args && Object.keys(ctx.args).some(k => def.includes(k))) {
|
|
11
|
+
// we can only rely on the cache if there are no contextual
|
|
12
|
+
// resolutions like "this" or generic args
|
|
13
|
+
return parseString(def, ctx);
|
|
14
|
+
}
|
|
15
|
+
const scopeCache = (parseCache[ctx.$.id] ??= {});
|
|
16
|
+
return (scopeCache[def] ??= parseString(def, ctx));
|
|
17
|
+
}
|
|
18
|
+
return hasDomain(def, "object") ?
|
|
19
|
+
parseObject(def, ctx)
|
|
20
|
+
: throwParseError(writeBadDefinitionTypeMessage(domainOf(def)));
|
|
21
|
+
};
|
|
5
22
|
export const parseObject = (def, ctx) => {
|
|
6
23
|
const objectKind = objectKindOf(def);
|
|
7
24
|
switch (objectKind) {
|
|
@@ -26,4 +43,5 @@ export const parseObject = (def, ctx) => {
|
|
|
26
43
|
return throwParseError(writeBadDefinitionTypeMessage(objectKind ?? printable(def)));
|
|
27
44
|
}
|
|
28
45
|
};
|
|
46
|
+
export const parseTuple = (def, ctx) => maybeParseTupleExpression(def, ctx) ?? parseTupleLiteral(def, ctx);
|
|
29
47
|
export const writeBadDefinitionTypeMessage = (actual) => `Type definitions must be strings or objects (was ${actual})`;
|