arktype 2.0.0-beta.2 → 2.0.0-beta.4
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/ark.d.ts +18 -4
- package/out/ark.js +22 -3
- package/out/ast.d.ts +217 -0
- package/out/ast.js +3 -0
- package/out/config.d.ts +12 -0
- package/out/generic.d.ts +106 -20
- package/out/index.d.ts +6 -5
- package/out/index.js +3 -2
- package/out/intersect.d.ts +28 -0
- package/out/intersect.js +1 -0
- package/out/keys.d.ts +16 -0
- package/out/keys.js +1 -0
- package/out/keywords/arkGenerics.d.ts +19 -0
- package/out/keywords/arkGenerics.js +19 -0
- package/out/keywords/format.d.ts +11 -0
- package/out/keywords/format.js +31 -0
- package/out/keywords/internal.d.ts +11 -0
- package/out/keywords/internal.js +25 -0
- package/out/keywords/jsObjects.d.ts +15 -0
- package/out/keywords/jsObjects.js +13 -0
- package/out/keywords/parsing.d.ts +16 -0
- package/out/keywords/parsing.js +77 -0
- package/out/keywords/platformObjects.d.ts +13 -0
- package/out/keywords/platformObjects.js +12 -0
- package/out/keywords/tsGenerics.d.ts +37 -0
- package/out/keywords/tsGenerics.js +45 -0
- package/out/keywords/tsKeywords.d.ts +18 -0
- package/out/keywords/tsKeywords.js +17 -0
- package/out/keywords/typedArray.d.ts +16 -0
- package/out/keywords/typedArray.js +14 -0
- package/out/keywords/utils/creditCard.d.ts +2 -0
- package/out/keywords/utils/creditCard.js +25 -0
- package/out/keywords/utils/date.d.ts +20 -0
- package/out/keywords/utils/date.js +36 -0
- package/out/keywords/utils/ip.d.ts +3 -0
- package/out/keywords/utils/ip.js +20 -0
- package/out/keywords/utils/regex.d.ts +2 -0
- package/out/keywords/utils/regex.js +9 -0
- package/out/keywords/validation.d.ts +19 -0
- package/out/keywords/validation.js +84 -0
- package/out/module.d.ts +19 -11
- package/out/module.js +1 -0
- package/out/parser/definition.d.ts +5 -4
- package/out/parser/objectLiteral.d.ts +7 -6
- package/out/parser/objectLiteral.js +2 -2
- package/out/parser/semantic/bounds.d.ts +2 -1
- package/out/parser/semantic/divisor.d.ts +2 -2
- package/out/parser/semantic/infer.d.ts +14 -9
- package/out/parser/semantic/keyof.d.ts +7 -0
- package/out/parser/semantic/keyof.js +1 -0
- package/out/parser/semantic/validate.d.ts +4 -5
- package/out/parser/semantic/validate.js +0 -1
- package/out/parser/string/reduce/dynamic.d.ts +2 -1
- package/out/parser/string/reduce/shared.d.ts +1 -1
- package/out/parser/string/reduce/static.d.ts +1 -1
- package/out/parser/string/shift/operand/date.d.ts +3 -1
- package/out/parser/string/shift/operand/enclosed.js +1 -1
- package/out/parser/string/shift/operand/genericArgs.d.ts +4 -4
- package/out/parser/string/shift/operand/unenclosed.d.ts +13 -8
- package/out/parser/string/shift/operator/bounds.d.ts +2 -1
- package/out/parser/string/shift/operator/bounds.js +4 -4
- package/out/parser/string/shift/operator/default.d.ts +2 -1
- package/out/parser/string/string.d.ts +2 -1
- package/out/parser/tuple.d.ts +5 -3
- package/out/parser/tuple.js +5 -5
- package/out/scope.d.ts +47 -26
- package/out/scope.js +2 -7
- package/out/subtypes/array.d.ts +13 -0
- package/out/subtypes/array.js +1 -0
- package/out/subtypes/base.d.ts +83 -0
- package/out/subtypes/base.js +1 -0
- package/out/subtypes/date.d.ts +11 -0
- package/out/subtypes/date.js +1 -0
- package/out/subtypes/instantiate.d.ts +16 -0
- package/out/subtypes/instantiate.js +1 -0
- package/out/subtypes/morph.d.ts +13 -0
- package/out/subtypes/morph.js +1 -0
- package/out/subtypes/number.d.ts +12 -0
- package/out/subtypes/number.js +1 -0
- package/out/subtypes/object.d.ts +27 -0
- package/out/subtypes/object.js +1 -0
- package/out/subtypes/string.d.ts +13 -0
- package/out/subtypes/string.js +1 -0
- package/out/subtypes/validator.d.ts +11 -0
- package/out/subtypes/validator.js +1 -0
- package/out/type.d.ts +17 -71
- package/package.json +3 -3
- package/out/match.d.ts +0 -71
- package/out/match.js +0 -50
package/out/scope.js
CHANGED
|
@@ -32,10 +32,9 @@ var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn,
|
|
|
32
32
|
if (target) Object.defineProperty(target, contextIn.name, descriptor);
|
|
33
33
|
done = true;
|
|
34
34
|
};
|
|
35
|
-
import {
|
|
35
|
+
import { BaseScope, hasArkKind, parseGeneric } from "@ark/schema";
|
|
36
36
|
import { bound, domainOf, hasDomain, isThunk, throwParseError } from "@ark/util";
|
|
37
37
|
import { parseGenericParams } from "./generic.js";
|
|
38
|
-
import { createMatchParser } from "./match.js";
|
|
39
38
|
import { parseObject, writeBadDefinitionTypeMessage } from "./parser/definition.js";
|
|
40
39
|
import { DynamicState } from "./parser/string/reduce/dynamic.js";
|
|
41
40
|
import { writeUnexpectedCharacterMessage } from "./parser/string/shift/operator/operator.js";
|
|
@@ -44,7 +43,7 @@ import { fullStringParse } from "./parser/string/string.js";
|
|
|
44
43
|
import { InternalTypeParser } from "./type.js";
|
|
45
44
|
export const scope = ((def, config = {}) => new InternalScope(def, config));
|
|
46
45
|
let InternalScope = (() => {
|
|
47
|
-
let _classSuper =
|
|
46
|
+
let _classSuper = BaseScope;
|
|
48
47
|
let _instanceExtraInitializers = [];
|
|
49
48
|
let _parseRoot_decorators;
|
|
50
49
|
return class InternalScope extends _classSuper {
|
|
@@ -55,11 +54,7 @@ let InternalScope = (() => {
|
|
|
55
54
|
if (_metadata) Object.defineProperty(this, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
|
|
56
55
|
}
|
|
57
56
|
parseCache = (__runInitializers(this, _instanceExtraInitializers), {});
|
|
58
|
-
constructor(def, config) {
|
|
59
|
-
super(def, config);
|
|
60
|
-
}
|
|
61
57
|
type = new InternalTypeParser(this);
|
|
62
|
-
match = createMatchParser(this);
|
|
63
58
|
declare = (() => ({
|
|
64
59
|
type: this.type
|
|
65
60
|
})).bind(this);
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { ExactLength, ExclusiveNumericRangeSchema, exclusivizeRangeSchema, InclusiveNumericRangeSchema } from "@ark/schema";
|
|
2
|
+
import type { applyConstraint } from "../ast.js";
|
|
3
|
+
import type { ObjectType } from "./object.js";
|
|
4
|
+
interface Type<
|
|
5
|
+
/** @ts-ignore cast variance */
|
|
6
|
+
out t extends readonly unknown[] = readonly unknown[], $ = {}> extends ObjectType<t, $> {
|
|
7
|
+
atLeastLength<const schema extends InclusiveNumericRangeSchema>(schema: schema): Type<applyConstraint<t, "minLength", schema>, $>;
|
|
8
|
+
atMostLength<const schema extends InclusiveNumericRangeSchema>(schema: schema): Type<applyConstraint<t, "maxLength", schema>, $>;
|
|
9
|
+
moreThanLength<const schema extends ExclusiveNumericRangeSchema>(schema: schema): Type<applyConstraint<t, "minLength", exclusivizeRangeSchema<schema>>, $>;
|
|
10
|
+
lessThanLength<const schema extends ExclusiveNumericRangeSchema>(schema: schema): Type<applyConstraint<t, "maxLength", exclusivizeRangeSchema<schema>>, $>;
|
|
11
|
+
exactlyLength<const schema extends ExactLength.Schema>(schema: schema): Type<applyConstraint<t, "exactLength", schema>, $>;
|
|
12
|
+
}
|
|
13
|
+
export type { Type as ArrayType };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import type { ArkErrors, BaseRoot, Disjoint, MetaSchema, Morph, UndeclaredKeyBehavior } from "@ark/schema";
|
|
2
|
+
import type { anyOrNever, Callable, ErrorMessage, Json, unset } from "@ark/util";
|
|
3
|
+
import type { distillConstrainableIn, distillConstrainableOut, distillIn, distillOut, distillValidatedOut, inferMorphOut, inferPipes, inferred } from "../ast.js";
|
|
4
|
+
import type { inferIntersection } from "../intersect.js";
|
|
5
|
+
import type { Scope } from "../scope.js";
|
|
6
|
+
import type { inferTypeRoot, validateTypeRoot } from "../type.js";
|
|
7
|
+
import type { ArrayType } from "./array.js";
|
|
8
|
+
import type { instantiateType } from "./instantiate.js";
|
|
9
|
+
/** @ts-ignore cast variance */
|
|
10
|
+
interface Type<out t = unknown, $ = {}> extends Callable<(data: unknown) => distillOut<t> | ArkErrors> {
|
|
11
|
+
t: t;
|
|
12
|
+
tIn: distillConstrainableIn<t>;
|
|
13
|
+
tOut: distillConstrainableOut<t>;
|
|
14
|
+
tValidatedOut: distillValidatedOut<t>;
|
|
15
|
+
infer: distillOut<t>;
|
|
16
|
+
inferIn: distillIn<t>;
|
|
17
|
+
[inferred]: t;
|
|
18
|
+
json: Json;
|
|
19
|
+
description: string;
|
|
20
|
+
expression: string;
|
|
21
|
+
internal: BaseRoot;
|
|
22
|
+
$: Scope<$>;
|
|
23
|
+
assert(data: unknown): this["infer"];
|
|
24
|
+
allows(data: unknown): data is this["inferIn"];
|
|
25
|
+
traverse(data: unknown): distillOut<t> | ArkErrors;
|
|
26
|
+
configure(meta: MetaSchema): this;
|
|
27
|
+
describe(description: string): this;
|
|
28
|
+
onUndeclaredKey(behavior: UndeclaredKeyBehavior): this;
|
|
29
|
+
onDeepUndeclaredKey(behavior: UndeclaredKeyBehavior): this;
|
|
30
|
+
from(literal: this["inferIn"]): this["infer"];
|
|
31
|
+
as<t = unset>(...args: validateChainedAsArgs<t>): instantiateType<t, $>;
|
|
32
|
+
get in(): instantiateType<this["tIn"], $>;
|
|
33
|
+
get out(): instantiateType<this["tValidatedOut"], $>;
|
|
34
|
+
intersect<const def, r = inferTypeRoot<def, $>>(def: validateTypeRoot<def, $>): instantiateType<inferIntersection<t, r>, $> | Disjoint;
|
|
35
|
+
and<const def, r = inferTypeRoot<def, $>>(def: validateTypeRoot<def, $>): instantiateType<inferIntersection<t, r>, $>;
|
|
36
|
+
or<const def, r = inferTypeRoot<def, $>>(def: validateTypeRoot<def, $>): instantiateType<t | r, $>;
|
|
37
|
+
array(): ArrayType<t[], $>;
|
|
38
|
+
pipe: ChainedPipes<t, $>;
|
|
39
|
+
equals<def>(def: validateTypeRoot<def, $>): this is instantiateType<inferTypeRoot<def, $>, $>;
|
|
40
|
+
extract<def>(r: validateTypeRoot<def, $>): instantiateType<Extract<t, inferTypeRoot<def, $>>, $>;
|
|
41
|
+
exclude<def>(r: validateTypeRoot<def, $>): instantiateType<Exclude<t, inferTypeRoot<def, $>>, $>;
|
|
42
|
+
extends<def>(other: validateTypeRoot<def, $>): this is instantiateType<inferTypeRoot<def, $>, $>;
|
|
43
|
+
overlaps<def>(r: validateTypeRoot<def, $>): boolean;
|
|
44
|
+
/** @deprecated */
|
|
45
|
+
apply: Function["apply"];
|
|
46
|
+
/** @deprecated */
|
|
47
|
+
bind: Function["bind"];
|
|
48
|
+
/** @deprecated */
|
|
49
|
+
call: Function["call"];
|
|
50
|
+
/** @deprecated */
|
|
51
|
+
caller: Function;
|
|
52
|
+
/** @deprecated */
|
|
53
|
+
length: number;
|
|
54
|
+
/** @deprecated */
|
|
55
|
+
name: string;
|
|
56
|
+
/** @deprecated */
|
|
57
|
+
prototype: Function["prototype"];
|
|
58
|
+
/** @deprecated */
|
|
59
|
+
arguments: Function["arguments"];
|
|
60
|
+
/** @deprecated */
|
|
61
|
+
Symbol: never;
|
|
62
|
+
}
|
|
63
|
+
interface ChainedPipeSignature<t, $> {
|
|
64
|
+
<a extends Morph<distillOut<t>>, r = instantiateType<inferPipes<t, [a]>, $>>(a: a): r;
|
|
65
|
+
<a extends Morph<distillOut<t>>, b extends Morph<inferMorphOut<a>>, r = instantiateType<inferPipes<t, [a, b]>, $>>(a: a, b: b): r;
|
|
66
|
+
<a extends Morph<distillOut<t>>, b extends Morph<inferMorphOut<a>>, c extends Morph<inferMorphOut<b>>, r = instantiateType<inferPipes<t, [a, b, c]>, $>>(a: a, b: b, c: c): r;
|
|
67
|
+
<a extends Morph<distillOut<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): r;
|
|
68
|
+
<a extends Morph<distillOut<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): r;
|
|
69
|
+
<a extends Morph<distillOut<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): r;
|
|
70
|
+
<a extends Morph<distillOut<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): r;
|
|
71
|
+
}
|
|
72
|
+
export interface ChainedPipes<t, $> extends ChainedPipeSignature<t, $> {
|
|
73
|
+
try: ChainedPipeSignature<t, $>;
|
|
74
|
+
}
|
|
75
|
+
type validateChainedAsArgs<t> = [
|
|
76
|
+
t
|
|
77
|
+
] extends [unset] ? [
|
|
78
|
+
t
|
|
79
|
+
] extends [anyOrNever] ? [
|
|
80
|
+
] : [
|
|
81
|
+
ErrorMessage<"as requires an explicit type parameter like myType.as<t>()">
|
|
82
|
+
] : [];
|
|
83
|
+
export type { Type as BaseType };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { ExclusiveDateRangeSchema, exclusivizeRangeSchema, InclusiveDateRangeSchema } from "@ark/schema";
|
|
2
|
+
import type { applyConstraint } from "../ast.js";
|
|
3
|
+
import type { ObjectType } from "./object.js";
|
|
4
|
+
/** @ts-ignore cast variance */
|
|
5
|
+
interface Type<out t extends globalThis.Date = globalThis.Date, $ = {}> extends ObjectType<t, $> {
|
|
6
|
+
atOrAfter<const schema extends InclusiveDateRangeSchema>(schema: schema): Type<applyConstraint<t, "after", schema>, $>;
|
|
7
|
+
atOrBefore<const schema extends InclusiveDateRangeSchema>(schema: schema): Type<applyConstraint<t, "before", schema>, $>;
|
|
8
|
+
laterThan<const schema extends ExclusiveDateRangeSchema>(schema: schema): Type<applyConstraint<t, "after", exclusivizeRangeSchema<schema>>, $>;
|
|
9
|
+
earlierThan<const schema extends ExclusiveDateRangeSchema>(schema: schema): Type<applyConstraint<t, "before", exclusivizeRangeSchema<schema>>, $>;
|
|
10
|
+
}
|
|
11
|
+
export type { Type as DateType };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { anyOrNever, array } from "@ark/util";
|
|
2
|
+
import type { MorphAst } from "../ast.js";
|
|
3
|
+
import type { ArrayType } from "./array.js";
|
|
4
|
+
import type { DateType } from "./date.js";
|
|
5
|
+
import type { MorphType } from "./morph.js";
|
|
6
|
+
import type { NumberType } from "./number.js";
|
|
7
|
+
import type { ObjectType } from "./object.js";
|
|
8
|
+
import type { StringType } from "./string.js";
|
|
9
|
+
import type { ValidatorType } from "./validator.js";
|
|
10
|
+
export type instantiateType<t, $> = [
|
|
11
|
+
Extract<t, MorphAst>
|
|
12
|
+
] extends [anyOrNever] ? [
|
|
13
|
+
t
|
|
14
|
+
] extends [string] ? StringType<t, $> : [t] extends [number] ? NumberType<t, $> : [t] extends [object] ? [
|
|
15
|
+
t
|
|
16
|
+
] extends [array] ? ArrayType<t, $> : [t] extends [Date] ? DateType<t, $> : ObjectType<t, $> : ValidatorType<t, $> : MorphType<t, $>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Predicate, PredicateCast } from "@ark/schema";
|
|
2
|
+
import type { applyConstraint, Out, To } from "../ast.js";
|
|
3
|
+
import type { inferPipe } from "../intersect.js";
|
|
4
|
+
import type { inferTypeRoot, validateTypeRoot } from "../type.js";
|
|
5
|
+
import type { BaseType } from "./base.js";
|
|
6
|
+
/** @ts-ignore cast variance */
|
|
7
|
+
interface Type<out t = unknown, $ = {}> extends BaseType<t, $> {
|
|
8
|
+
to<const def, r = inferTypeRoot<def, $>>(def: validateTypeRoot<def, $>): Type<inferPipe<t, r>, $>;
|
|
9
|
+
narrow<narrowed extends this["infer"] = never>(predicate: Predicate<this["infer"]> | PredicateCast<this["infer"], narrowed>): Type<([
|
|
10
|
+
narrowed
|
|
11
|
+
] extends [never] ? applyConstraint<this["tOut"], "predicate", Predicate> : narrowed) extends infer o ? this["tValidatedOut"] extends this["tOut"] ? (In: this["tIn"]) => To<o> : (In: this["tIn"]) => Out<o> : never, $>;
|
|
12
|
+
}
|
|
13
|
+
export type { Type as MorphType };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Divisor, ExclusiveNumericRangeSchema, exclusivizeRangeSchema, InclusiveNumericRangeSchema } from "@ark/schema";
|
|
2
|
+
import type { applyConstraint } from "../ast.js";
|
|
3
|
+
import type { ValidatorType } from "./validator.js";
|
|
4
|
+
/** @ts-ignore cast variance */
|
|
5
|
+
interface Type<out t extends number = number, $ = {}> extends ValidatorType<t, $> {
|
|
6
|
+
divisibleBy<const schema extends Divisor.Schema>(schema: schema): Type<applyConstraint<t, "divisor", schema>, $>;
|
|
7
|
+
atLeast<const schema extends InclusiveNumericRangeSchema>(schema: schema): Type<applyConstraint<t, "min", schema>, $>;
|
|
8
|
+
atMost<const schema extends InclusiveNumericRangeSchema>(schema: schema): Type<applyConstraint<t, "max", schema>, $>;
|
|
9
|
+
moreThan<const schema extends ExclusiveNumericRangeSchema>(schema: schema): Type<applyConstraint<t, "min", exclusivizeRangeSchema<schema>>, $>;
|
|
10
|
+
lessThan<const schema extends ExclusiveNumericRangeSchema>(schema: schema): Type<applyConstraint<t, "max", exclusivizeRangeSchema<schema>>, $>;
|
|
11
|
+
}
|
|
12
|
+
export type { Type as NumberType };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { ErrorType, merge } from "@ark/util";
|
|
2
|
+
import type { type } from "../ark.js";
|
|
3
|
+
import type { arkKeyOf, getArkKey, toArkKey } from "../keys.js";
|
|
4
|
+
import type { inferTypeRoot, validateTypeRoot } from "../type.js";
|
|
5
|
+
import type { instantiateType } from "./instantiate.js";
|
|
6
|
+
import type { ValidatorType } from "./validator.js";
|
|
7
|
+
/** @ts-ignore cast variance */
|
|
8
|
+
interface Type<out t extends object = object, $ = {}> extends ValidatorType<t, $> {
|
|
9
|
+
keyof(): instantiateType<keyof t, $>;
|
|
10
|
+
get<k1 extends arkKeyOf<t>, r = instantiateType<getArkKey<t, k1>, $>>(k1: k1 | type.cast<k1>): r;
|
|
11
|
+
get<k1 extends arkKeyOf<t>, k2 extends arkKeyOf<getArkKey<t, k1>>, r = instantiateType<getArkKey<getArkKey<t, k1>, k2>, $>>(k1: k1 | type.cast<k1>, k2: k2 | type.cast<k2>): r;
|
|
12
|
+
get<k1 extends arkKeyOf<t>, k2 extends arkKeyOf<getArkKey<t, k1>>, k3 extends arkKeyOf<getArkKey<getArkKey<t, k1>, k2>>, r = instantiateType<getArkKey<getArkKey<getArkKey<t, k1>, k2>, k3>, $>>(k1: k1 | type.cast<k1>, k2: k2 | type.cast<k2>, k3: k3 | type.cast<k3>): r;
|
|
13
|
+
pick<const key extends arkKeyOf<t> = never>(...keys: (key | type.cast<key>)[]): Type<{
|
|
14
|
+
[k in keyof t as Extract<toArkKey<t, k>, key>]: t[k];
|
|
15
|
+
}, $>;
|
|
16
|
+
omit<const key extends arkKeyOf<t> = never>(...keys: (key | type.cast<key>)[]): Type<{
|
|
17
|
+
[k in keyof t as Exclude<toArkKey<t, k>, key>]: t[k];
|
|
18
|
+
}, $>;
|
|
19
|
+
merge<const def, r = inferTypeRoot<def, $>>(def: validateTypeRoot<def, $> & (r extends object ? unknown : ErrorType<"Merged type must be an object", [actual: r]>)): Type<merge<t, r & object>, $>;
|
|
20
|
+
required(): Type<{
|
|
21
|
+
[k in keyof t]-?: t[k];
|
|
22
|
+
}, $>;
|
|
23
|
+
partial(): Type<{
|
|
24
|
+
[k in keyof t]?: t[k];
|
|
25
|
+
}, $>;
|
|
26
|
+
}
|
|
27
|
+
export type { Type as ObjectType };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { ExactLength, ExclusiveNumericRangeSchema, exclusivizeRangeSchema, InclusiveNumericRangeSchema, Pattern } from "@ark/schema";
|
|
2
|
+
import type { applyConstraint } from "../ast.js";
|
|
3
|
+
import type { ValidatorType } from "./validator.js";
|
|
4
|
+
/** @ts-ignore cast variance */
|
|
5
|
+
interface Type<out t extends string = string, $ = {}> extends ValidatorType<t, $> {
|
|
6
|
+
matching<const schema extends Pattern.Schema>(schema: schema): Type<applyConstraint<t, "pattern", schema>, $>;
|
|
7
|
+
atLeastLength<const schema extends InclusiveNumericRangeSchema>(schema: schema): Type<applyConstraint<t, "minLength", schema>, $>;
|
|
8
|
+
atMostLength<const schema extends InclusiveNumericRangeSchema>(schema: schema): Type<applyConstraint<t, "maxLength", schema>, $>;
|
|
9
|
+
moreThanLength<const schema extends ExclusiveNumericRangeSchema>(schema: schema): Type<applyConstraint<t, "minLength", exclusivizeRangeSchema<schema>>, $>;
|
|
10
|
+
lessThanLength<const schema extends ExclusiveNumericRangeSchema>(schema: schema): Type<applyConstraint<t, "maxLength", exclusivizeRangeSchema<schema>>, $>;
|
|
11
|
+
exactlyLength<const schema extends ExactLength.Schema>(schema: schema): Type<applyConstraint<t, "exactLength", schema>, $>;
|
|
12
|
+
}
|
|
13
|
+
export type { Type as StringType };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Predicate, PredicateCast } from "@ark/schema";
|
|
2
|
+
import type { applyConstraint } from "../ast.js";
|
|
3
|
+
import type { BaseType } from "./base.js";
|
|
4
|
+
import type { instantiateType } from "./instantiate.js";
|
|
5
|
+
/** @ts-ignore cast variance */
|
|
6
|
+
interface Type<out t = unknown, $ = {}> extends BaseType<t, $> {
|
|
7
|
+
narrow<narrowed extends this["infer"] = never>(predicate: Predicate<this["infer"]> | PredicateCast<this["infer"], narrowed>): instantiateType<[
|
|
8
|
+
narrowed
|
|
9
|
+
] extends [never] ? applyConstraint<t, "predicate", Predicate> : narrowed, $>;
|
|
10
|
+
}
|
|
11
|
+
export type { Type as ValidatorType };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/out/type.d.ts
CHANGED
|
@@ -1,89 +1,35 @@
|
|
|
1
|
-
import { ArkErrors, BaseRoot, type
|
|
2
|
-
import { Callable, type Constructor, type array, type conform
|
|
3
|
-
import type {
|
|
1
|
+
import { ArkErrors, BaseRoot, type MetaSchema, type Morph, type Predicate } from "@ark/schema";
|
|
2
|
+
import { Callable, type Constructor, type array, type conform } from "@ark/util";
|
|
3
|
+
import type { distillIn, distillOut } from "./ast.js";
|
|
4
4
|
import { type Generic, type ParameterString, type baseGenericConstraints, type parseValidGenericParams, type validateParameterString } from "./generic.js";
|
|
5
5
|
import type { inferDefinition, validateDeclared, validateDefinition } from "./parser/definition.js";
|
|
6
6
|
import type { IndexOneOperator, IndexZeroOperator, TupleInfixOperator } from "./parser/tuple.js";
|
|
7
7
|
import type { InternalScope, Scope, bindThis } from "./scope.js";
|
|
8
|
+
import type { BaseType } from "./subtypes/base.js";
|
|
9
|
+
import type { instantiateType } from "./subtypes/instantiate.js";
|
|
8
10
|
/** The convenience properties attached to `type` */
|
|
9
11
|
export type TypeParserAttachments = Omit<TypeParser, never>;
|
|
10
12
|
export interface TypeParser<$ = {}> {
|
|
11
|
-
<const def
|
|
12
|
-
<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], $>, $>;
|
|
13
|
+
<const def, r = Type<inferTypeRoot<def, $>, $>>(def: validateTypeRoot<def, $>): r;
|
|
13
14
|
<params extends ParameterString, const def>(params: validateParameterString<params, $>, def: validateDefinition<def, $, baseGenericConstraints<parseValidGenericParams<params, $>>>): Generic<parseValidGenericParams<params, $>, def, $>;
|
|
14
|
-
|
|
15
|
+
<const zero, const one, const rest extends array, r = Type<inferTypeRoot<[zero, one, ...rest], $>, $>>(_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 "@" ? [MetaSchema] : [validateTypeRoot<rest[0], $>] : []): r;
|
|
16
|
+
raw(def: unknown): BaseType<any, $>;
|
|
15
17
|
errors: typeof ArkErrors;
|
|
16
18
|
}
|
|
17
19
|
export declare class InternalTypeParser extends Callable<(...args: unknown[]) => BaseRoot | Generic, TypeParserAttachments> {
|
|
18
20
|
constructor($: InternalScope);
|
|
19
21
|
}
|
|
20
22
|
export type DeclarationParser<$> = <preinferred>() => {
|
|
21
|
-
type: <def>(def: validateDeclared<preinferred, def, $, bindThis<def>>) => Type<preinferred, $>;
|
|
23
|
+
type: <const def>(def: validateDeclared<preinferred, def, $, bindThis<def>>) => Type<preinferred, $>;
|
|
22
24
|
};
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
array(): Type<t[], $>;
|
|
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<predicate extends Predicate<distillOut<t>>>(predicate: predicate): Type<t extends MorphAst ? inferPredicate<this["tOut"], predicate> extends infer narrowed ? (In: this["tIn"]) => Out<narrowed> : never : inferPredicate<t, predicate>, $>;
|
|
41
|
-
equals<def>(def: validateTypeRoot<def, $>): this is Type<inferTypeRoot<def>, $>;
|
|
42
|
-
extract<def>(r: validateTypeRoot<def, $>): Type<Extract<t, inferTypeRoot<def, $>>, $>;
|
|
43
|
-
exclude<def>(r: validateTypeRoot<def, $>): Type<Exclude<t, inferTypeRoot<def, $>>, $>;
|
|
44
|
-
extends<def>(other: validateTypeRoot<def, $>): this is Type<inferTypeRoot<def>, $>;
|
|
45
|
-
overlaps<def>(r: validateTypeRoot<def, $>): boolean;
|
|
46
|
-
omit<const key extends arkKeyOf<t> = never>(this: validateStructuralOperand<"omit", this>, ...keys: array<key | type.cast<key>>): Type<{
|
|
47
|
-
[k in keyof t as Exclude<toArkKey<t, k>, key>]: t[k];
|
|
48
|
-
}, $>;
|
|
49
|
-
pick<const key extends arkKeyOf<t> = never>(this: validateStructuralOperand<"pick", this>, ...keys: array<key | type.cast<key>>): Type<{
|
|
50
|
-
[k in keyof t as Extract<toArkKey<t, k>, key>]: t[k];
|
|
51
|
-
}, $>;
|
|
52
|
-
required(this: validateStructuralOperand<"required", this>): Type<{
|
|
53
|
-
[k in keyof this["inferIn"]]-?: this["inferIn"][k];
|
|
54
|
-
}, $>;
|
|
55
|
-
partial(this: validateStructuralOperand<"partial", this>): Type<{
|
|
56
|
-
[k in keyof this["inferIn"]]?: this["inferIn"][k];
|
|
57
|
-
}, $>;
|
|
58
|
-
get<k1 extends arkKeyOf<t>>(k1: k1 | type.cast<k1>): Type<getArkKey<t, k1>, $>;
|
|
59
|
-
get<k1 extends arkKeyOf<t>, k2 extends arkKeyOf<getArkKey<t, k1>>>(k1: k1 | type.cast<k1>, k2: k2 | type.cast<k2>): Type<getArkKey<getArkKey<t, k1>, k2>, $>;
|
|
60
|
-
get<k1 extends arkKeyOf<t>, k2 extends arkKeyOf<getArkKey<t, k1>>, k3 extends arkKeyOf<getArkKey<getArkKey<t, k1>, k2>>>(k1: k1 | type.cast<k1>, k2: k2 | type.cast<k2>, k3: k3 | type.cast<k3>): Type<getArkKey<getArkKey<getArkKey<t, k1>, k2>, k3>, $>;
|
|
61
|
-
constrain<kind extends PrimitiveConstraintKind, const def extends NodeSchema<kind>>(kind: conform<kind, constraintKindOf<this["inferIn"]>>, def: def): Type<constrain<t, kind, def>, $>;
|
|
62
|
-
satisfying<predicate extends Predicate<distillIn<t>>>(predicate: predicate): Type<t extends MorphAst ? (In: inferPredicate<this["tIn"], predicate>) => Out<this["tOut"]> : inferPredicate<t, predicate>, $>;
|
|
63
|
-
divisibleBy<const schema extends DivisorSchema>(this: validateChainedConstraint<"divisor", this>, schema: schema): Type<constrain<t, "divisor", schema>, $>;
|
|
64
|
-
matching<const schema extends PatternSchema>(this: validateChainedConstraint<"pattern", this>, schema: schema): Type<constrain<t, "pattern", schema>, $>;
|
|
65
|
-
atLeast<const schema extends InclusiveNumericRangeSchema>(this: validateChainedConstraint<"min", this>, schema: schema): Type<constrain<t, "min", schema>, $>;
|
|
66
|
-
atMost<const schema extends InclusiveNumericRangeSchema>(this: validateChainedConstraint<"max", this>, schema: schema): Type<constrain<t, "max", schema>, $>;
|
|
67
|
-
moreThan<const schema extends ExclusiveNumericRangeSchema>(this: validateChainedConstraint<"min", this>, schema: schema): Type<constrain<t, "min", exclusivizeRangeSchema<schema>>, $>;
|
|
68
|
-
lessThan<const schema extends ExclusiveNumericRangeSchema>(this: validateChainedConstraint<"max", this>, schema: schema): Type<constrain<t, "max", exclusivizeRangeSchema<schema>>, $>;
|
|
69
|
-
atLeastLength<const schema extends InclusiveNumericRangeSchema>(this: validateChainedConstraint<"minLength", this>, schema: schema): Type<constrain<t, "minLength", schema>, $>;
|
|
70
|
-
atMostLength<const schema extends InclusiveNumericRangeSchema>(this: validateChainedConstraint<"maxLength", this>, schema: schema): Type<constrain<t, "maxLength", schema>, $>;
|
|
71
|
-
moreThanLength<const schema extends ExclusiveNumericRangeSchema>(this: validateChainedConstraint<"minLength", this>, schema: schema): Type<constrain<t, "minLength", exclusivizeRangeSchema<schema>>, $>;
|
|
72
|
-
lessThanLength<const schema extends ExclusiveNumericRangeSchema>(this: validateChainedConstraint<"maxLength", this>, schema: schema): Type<constrain<t, "maxLength", exclusivizeRangeSchema<schema>>, $>;
|
|
73
|
-
exactlyLength<const schema extends ExactLengthSchema>(this: validateChainedConstraint<"exactLength", this>, schema: schema): Type<constrain<t, "exactLength", schema>, $>;
|
|
74
|
-
atOrAfter<const schema extends InclusiveDateRangeSchema>(this: validateChainedConstraint<"after", this>, schema: schema): Type<constrain<t, "after", schema>, $>;
|
|
75
|
-
atOrBefore<const schema extends InclusiveDateRangeSchema>(this: validateChainedConstraint<"before", this>, schema: schema): Type<constrain<t, "before", schema>, $>;
|
|
76
|
-
laterThan<const schema extends ExclusiveDateRangeSchema>(this: validateChainedConstraint<"after", this>, schema: schema): Type<constrain<t, "after", exclusivizeRangeSchema<schema>>, $>;
|
|
77
|
-
earlierThan<const schema extends ExclusiveDateRangeSchema>(this: validateChainedConstraint<"before", this>, schema: schema): Type<constrain<t, "before", exclusivizeRangeSchema<schema>>, $>;
|
|
78
|
-
}
|
|
79
|
-
export interface Type<
|
|
80
|
-
/** @ts-expect-error allow instantiation assignment to the base type */
|
|
81
|
-
out t = unknown, $ = {}> extends _Type<t, $> {
|
|
25
|
+
export type DefinitionParser<$> = <def>(def: validateTypeRoot<def, $>) => def;
|
|
26
|
+
export type validateTypeRoot<def, $ = {}> = validateDefinition<def, $, bindThis<def>>;
|
|
27
|
+
export type inferTypeRoot<def, $> = inferDefinition<def, $, bindThis<def>>;
|
|
28
|
+
export type validateAmbient<def> = validateTypeRoot<def, {}>;
|
|
29
|
+
export type inferAmbient<def> = inferTypeRoot<def, {}>;
|
|
30
|
+
export type Type<t = unknown, $ = {}> = instantiateType<t, $>;
|
|
31
|
+
export declare namespace Type {
|
|
32
|
+
type Any<t = any> = BaseType<t, any>;
|
|
82
33
|
}
|
|
83
34
|
export type TypeConstructor<t = unknown, $ = {}> = new (def: unknown, $: Scope<$>) => Type<t, $>;
|
|
84
|
-
export type AnyType<out t = unknown> = Type<t, any>;
|
|
85
35
|
export declare const Type: TypeConstructor;
|
|
86
|
-
export type DefinitionParser<$> = <def>(def: validateTypeRoot<def, $>) => def;
|
|
87
|
-
export type validateTypeRoot<def, $ = {}> = validateDefinition<def, $, bindThis<def>>;
|
|
88
|
-
export type inferTypeRoot<def, $ = {}> = inferDefinition<def, $, bindThis<def>>;
|
|
89
|
-
export {};
|
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-beta.
|
|
4
|
+
"version": "2.0.0-beta.4",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": {
|
|
7
7
|
"name": "David Blass",
|
|
@@ -25,8 +25,8 @@
|
|
|
25
25
|
"out"
|
|
26
26
|
],
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"@ark/util": "0.1
|
|
29
|
-
"@ark/schema": "0.
|
|
28
|
+
"@ark/util": "0.2.1",
|
|
29
|
+
"@ark/schema": "0.3.1"
|
|
30
30
|
},
|
|
31
31
|
"publishConfig": {
|
|
32
32
|
"access": "public"
|
package/out/match.d.ts
DELETED
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
import type { Narrowed, distillOut, inferIntersection } from "@ark/schema";
|
|
2
|
-
import type { ErrorMessage, isDisjoint, numericStringKeyOf, override, propValueOf, unionToTuple } from "@ark/util";
|
|
3
|
-
import type { Scope } from "./scope.js";
|
|
4
|
-
import type { inferTypeRoot, validateTypeRoot } from "./type.js";
|
|
5
|
-
type MatchParserContext = {
|
|
6
|
-
thens: readonly ((In: unknown) => unknown)[];
|
|
7
|
-
$: unknown;
|
|
8
|
-
exhaustiveOver: unknown;
|
|
9
|
-
};
|
|
10
|
-
export type MatchParser<$> = CaseMatchParser<{
|
|
11
|
-
thens: [];
|
|
12
|
-
exhaustiveOver: unknown;
|
|
13
|
-
$: $;
|
|
14
|
-
}> & {
|
|
15
|
-
(): ChainableMatchParser<{
|
|
16
|
-
thens: [];
|
|
17
|
-
exhaustiveOver: unknown;
|
|
18
|
-
$: $;
|
|
19
|
-
}>;
|
|
20
|
-
only: <inputs>() => ChainableMatchParser<{
|
|
21
|
-
thens: [];
|
|
22
|
-
exhaustiveOver: inputs;
|
|
23
|
-
$: $;
|
|
24
|
-
}>;
|
|
25
|
-
};
|
|
26
|
-
type matcherInputs<ctx extends MatchParserContext> = Parameters<ctx["thens"][number]>[0];
|
|
27
|
-
type getHandledBranches<ctx extends MatchParserContext> = Exclude<matcherInputs<ctx>, Narrowed>;
|
|
28
|
-
type getUnhandledBranches<ctx extends MatchParserContext> = distillOut<Exclude<ctx["exhaustiveOver"], getHandledBranches<ctx>>>;
|
|
29
|
-
type addBranches<ctx extends MatchParserContext, branches extends unknown[]> = override<ctx, {
|
|
30
|
-
thens: [...ctx["thens"], ...branches];
|
|
31
|
-
}>;
|
|
32
|
-
type validateWhenDefinition<def, ctx extends MatchParserContext> = def extends validateTypeRoot<def, ctx["$"]> ? inferMatchBranch<def, ctx> extends getHandledBranches<ctx> ? ErrorMessage<"This branch is redundant and will never be reached"> : def : validateTypeRoot<def, ctx["$"]>;
|
|
33
|
-
type inferMatchBranch<def, ctx extends MatchParserContext> = distillOut<inferIntersection<getUnhandledBranches<ctx>, inferTypeRoot<def, ctx["$"]>>>;
|
|
34
|
-
export type ChainableMatchParser<ctx extends MatchParserContext> = {
|
|
35
|
-
when: <def, ret>(when: validateWhenDefinition<def, ctx>, then: (In: inferMatchBranch<def, ctx>) => ret) => ChainableMatchParser<addBranches<ctx, [(In: inferMatchBranch<def, ctx>) => ret]>>;
|
|
36
|
-
cases: CaseMatchParser<ctx>;
|
|
37
|
-
orThrow: () => finalizeMatchParser<addBranches<ctx, [(In: getHandledBranches<ctx>) => never]>>;
|
|
38
|
-
default: MatchParserDefaultInvocation<ctx>;
|
|
39
|
-
finalize: (this: getUnhandledBranches<ctx> extends never ? ChainableMatchParser<ctx> : ErrorMessage<"Cannot manually finalize a non-exhaustive matcher: consider adding a `.default` case, using one of the `.orX` methods, or using `match.only<T>`">) => finalizeMatchParser<ctx>;
|
|
40
|
-
};
|
|
41
|
-
type MatchParserDefaultInvocation<ctx extends MatchParserContext> = {
|
|
42
|
-
<f extends (In: getUnhandledBranches<ctx>) => unknown>(f: f): finalizeWithDefault<ctx, ReturnType<f>>;
|
|
43
|
-
<const value>(value: value): finalizeWithDefault<ctx, value>;
|
|
44
|
-
};
|
|
45
|
-
type validateCases<cases, ctx extends MatchParserContext> = {
|
|
46
|
-
[def in keyof cases | keyof ctx["$"] | "default"]?: def extends "default" ? (In: getUnhandledBranches<ctx>) => unknown : def extends validateWhenDefinition<def, ctx> ? (In: inferMatchBranch<def, ctx>) => unknown : validateWhenDefinition<def, ctx>;
|
|
47
|
-
};
|
|
48
|
-
type errorCases<cases, ctx extends MatchParserContext> = {
|
|
49
|
-
[def in keyof cases]?: def extends "default" ? (In: getUnhandledBranches<ctx>) => unknown : def extends validateWhenDefinition<def, ctx> ? (In: inferMatchBranch<def, ctx>) => unknown : validateWhenDefinition<def, ctx>;
|
|
50
|
-
} & {
|
|
51
|
-
[k in Exclude<keyof ctx["$"], keyof cases>]?: (In: distillOut<inferIntersection<getUnhandledBranches<ctx>, ctx["$"][k]>>) => unknown;
|
|
52
|
-
} & {
|
|
53
|
-
default?: (In: getUnhandledBranches<ctx>) => unknown;
|
|
54
|
-
};
|
|
55
|
-
export type CaseMatchParser<ctx extends MatchParserContext> = <cases>(def: cases extends validateCases<cases, ctx> ? cases : errorCases<cases, ctx>) => cases extends {
|
|
56
|
-
default: (...args: never[]) => infer defaultReturn;
|
|
57
|
-
} ? finalizeWithDefault<addBranches<ctx, unionToTuple<cases[Exclude<keyof cases, "default">]>>, defaultReturn> : ChainableMatchParser<addBranches<ctx, unionToTuple<propValueOf<cases>>>>;
|
|
58
|
-
type finalizeWithDefault<ctx extends MatchParserContext, defaultReturn> = finalizeMatchParser<addBranches<ctx, [(_: getUnhandledBranches<ctx>) => defaultReturn]>>;
|
|
59
|
-
type finalizeMatchParser<ctx extends MatchParserContext> = MatchInvocation<{
|
|
60
|
-
thens: ctx["thens"];
|
|
61
|
-
initialInputs: ctx["exhaustiveOver"];
|
|
62
|
-
}>;
|
|
63
|
-
type MatchInvocationContext = {
|
|
64
|
-
thens: readonly ((...args: never[]) => unknown)[];
|
|
65
|
-
initialInputs: unknown;
|
|
66
|
-
};
|
|
67
|
-
export type MatchInvocation<ctx extends MatchInvocationContext> = <const data extends ctx["initialInputs"]>(data: data) => {
|
|
68
|
-
[i in numericStringKeyOf<ctx["thens"]>]: isDisjoint<data, Parameters<ctx["thens"][i]>[0]> extends true ? never : ReturnType<ctx["thens"][i]>;
|
|
69
|
-
}[numericStringKeyOf<ctx["thens"]>];
|
|
70
|
-
export declare const createMatchParser: <$>($: Scope) => MatchParser<$>;
|
|
71
|
-
export {};
|
package/out/match.js
DELETED
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
export const createMatchParser = ($) => (() => { }).bind($);
|
|
2
|
-
// const matchParser = (isRestricted: boolean) => {
|
|
3
|
-
// const handledCases: { when: RawRoot; then: Morph }[] = []
|
|
4
|
-
// let defaultCase: ((x: unknown) => unknown) | null = null
|
|
5
|
-
// const parser = {
|
|
6
|
-
// when: (when: unknown, then: Morph) => {
|
|
7
|
-
// handledCases.push({ when: $.parseRoot(when, {}), then })
|
|
8
|
-
// return parser
|
|
9
|
-
// },
|
|
10
|
-
// finalize: () => {
|
|
11
|
-
// // TODO: exhaustiveness checking
|
|
12
|
-
// const branches = handledCases.flatMap(({ when, then }) => {
|
|
13
|
-
// if (when.kind === "union") {
|
|
14
|
-
// return when.branches.map((branch) => ({
|
|
15
|
-
// in: branch,
|
|
16
|
-
// morph: then
|
|
17
|
-
// }))
|
|
18
|
-
// }
|
|
19
|
-
// if (when.kind === "morph") {
|
|
20
|
-
// return [{ in: when, morph: [when.morph, then] }]
|
|
21
|
-
// }
|
|
22
|
-
// return [{ in: when, morph: then }]
|
|
23
|
-
// })
|
|
24
|
-
// if (defaultCase) {
|
|
25
|
-
// branches.push({ in: keywordNodes.unknown, morph: defaultCase })
|
|
26
|
-
// }
|
|
27
|
-
// const matchers = $.node("union", {
|
|
28
|
-
// branches,
|
|
29
|
-
// ordered: true
|
|
30
|
-
// })
|
|
31
|
-
// return matchers.assert
|
|
32
|
-
// },
|
|
33
|
-
// orThrow: () => {
|
|
34
|
-
// // implicitly finalize, we don't need to do anything else because we throw either way
|
|
35
|
-
// return parser.finalize()
|
|
36
|
-
// },
|
|
37
|
-
// default: (x: unknown) => {
|
|
38
|
-
// if (x instanceof Function) {
|
|
39
|
-
// defaultCase = x as never
|
|
40
|
-
// } else {
|
|
41
|
-
// defaultCase = () => x
|
|
42
|
-
// }
|
|
43
|
-
// return parser.finalize()
|
|
44
|
-
// }
|
|
45
|
-
// }
|
|
46
|
-
// return parser
|
|
47
|
-
// }
|
|
48
|
-
// return Object.assign(() => matchParser(false), {
|
|
49
|
-
// only: () => matchParser(true)
|
|
50
|
-
// }) as never
|