arktype 2.0.0-dev.1 → 2.0.0-dev.10
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/__tests__/array.test.ts +270 -151
- package/__tests__/badDefinitionType.test.ts +10 -3
- package/__tests__/basis.test.ts +6 -2
- package/__tests__/bounds.test.ts +282 -352
- package/__tests__/cast.test.ts +7 -2
- package/__tests__/completions.test.ts +8 -3
- package/__tests__/config.test.ts +95 -167
- package/__tests__/dateLiteral.test.ts +8 -4
- package/__tests__/declared.test.ts +7 -6
- package/__tests__/discrimination.test.ts +1 -1
- package/__tests__/divisor.test.ts +46 -22
- package/__tests__/enclosed.test.ts +39 -25
- package/__tests__/examples.test.ts +56 -56
- package/__tests__/expressions.test.ts +184 -112
- package/__tests__/generic.test.ts +307 -288
- package/__tests__/group.test.ts +67 -54
- package/__tests__/imports.test.ts +62 -49
- package/__tests__/instanceof.test.ts +25 -23
- package/__tests__/intersection.test.ts +52 -48
- package/__tests__/keyTraversal.test.ts +77 -73
- package/__tests__/keyof.test.ts +68 -52
- package/__tests__/keywords.test.ts +164 -138
- package/__tests__/literal.test.ts +60 -21
- package/__tests__/match.bench.ts +31 -0
- package/__tests__/match.test.ts +222 -22
- package/__tests__/narrow.test.ts +52 -54
- package/__tests__/objectLiteral.test.ts +347 -205
- package/__tests__/operator.bench.ts +4 -4
- package/__tests__/pipe.test.ts +501 -0
- package/__tests__/realWorld.test.ts +63 -0
- package/__tests__/regex.test.ts +17 -7
- package/__tests__/scope.bench.ts +1 -1
- package/__tests__/scope.test.ts +257 -23
- package/__tests__/serialization.test.ts +19 -0
- package/__tests__/string.test.ts +11 -7
- package/__tests__/submodule.test.ts +26 -39
- package/__tests__/this.test.ts +83 -57
- package/__tests__/thunk.test.ts +13 -7
- package/__tests__/traverse.test.ts +76 -54
- package/__tests__/tsGenerics.test.ts +12 -12
- package/__tests__/type.test.ts +36 -0
- package/__tests__/typeReference.test.ts +7 -7
- package/__tests__/unenclosed.test.ts +116 -100
- package/__tests__/union.test.ts +171 -104
- package/ark.ts +21 -65
- package/generic.ts +69 -0
- package/main.ts +6 -18
- package/match.ts +195 -93
- package/module.ts +15 -0
- package/out/ark.d.ts +15 -39
- package/out/ark.js +9 -18
- package/out/ark.js.map +1 -1
- package/out/generic.d.ts +21 -0
- package/out/generic.js +24 -0
- package/out/generic.js.map +1 -0
- package/out/main.d.ts +6 -7
- package/out/main.js +4 -3
- package/out/main.js.map +1 -1
- package/out/match.d.ts +57 -34
- package/out/match.js +51 -17
- package/out/match.js.map +1 -1
- package/out/module.d.ts +10 -0
- package/out/module.js +3 -0
- package/out/module.js.map +1 -0
- package/out/parser/definition.d.ts +11 -13
- package/out/parser/definition.js +10 -13
- package/out/parser/definition.js.map +1 -1
- package/out/parser/generic.d.ts +5 -7
- package/out/parser/generic.js +7 -11
- package/out/parser/generic.js.map +1 -1
- package/out/parser/objectLiteral.d.ts +45 -14
- package/out/parser/objectLiteral.js +61 -52
- package/out/parser/objectLiteral.js.map +1 -1
- package/out/parser/semantic/bounds.d.ts +10 -8
- package/out/parser/semantic/bounds.js +0 -1
- package/out/parser/semantic/bounds.js.map +1 -1
- package/out/parser/semantic/divisor.d.ts +5 -7
- package/out/parser/semantic/divisor.js +0 -1
- package/out/parser/semantic/infer.d.ts +24 -0
- package/out/parser/semantic/infer.js +1 -0
- package/out/parser/semantic/infer.js.map +1 -0
- package/out/parser/semantic/utils.d.ts +3 -7
- package/out/parser/semantic/utils.js +0 -1
- package/out/parser/semantic/validate.d.ts +15 -14
- package/out/parser/semantic/validate.js +1 -1
- package/out/parser/semantic/validate.js.map +1 -1
- package/out/parser/string/reduce/dynamic.d.ts +13 -13
- package/out/parser/string/reduce/dynamic.js +31 -26
- package/out/parser/string/reduce/dynamic.js.map +1 -1
- package/out/parser/string/reduce/shared.d.ts +6 -20
- package/out/parser/string/reduce/shared.js +0 -1
- package/out/parser/string/reduce/shared.js.map +1 -1
- package/out/parser/string/reduce/static.d.ts +27 -24
- package/out/parser/string/reduce/static.js +0 -1
- package/out/parser/string/shift/operand/date.d.ts +3 -3
- package/out/parser/string/shift/operand/date.js +4 -7
- package/out/parser/string/shift/operand/date.js.map +1 -1
- package/out/parser/string/shift/operand/enclosed.d.ts +4 -19
- package/out/parser/string/shift/operand/enclosed.js +12 -13
- package/out/parser/string/shift/operand/enclosed.js.map +1 -1
- package/out/parser/string/shift/operand/genericArgs.d.ts +9 -10
- package/out/parser/string/shift/operand/genericArgs.js +6 -10
- package/out/parser/string/shift/operand/genericArgs.js.map +1 -1
- package/out/parser/string/shift/operand/operand.d.ts +3 -4
- package/out/parser/string/shift/operand/operand.js +8 -12
- package/out/parser/string/shift/operand/operand.js.map +1 -1
- package/out/parser/string/shift/operand/unenclosed.d.ts +15 -22
- package/out/parser/string/shift/operand/unenclosed.js +24 -36
- package/out/parser/string/shift/operand/unenclosed.js.map +1 -1
- package/out/parser/string/shift/operator/bounds.d.ts +9 -13
- package/out/parser/string/shift/operator/bounds.js +43 -54
- package/out/parser/string/shift/operator/bounds.js.map +1 -1
- package/out/parser/string/shift/operator/divisor.d.ts +2 -4
- package/out/parser/string/shift/operator/divisor.js +1 -3
- package/out/parser/string/shift/operator/divisor.js.map +1 -1
- package/out/parser/string/shift/operator/operator.d.ts +2 -3
- package/out/parser/string/shift/operator/operator.js +12 -19
- package/out/parser/string/shift/operator/operator.js.map +1 -1
- package/out/parser/string/shift/scanner.d.ts +2 -2
- package/out/parser/string/shift/scanner.js +14 -16
- package/out/parser/string/shift/scanner.js.map +1 -1
- package/out/parser/string/string.d.ts +8 -6
- package/out/parser/string/string.js +1 -3
- package/out/parser/string/string.js.map +1 -1
- package/out/parser/tuple.d.ts +102 -43
- package/out/parser/tuple.js +119 -87
- package/out/parser/tuple.js.map +1 -1
- package/out/scope.d.ts +43 -120
- package/out/scope.js +39 -209
- package/out/scope.js.map +1 -1
- package/out/type.d.ts +43 -64
- package/out/type.js +30 -131
- package/out/type.js.map +1 -1
- package/package.json +7 -3
- package/parser/definition.ts +77 -93
- package/parser/generic.ts +38 -40
- package/parser/objectLiteral.ts +172 -136
- package/parser/semantic/bounds.ts +33 -32
- package/parser/semantic/divisor.ts +8 -10
- package/parser/semantic/infer.ts +141 -0
- package/parser/semantic/utils.ts +12 -23
- package/parser/semantic/validate.ts +81 -73
- package/parser/string/reduce/dynamic.ts +67 -55
- package/parser/string/reduce/shared.ts +1 -3
- package/parser/string/reduce/static.ts +67 -69
- package/parser/string/shift/operand/date.ts +15 -14
- package/parser/string/shift/operand/enclosed.ts +30 -25
- package/parser/string/shift/operand/genericArgs.ts +46 -45
- package/parser/string/shift/operand/operand.ts +32 -38
- package/parser/string/shift/operand/unenclosed.ts +107 -158
- package/parser/string/shift/operator/bounds.ts +118 -112
- package/parser/string/shift/operator/divisor.ts +14 -17
- package/parser/string/shift/operator/operator.ts +35 -46
- package/parser/string/shift/scanner.ts +51 -50
- package/parser/string/string.ts +30 -35
- package/parser/tuple.ts +375 -307
- package/scope.ts +170 -453
- package/type.ts +193 -274
- package/__tests__/astUtils.test.ts +0 -17
- package/__tests__/cyclic.test.ts +0 -144
- package/__tests__/jsdoc.test.ts +0 -64
- package/__tests__/morph.test.ts +0 -409
- package/__tests__/nodeDefinitions.test.ts +0 -190
- package/__tests__/typeMethods.test.ts +0 -69
- package/out/ark.d.ts.map +0 -1
- package/out/arkConfig.d.ts +0 -2
- package/out/arkConfig.d.ts.map +0 -1
- package/out/arkConfig.js +0 -10
- package/out/arkConfig.js.map +0 -1
- package/out/main.d.ts.map +0 -1
- package/out/match.d.ts.map +0 -1
- package/out/parser/definition.d.ts.map +0 -1
- package/out/parser/generic.d.ts.map +0 -1
- package/out/parser/objectLiteral.d.ts.map +0 -1
- package/out/parser/semantic/bounds.d.ts.map +0 -1
- package/out/parser/semantic/divisor.d.ts.map +0 -1
- package/out/parser/semantic/intersections.d.ts +0 -14
- package/out/parser/semantic/intersections.d.ts.map +0 -1
- package/out/parser/semantic/intersections.js +0 -2
- package/out/parser/semantic/intersections.js.map +0 -1
- package/out/parser/semantic/semantic.d.ts +0 -33
- package/out/parser/semantic/semantic.d.ts.map +0 -1
- package/out/parser/semantic/semantic.js +0 -2
- package/out/parser/semantic/semantic.js.map +0 -1
- package/out/parser/semantic/utils.d.ts.map +0 -1
- package/out/parser/semantic/validate.d.ts.map +0 -1
- package/out/parser/shared.d.ts +0 -54
- package/out/parser/shared.d.ts.map +0 -1
- package/out/parser/shared.js +0 -47
- package/out/parser/shared.js.map +0 -1
- package/out/parser/string/reduce/dynamic.d.ts.map +0 -1
- package/out/parser/string/reduce/shared.d.ts.map +0 -1
- package/out/parser/string/reduce/static.d.ts.map +0 -1
- package/out/parser/string/shift/operand/date.d.ts.map +0 -1
- package/out/parser/string/shift/operand/enclosed.d.ts.map +0 -1
- package/out/parser/string/shift/operand/genericArgs.d.ts.map +0 -1
- package/out/parser/string/shift/operand/operand.d.ts.map +0 -1
- package/out/parser/string/shift/operand/unenclosed.d.ts.map +0 -1
- package/out/parser/string/shift/operator/bounds.d.ts.map +0 -1
- package/out/parser/string/shift/operator/divisor.d.ts.map +0 -1
- package/out/parser/string/shift/operator/operator.d.ts.map +0 -1
- package/out/parser/string/shift/scanner.d.ts.map +0 -1
- package/out/parser/string/string.d.ts.map +0 -1
- package/out/parser/tuple.d.ts.map +0 -1
- package/out/scope.d.ts.map +0 -1
- package/out/test.d.ts +0 -2
- package/out/test.d.ts.map +0 -1
- package/out/test.js +0 -7
- package/out/test.js.map +0 -1
- package/out/tsconfig.build.tsbuildinfo +0 -1
- package/out/type.d.ts.map +0 -1
- package/out/util.d.ts +0 -19
- package/out/util.d.ts.map +0 -1
- package/out/util.js +0 -7
- package/out/util.js.map +0 -1
- package/parser/semantic/intersections.ts +0 -46
- package/parser/semantic/semantic.ts +0 -144
- package/parser/shared.ts +0 -152
- package/util.ts +0 -36
package/out/type.d.ts
CHANGED
|
@@ -1,77 +1,56 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { Callable, type Constructor, type
|
|
1
|
+
import { ArkErrors, type BaseMeta, type BaseRoot, type Disjoint, type Morph, type NodeDef, type Out, type Predicate, type PrimitiveConstraintKind, RawSchema, type Schema, type ambient, type constrain, type constraintKindOf, type distillConstrainableOut, type distillIn, type distillOut, type includesMorphs, type inferIntersection, type inferMorphOut, type inferNarrow, type inferPipes, type internalImplementationOf } from "@arktype/schema";
|
|
2
|
+
import { Callable, type Constructor, type array, type conform } from "@arktype/util";
|
|
3
|
+
import { Generic, type validateParameterString } from "./generic.js";
|
|
3
4
|
import type { inferDefinition, validateDeclared, validateDefinition } from "./parser/definition.js";
|
|
4
|
-
import { parseGenericParams
|
|
5
|
-
import type { inferIntersection } from "./parser/semantic/intersections.js";
|
|
5
|
+
import { parseGenericParams } from "./parser/generic.js";
|
|
6
6
|
import type { IndexOneOperator, IndexZeroOperator, TupleInfixOperator } from "./parser/tuple.js";
|
|
7
|
-
import type { Scope, bindThis } from "./scope.js";
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
import type { RawScope, Scope, bindThis } from "./scope.js";
|
|
8
|
+
export interface TypeParserAttachments {
|
|
9
|
+
errors: typeof ArkErrors;
|
|
10
|
+
}
|
|
11
|
+
export interface TypeParser<$> extends TypeParserAttachments {
|
|
10
12
|
<const def>(def: validateTypeRoot<def, $>): Type<inferTypeRoot<def, $>, $>;
|
|
11
|
-
<const zero, const one, const rest extends
|
|
12
|
-
Morph<extractOut<inferTypeRoot<zero, $>>, unknown>
|
|
13
|
-
] : one extends "@" ? [string | BaseMeta] : [validateTypeRoot<rest[0], $>] : []): Type<inferTypeRoot<[zero, one, ...rest], $>, $>;
|
|
13
|
+
<const zero, const one, const rest extends array>(_0: zero extends IndexZeroOperator ? zero : validateTypeRoot<zero, $>, _1: zero extends "keyof" ? validateTypeRoot<one, $> : zero extends "instanceof" ? conform<one, Constructor> : zero extends "===" ? conform<one, unknown> : conform<one, IndexOneOperator>, ..._2: zero extends "===" ? rest : zero extends "instanceof" ? conform<rest, readonly Constructor[]> : one extends TupleInfixOperator ? one extends ":" ? [Predicate<distillIn<inferTypeRoot<zero, $>>>] : one extends "=>" ? [Morph<distillOut<inferTypeRoot<zero, $>>, unknown>] : one extends "@" ? [string | BaseMeta] : [validateTypeRoot<rest[0], $>] : []): Type<inferTypeRoot<[zero, one, ...rest], $>, $>;
|
|
14
14
|
<params extends string, const def>(params: `<${validateParameterString<params>}>`, def: validateDefinition<def, $, {
|
|
15
15
|
[param in parseGenericParams<params>[number]]: unknown;
|
|
16
16
|
}>): Generic<parseGenericParams<params>, def, $>;
|
|
17
|
-
}
|
|
17
|
+
}
|
|
18
|
+
export declare class RawTypeParser extends Callable<(...args: unknown[]) => RawSchema | Generic, TypeParserAttachments> {
|
|
19
|
+
constructor($: RawScope);
|
|
20
|
+
}
|
|
18
21
|
export type DeclarationParser<$> = <preinferred>() => {
|
|
19
|
-
type: <def>(def: validateDeclared<preinferred, def,
|
|
22
|
+
type: <def>(def: validateDeclared<preinferred, def, $ & ambient, bindThis<def>>) => Type<preinferred, $>;
|
|
20
23
|
};
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
};
|
|
27
|
-
export declare class Type<t = unknown, $ = any> extends Callable<(data: unknown) => ArkResult<distill<extractOut<t>>>> {
|
|
28
|
-
definition: unknown;
|
|
29
|
-
scope: Scope;
|
|
30
|
-
[inferred]: t;
|
|
31
|
-
infer: distill<extractOut<t>>;
|
|
32
|
-
config: TypeConfig;
|
|
33
|
-
root: TypeNode<t>;
|
|
34
|
-
allows: this["root"]["allows"];
|
|
35
|
-
expected: string;
|
|
36
|
-
json: Json;
|
|
37
|
-
constructor(definition: unknown, scope: Scope);
|
|
38
|
-
configure(config: TypeConfig): this;
|
|
39
|
-
from(literal: this["in"]["infer"]): this["in"]["infer"];
|
|
24
|
+
declare class _Type<t = unknown, $ = any> extends BaseRoot<t, $> implements internalImplementationOf<Schema> {
|
|
25
|
+
$: Scope<$>;
|
|
26
|
+
get in(): Type<this["tIn"], $>;
|
|
27
|
+
get out(): Type<this["tOut"], $>;
|
|
28
|
+
intersect<def>(def: validateTypeRoot<def, $>): Type<inferIntersection<t, inferTypeRoot<def, $>>> | Disjoint;
|
|
40
29
|
and<def>(def: validateTypeRoot<def, $>): Type<inferIntersection<t, inferTypeRoot<def, $>>, $>;
|
|
41
30
|
or<def>(def: validateTypeRoot<def, $>): Type<t | inferTypeRoot<def, $>, $>;
|
|
42
|
-
morph<morph extends Morph<this["infer"]>>(morph: morph): Type<(In: this["in"]["infer"]) => Out<inferMorphOut<ReturnType<morph>>>, $>;
|
|
43
|
-
morph<morph extends Morph<this["infer"]>, def>(morph: morph, outValidator: validateTypeRoot<def, $>): Type<(In: this["in"]["infer"]) => Out<extractOut<inferTypeRoot<def, $>>>, $>;
|
|
44
|
-
narrow<def extends Predicate<extractOut<t>>>(def: def): Type<includesMorphs<t> extends true ? (In: this["in"]["infer"]) => Out<inferNarrow<this["infer"], def>> : inferNarrow<this["infer"], def>, $>;
|
|
45
31
|
array(): Type<t[], $>;
|
|
46
|
-
keyof(): Type<keyof this["
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
extends<
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
32
|
+
keyof(): Type<keyof this["inferIn"], $>;
|
|
33
|
+
pipe<a extends Morph<this["infer"]>>(a: a): Type<inferPipes<t, [a]>, $>;
|
|
34
|
+
pipe<a extends Morph<this["infer"]>, b extends Morph<inferMorphOut<a>>>(a: a, b: b): Type<inferPipes<t, [a, b]>, $>;
|
|
35
|
+
pipe<a extends Morph<this["infer"]>, b extends Morph<inferMorphOut<a>>, c extends Morph<inferMorphOut<b>>>(a: a, b: b, c: c): Type<inferPipes<t, [a, b, c]>, $>;
|
|
36
|
+
pipe<a extends Morph<this["infer"]>, b extends Morph<inferMorphOut<a>>, c extends Morph<inferMorphOut<b>>, d extends Morph<inferMorphOut<c>>>(a: a, b: b, c: c, d: d): Type<inferPipes<t, [a, b, c, d]>, $>;
|
|
37
|
+
pipe<a extends Morph<this["infer"]>, b extends Morph<inferMorphOut<a>>, c extends Morph<inferMorphOut<b>>, d extends Morph<inferMorphOut<c>>, e extends Morph<inferMorphOut<d>>>(a: a, b: b, c: c, d: d, e: e): Type<inferPipes<t, [a, b, c, d, e]>, $>;
|
|
38
|
+
pipe<a extends Morph<this["infer"]>, 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>>>(a: a, b: b, c: c, d: d, e: e, f: f): Type<inferPipes<t, [a, b, c, d, e, f]>, $>;
|
|
39
|
+
pipe<a extends Morph<this["infer"]>, 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>>>(a: a, b: b, c: c, d: d, e: e, f: f, g: g): Type<inferPipes<t, [a, b, c, d, e, f, g]>, $>;
|
|
40
|
+
narrow<def extends Predicate<distillConstrainableOut<t>>>(def: def): Type<includesMorphs<t> extends true ? (In: this["tIn"]) => Out<inferNarrow<this["infer"], def>> : inferNarrow<this["infer"], def>, $>;
|
|
41
|
+
equals<def>(def: validateTypeRoot<def, $>): this is Type<inferTypeRoot<def>, $>;
|
|
42
|
+
extract<def>(r: validateTypeRoot<def, $>): Type<t, $>;
|
|
43
|
+
exclude<def>(r: validateTypeRoot<def, $>): Type<t, $>;
|
|
44
|
+
extends<def>(other: validateTypeRoot<def, $>): this is Type<inferTypeRoot<def>, $>;
|
|
45
|
+
constrain<kind extends PrimitiveConstraintKind, const def extends NodeDef<kind>>(kind: conform<kind, constraintKindOf<this["inferIn"]>>, def: def): Type<constrain<t, kind, def>, $>;
|
|
54
46
|
}
|
|
55
|
-
export
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
export
|
|
60
|
-
export
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
definition: def;
|
|
65
|
-
scope: Scope;
|
|
66
|
-
};
|
|
67
|
-
export type BoundArgs = Record<string, TypeNode>;
|
|
68
|
-
export type Generic<params extends string[] = string[], def = unknown, $ = any> = {
|
|
69
|
-
<args>(...args: conform<args, {
|
|
70
|
-
[i in keyof params]: validateTypeRoot<args[i & keyof args], $>;
|
|
71
|
-
}>): Type<inferDefinition<def, $, bindGenericInstantiation<params, $, args>>, $>;
|
|
72
|
-
} & GenericProps<params, def, $>;
|
|
73
|
-
type bindGenericInstantiation<params extends string[], $, args> = {
|
|
74
|
-
[i in keyof params & `${number}` as params[i]]: inferTypeRoot<args[i & keyof args], $>;
|
|
75
|
-
};
|
|
47
|
+
export interface Type<
|
|
48
|
+
/** @ts-expect-error allow instantiation assignment to the base type */
|
|
49
|
+
out t = unknown, $ = any> extends _Type<t, $> {
|
|
50
|
+
}
|
|
51
|
+
export type TypeConstructor<t = unknown, $ = any> = new (def: unknown, $: Scope<$>) => Type<t, $>;
|
|
52
|
+
export declare const Type: TypeConstructor;
|
|
53
|
+
export type DefinitionParser<$> = <def>(def: validateTypeRoot<def, $>) => def;
|
|
54
|
+
export type validateTypeRoot<def, $ = {}> = validateDefinition<def, $ & ambient, bindThis<def>>;
|
|
55
|
+
export type inferTypeRoot<def, $ = {}> = inferDefinition<def, $ & ambient, bindThis<def>>;
|
|
76
56
|
export {};
|
|
77
|
-
//# sourceMappingURL=type.d.ts.map
|
package/out/type.js
CHANGED
|
@@ -1,134 +1,33 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { Callable
|
|
1
|
+
import { ArkErrors, RawSchema } from "@arktype/schema";
|
|
2
|
+
import { Callable } from "@arktype/util";
|
|
3
|
+
import { Generic } from "./generic.js";
|
|
3
4
|
import { parseGenericParams } from "./parser/generic.js";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
args
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
scope;
|
|
31
|
-
config;
|
|
32
|
-
root;
|
|
33
|
-
allows;
|
|
34
|
-
expected;
|
|
35
|
-
json;
|
|
36
|
-
constructor(definition, scope) {
|
|
37
|
-
const root = parseTypeRoot(definition, scope);
|
|
38
|
-
super(root.apply, root);
|
|
39
|
-
this.definition = definition;
|
|
40
|
-
this.scope = scope;
|
|
41
|
-
this.root = root;
|
|
42
|
-
this.allows = root.allows;
|
|
43
|
-
this.config = scope.config;
|
|
44
|
-
this.json = root.json;
|
|
45
|
-
this.expected = this.root.description;
|
|
46
|
-
}
|
|
47
|
-
configure(config) {
|
|
48
|
-
this.config = { ...this.config, ...config };
|
|
49
|
-
return this;
|
|
50
|
-
}
|
|
51
|
-
// TODO: should return out
|
|
52
|
-
from(literal) {
|
|
53
|
-
return literal;
|
|
54
|
-
}
|
|
55
|
-
// TODO: Morph intersections, ordering
|
|
56
|
-
and(def) {
|
|
57
|
-
return new Type(this.root.and(parseTypeRoot(def, this.scope)), this.scope);
|
|
58
|
-
}
|
|
59
|
-
or(def) {
|
|
60
|
-
return new Type(this.root.or(parseTypeRoot(def, this.scope)), this.scope);
|
|
61
|
-
}
|
|
62
|
-
morph(morph, outValidator) {
|
|
63
|
-
// TODO: tuple expression for out validator
|
|
64
|
-
outValidator;
|
|
65
|
-
return this;
|
|
66
|
-
// return new Type(
|
|
67
|
-
// this.root.constrain("morph", morph),
|
|
68
|
-
// this.scope
|
|
69
|
-
// ) as never
|
|
70
|
-
}
|
|
71
|
-
// TODO: based on below, should maybe narrow morph output if used after
|
|
72
|
-
narrow(def) {
|
|
73
|
-
return new Type(this.root.constrain("predicate", def), this.scope);
|
|
74
|
-
}
|
|
75
|
-
array() {
|
|
76
|
-
return new Type(this.root.array(), this.scope);
|
|
77
|
-
}
|
|
78
|
-
keyof() {
|
|
79
|
-
return new Type(this.root.keyof(), this.scope);
|
|
80
|
-
}
|
|
81
|
-
assert(data) {
|
|
82
|
-
const result = this.call(null, data);
|
|
83
|
-
return result.errors ? result.errors.throw() : result.out;
|
|
84
|
-
}
|
|
85
|
-
equals(other) {
|
|
86
|
-
return this.root.equals(parseTypeRoot(other, this.scope));
|
|
87
|
-
}
|
|
88
|
-
extends(other) {
|
|
89
|
-
return this.root.extends(parseTypeRoot(other, this.scope));
|
|
90
|
-
}
|
|
91
|
-
inCache;
|
|
92
|
-
get in() {
|
|
93
|
-
this.inCache ??= new Type(this.root.in, this.scope);
|
|
94
|
-
return this.inCache;
|
|
95
|
-
}
|
|
96
|
-
outCache;
|
|
97
|
-
get out() {
|
|
98
|
-
this.outCache ??= new Type(this.root.out, this.scope);
|
|
99
|
-
return this.outCache;
|
|
5
|
+
const typeParserAttachments = Object.freeze({
|
|
6
|
+
errors: ArkErrors
|
|
7
|
+
});
|
|
8
|
+
export class RawTypeParser extends Callable {
|
|
9
|
+
constructor($) {
|
|
10
|
+
super((...args) => {
|
|
11
|
+
if (args.length === 1) {
|
|
12
|
+
// treat as a simple definition
|
|
13
|
+
return $.parseRoot(args[0]);
|
|
14
|
+
}
|
|
15
|
+
if (args.length === 2 &&
|
|
16
|
+
typeof args[0] === "string" &&
|
|
17
|
+
args[0][0] === "<" &&
|
|
18
|
+
args[0].at(-1) === ">") {
|
|
19
|
+
// if there are exactly two args, the first of which looks like <${string}>,
|
|
20
|
+
// treat as a generic
|
|
21
|
+
const params = parseGenericParams(args[0].slice(1, -1));
|
|
22
|
+
const def = args[1];
|
|
23
|
+
// TODO: validateUninstantiatedGeneric, remove this cast
|
|
24
|
+
return new Generic(params, def, $);
|
|
25
|
+
}
|
|
26
|
+
// otherwise, treat as a tuple expression. technically, this also allows
|
|
27
|
+
// non-expression tuple definitions to be parsed, but it's not a supported
|
|
28
|
+
// part of the API as specified by the associated types
|
|
29
|
+
return $.parseRoot(args);
|
|
30
|
+
}, { bind: $, attach: typeParserAttachments });
|
|
100
31
|
}
|
|
101
32
|
}
|
|
102
|
-
const
|
|
103
|
-
args: args ?? scope.bindThis(),
|
|
104
|
-
baseName: "type"
|
|
105
|
-
});
|
|
106
|
-
export const validateUninstantiatedGeneric = (g) => {
|
|
107
|
-
// the unconstrained instantiation of the generic is not used for now
|
|
108
|
-
// other than to eagerly validate that the def does not contain any errors
|
|
109
|
-
g.scope.parseDefinition(g.definition,
|
|
110
|
-
// once we support constraints on generic parameters, we'd use
|
|
111
|
-
// the base type here: https://github.com/arktypeio/arktype/issues/796
|
|
112
|
-
{
|
|
113
|
-
baseName: "generic",
|
|
114
|
-
args: map(g.parameters, (_, name) => [name, keywords.unknown])
|
|
115
|
-
});
|
|
116
|
-
return g;
|
|
117
|
-
};
|
|
118
|
-
export const generic = (parameters, definition, scope) => {
|
|
119
|
-
return Object.assign((...args) => {
|
|
120
|
-
const argNodes = map(parameters, (i, param) => [
|
|
121
|
-
param,
|
|
122
|
-
parseTypeRoot(args[i], scope)
|
|
123
|
-
]);
|
|
124
|
-
const root = parseTypeRoot(definition, scope, argNodes);
|
|
125
|
-
return new Type(root, scope);
|
|
126
|
-
}, {
|
|
127
|
-
[arkKind]: "generic",
|
|
128
|
-
parameters,
|
|
129
|
-
definition,
|
|
130
|
-
scope
|
|
131
|
-
// $ is only needed at compile-time
|
|
132
|
-
});
|
|
133
|
-
};
|
|
134
|
-
//# sourceMappingURL=type.js.map
|
|
33
|
+
export const Type = RawSchema;
|
package/out/type.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"type.js","sourceRoot":"","sources":["../type.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,
|
|
1
|
+
{"version":3,"file":"type.js","sourceRoot":"","sources":["../type.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,SAAS,EAST,SAAS,EAcT,MAAM,iBAAiB,CAAA;AACxB,OAAO,EACN,QAAQ,EAIR,MAAM,eAAe,CAAA;AACtB,OAAO,EAAE,OAAO,EAAgC,MAAM,cAAc,CAAA;AAMpE,OAAO,EAAE,kBAAkB,EAAE,MAAM,qBAAqB,CAAA;AA8CxD,MAAM,qBAAqB,GAAG,MAAM,CAAC,MAAM,CAAC;IAC3C,MAAM,EAAE,SAAS;CACe,CAAC,CAAA;AAElC,MAAM,OAAO,aAAc,SAAQ,QAGlC;IACA,YAAY,CAAW;QACtB,KAAK,CACJ,CAAC,GAAG,IAAI,EAAE,EAAE;YACX,IAAI,IAAI,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;gBACvB,+BAA+B;gBAC/B,OAAO,CAAC,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAA;YAC5B,CAAC;YACD,IACC,IAAI,CAAC,MAAM,KAAK,CAAC;gBACjB,OAAO,IAAI,CAAC,CAAC,CAAC,KAAK,QAAQ;gBAC3B,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,GAAG;gBAClB,IAAI,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,KAAK,GAAG,EACrB,CAAC;gBACF,4EAA4E;gBAC5E,qBAAqB;gBACrB,MAAM,MAAM,GAAG,kBAAkB,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAA;gBACvD,MAAM,GAAG,GAAG,IAAI,CAAC,CAAC,CAAC,CAAA;gBACnB,wDAAwD;gBACxD,OAAO,IAAI,OAAO,CAAC,MAAM,EAAE,GAAG,EAAE,CAAU,CAAU,CAAA;YACrD,CAAC;YACD,wEAAwE;YACxE,0EAA0E;YAC1E,uDAAuD;YACvD,OAAO,CAAC,CAAC,SAAS,CAAC,IAAI,CAAC,CAAA;QACzB,CAAC,EACD,EAAE,IAAI,EAAE,CAAC,EAAE,MAAM,EAAE,qBAAqB,EAAE,CAC1C,CAAA;IACF,CAAC;CACD;AAoID,MAAM,CAAC,MAAM,IAAI,GAAoB,SAAkB,CAAA"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "arktype",
|
|
3
3
|
"description": "TypeScript's 1:1 validator, optimized from editor to runtime",
|
|
4
|
-
"version": "2.0.0-dev.
|
|
4
|
+
"version": "2.0.0-dev.10",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": {
|
|
7
7
|
"name": "David Blass",
|
|
@@ -20,6 +20,10 @@
|
|
|
20
20
|
"types": "./out/main.d.ts",
|
|
21
21
|
"default": "./out/main.js"
|
|
22
22
|
},
|
|
23
|
+
"./config": {
|
|
24
|
+
"types": "./out/config.d.ts",
|
|
25
|
+
"default": "./out/config.js"
|
|
26
|
+
},
|
|
23
27
|
"./internal/*": {
|
|
24
28
|
"default": "./out/*"
|
|
25
29
|
}
|
|
@@ -34,7 +38,7 @@
|
|
|
34
38
|
"test": "tsx ../repo/testPackage.ts"
|
|
35
39
|
},
|
|
36
40
|
"dependencies": {
|
|
37
|
-
"@arktype/util": "0.0.
|
|
38
|
-
"@arktype/schema": "0.
|
|
41
|
+
"@arktype/util": "0.0.36",
|
|
42
|
+
"@arktype/schema": "0.1.1"
|
|
39
43
|
}
|
|
40
44
|
}
|
package/parser/definition.ts
CHANGED
|
@@ -1,60 +1,60 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { type RawSchema, hasArkKind, type string } from "@arktype/schema"
|
|
2
2
|
import {
|
|
3
|
-
isThunk,
|
|
4
|
-
objectKindOf,
|
|
5
|
-
printable,
|
|
6
|
-
throwParseError,
|
|
7
|
-
type defined,
|
|
8
3
|
type Dict,
|
|
9
|
-
type equals,
|
|
10
4
|
type ErrorMessage,
|
|
11
|
-
type
|
|
5
|
+
type Primitive,
|
|
6
|
+
type array,
|
|
7
|
+
type defined,
|
|
8
|
+
type equals,
|
|
12
9
|
type isAny,
|
|
10
|
+
isThunk,
|
|
13
11
|
type isUnknown,
|
|
14
|
-
|
|
12
|
+
objectKindOf,
|
|
15
13
|
type objectKindOrDomainOf,
|
|
16
14
|
type optionalKeyOf,
|
|
17
|
-
|
|
18
|
-
type requiredKeyOf
|
|
15
|
+
printable,
|
|
16
|
+
type requiredKeyOf,
|
|
17
|
+
type show,
|
|
18
|
+
throwParseError
|
|
19
19
|
} from "@arktype/util"
|
|
20
20
|
import type { type } from "../ark.js"
|
|
21
21
|
import type { ParseContext } from "../scope.js"
|
|
22
|
-
import { Type } from "../type.js"
|
|
23
22
|
import {
|
|
24
|
-
parseObjectLiteral,
|
|
25
23
|
type inferObjectLiteral,
|
|
24
|
+
parseObjectLiteral,
|
|
26
25
|
type validateObjectLiteral
|
|
27
26
|
} from "./objectLiteral.js"
|
|
28
27
|
import type { validateString } from "./semantic/validate.js"
|
|
29
28
|
import type { BaseCompletions, inferString } from "./string/string.js"
|
|
30
29
|
import {
|
|
31
|
-
parseTuple,
|
|
32
|
-
type inferTuple,
|
|
33
30
|
type TupleExpression,
|
|
31
|
+
type inferTuple,
|
|
32
|
+
parseTuple,
|
|
34
33
|
type validateTuple
|
|
35
34
|
} from "./tuple.js"
|
|
36
35
|
|
|
37
|
-
export const parseObject = (def: object, ctx: ParseContext):
|
|
36
|
+
export const parseObject = (def: object, ctx: ParseContext): RawSchema => {
|
|
38
37
|
const objectKind = objectKindOf(def)
|
|
39
38
|
switch (objectKind) {
|
|
40
39
|
case undefined:
|
|
41
|
-
if (def
|
|
42
|
-
return def.root
|
|
43
|
-
}
|
|
44
|
-
if (isNode(def) && def.isType()) {
|
|
45
|
-
return def
|
|
46
|
-
}
|
|
40
|
+
if (hasArkKind(def, "schema")) return def
|
|
47
41
|
return parseObjectLiteral(def as Dict, ctx)
|
|
48
42
|
case "Array":
|
|
49
|
-
return parseTuple(def as
|
|
43
|
+
return parseTuple(def as array, ctx)
|
|
50
44
|
case "RegExp":
|
|
51
|
-
return
|
|
52
|
-
|
|
45
|
+
return ctx.$.node(
|
|
46
|
+
"intersection",
|
|
47
|
+
{
|
|
48
|
+
domain: "string",
|
|
49
|
+
regex: def as RegExp
|
|
50
|
+
},
|
|
51
|
+
{ prereduced: true }
|
|
52
|
+
)
|
|
53
|
+
case "Function": {
|
|
53
54
|
const resolvedDef = isThunk(def) ? def() : def
|
|
54
|
-
if (resolvedDef
|
|
55
|
-
return resolvedDef.root
|
|
56
|
-
}
|
|
55
|
+
if (hasArkKind(resolvedDef, "schema")) return resolvedDef
|
|
57
56
|
return throwParseError(writeBadDefinitionTypeMessage("Function"))
|
|
57
|
+
}
|
|
58
58
|
default:
|
|
59
59
|
return throwParseError(
|
|
60
60
|
writeBadDefinitionTypeMessage(objectKind ?? printable(def))
|
|
@@ -62,80 +62,64 @@ export const parseObject = (def: object, ctx: ParseContext): TypeNode => {
|
|
|
62
62
|
}
|
|
63
63
|
}
|
|
64
64
|
|
|
65
|
-
export type inferDefinition<def, $, args> =
|
|
66
|
-
? never
|
|
67
|
-
: def extends type.cast<infer t> | ThunkCast<infer t>
|
|
68
|
-
?
|
|
69
|
-
: def extends
|
|
70
|
-
|
|
71
|
-
: def extends
|
|
72
|
-
? inferTuple<def, $, args>
|
|
73
|
-
: def extends RegExp
|
|
74
|
-
? string
|
|
75
|
-
: def extends object
|
|
76
|
-
? inferObjectLiteral<def, $, args>
|
|
65
|
+
export type inferDefinition<def, $, args> =
|
|
66
|
+
isAny<def> extends true ? never
|
|
67
|
+
: def extends type.cast<infer t> | ThunkCast<infer t> ? t
|
|
68
|
+
: def extends string ? inferString<def, $, args>
|
|
69
|
+
: def extends array ? inferTuple<def, $, args>
|
|
70
|
+
: def extends RegExp ? string.matching<string>
|
|
71
|
+
: def extends object ? inferObjectLiteral<def, $, args>
|
|
77
72
|
: never
|
|
78
73
|
|
|
79
|
-
export type validateDefinition<def, $, args> =
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
: def extends
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
:
|
|
92
|
-
? writeBadDefinitionTypeMessage<objectKindOrDomainOf<def>>
|
|
93
|
-
: isUnknown<def> extends true
|
|
94
|
-
? // this allows the initial list of autocompletions to be populated when a user writes "type()",
|
|
95
|
-
// before having specified a definition
|
|
96
|
-
BaseCompletions<$, args> | {}
|
|
97
|
-
: validateObjectLiteral<def, $, args>
|
|
74
|
+
export type validateDefinition<def, $, args> =
|
|
75
|
+
null extends undefined ?
|
|
76
|
+
ErrorMessage<`'strict' or 'strictNullChecks' must be set to true in your tsconfig's 'compilerOptions'`>
|
|
77
|
+
: [def] extends [Terminal] ? def
|
|
78
|
+
: def extends string ? validateString<def, $, args>
|
|
79
|
+
: def extends array ? validateTuple<def, $, args>
|
|
80
|
+
: def extends BadDefinitionType ?
|
|
81
|
+
writeBadDefinitionTypeMessage<objectKindOrDomainOf<def>>
|
|
82
|
+
: isUnknown<def> extends true ?
|
|
83
|
+
// this allows the initial list of autocompletions to be populated when a user writes "type()",
|
|
84
|
+
// before having specified a definition
|
|
85
|
+
BaseCompletions<$, args> | {}
|
|
86
|
+
: validateObjectLiteral<def, $, args>
|
|
98
87
|
|
|
99
88
|
export type validateDeclared<declared, def, $, args> =
|
|
100
|
-
def extends validateDefinition<def, $, args>
|
|
101
|
-
|
|
102
|
-
|
|
89
|
+
def extends validateDefinition<def, $, args> ?
|
|
90
|
+
validateInference<def, declared, $, args>
|
|
91
|
+
: validateDefinition<def, $, args>
|
|
103
92
|
|
|
104
|
-
type validateInference<def, declared, $, args> =
|
|
105
|
-
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
: evaluate<declarationMismatch<def, declared, $, args>>
|
|
118
|
-
: def extends object
|
|
119
|
-
? evaluate<
|
|
93
|
+
type validateInference<def, declared, $, args> =
|
|
94
|
+
def extends RegExp | type.cast<unknown> | ThunkCast | TupleExpression ?
|
|
95
|
+
validateShallowInference<def, declared, $, args>
|
|
96
|
+
: def extends array ?
|
|
97
|
+
declared extends array ?
|
|
98
|
+
{
|
|
99
|
+
[i in keyof declared]: i extends keyof def ?
|
|
100
|
+
validateInference<def[i], declared[i], $, args>
|
|
101
|
+
: unknown
|
|
102
|
+
}
|
|
103
|
+
: show<declarationMismatch<def, declared, $, args>>
|
|
104
|
+
: def extends object ?
|
|
105
|
+
show<
|
|
120
106
|
{
|
|
121
|
-
[k in requiredKeyOf<declared>]: k extends keyof def
|
|
122
|
-
|
|
123
|
-
|
|
107
|
+
[k in requiredKeyOf<declared>]: k extends keyof def ?
|
|
108
|
+
validateInference<def[k], declared[k], $, args>
|
|
109
|
+
: unknown
|
|
124
110
|
} & {
|
|
125
|
-
[k in optionalKeyOf<declared> &
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
111
|
+
[k in optionalKeyOf<declared> & string as `${k}?`]: `${k}?` extends (
|
|
112
|
+
keyof def
|
|
113
|
+
) ?
|
|
114
|
+
validateInference<def[`${k}?`], defined<declared[k]>, $, args>
|
|
115
|
+
: unknown
|
|
129
116
|
}
|
|
130
|
-
|
|
131
|
-
:
|
|
117
|
+
>
|
|
118
|
+
: validateShallowInference<def, declared, $, args>
|
|
132
119
|
|
|
133
|
-
type validateShallowInference<def, declared, $, args> =
|
|
134
|
-
inferDefinition<def, $, args>,
|
|
135
|
-
declared
|
|
136
|
-
> extends true
|
|
137
|
-
? def
|
|
138
|
-
: evaluate<declarationMismatch<def, declared, $, args>>
|
|
120
|
+
type validateShallowInference<def, declared, $, args> =
|
|
121
|
+
equals<inferDefinition<def, $, args>, declared> extends true ? def
|
|
122
|
+
: show<declarationMismatch<def, declared, $, args>>
|
|
139
123
|
|
|
140
124
|
type declarationMismatch<def, declared, $, args> = {
|
|
141
125
|
declared: declared
|