arktype 2.0.0-dev.0 → 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/generic.ts
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type GenericNodeInstantiation,
|
|
3
|
+
type GenericProps,
|
|
4
|
+
type SchemaScope,
|
|
5
|
+
arkKind
|
|
6
|
+
} from "@arktype/schema"
|
|
7
|
+
import { Callable, type conform } from "@arktype/util"
|
|
8
|
+
import type { inferDefinition } from "./parser/definition.js"
|
|
9
|
+
import type {
|
|
10
|
+
GenericParamsParseError,
|
|
11
|
+
parseGenericParams
|
|
12
|
+
} from "./parser/generic.js"
|
|
13
|
+
import type { Type, inferTypeRoot, validateTypeRoot } from "./type.js"
|
|
14
|
+
export type validateParameterString<params extends string> =
|
|
15
|
+
parseGenericParams<params> extends GenericParamsParseError<infer message> ?
|
|
16
|
+
message
|
|
17
|
+
: params
|
|
18
|
+
|
|
19
|
+
export type GenericTypeInstantiation<
|
|
20
|
+
params extends string[] = string[],
|
|
21
|
+
def = any,
|
|
22
|
+
$ = any
|
|
23
|
+
> = <args>(
|
|
24
|
+
...args: conform<
|
|
25
|
+
args,
|
|
26
|
+
{
|
|
27
|
+
[i in keyof params]: validateTypeRoot<args[i & keyof args], $>
|
|
28
|
+
}
|
|
29
|
+
>
|
|
30
|
+
) => Type<inferDefinition<def, $, bindGenericInstantiation<params, $, args>>, $>
|
|
31
|
+
|
|
32
|
+
export type GenericInstantiation<
|
|
33
|
+
params extends string[] = string[],
|
|
34
|
+
def = any,
|
|
35
|
+
$ = any
|
|
36
|
+
> = GenericTypeInstantiation<params, def, $> &
|
|
37
|
+
GenericNodeInstantiation<params, def, $>
|
|
38
|
+
|
|
39
|
+
// TODO: Fix external reference (i.e. if this is attached to a scope, then args are defined using it)
|
|
40
|
+
type bindGenericInstantiation<params extends string[], $, args> = {
|
|
41
|
+
[i in keyof params & `${number}` as params[i]]: inferTypeRoot<
|
|
42
|
+
args[i & keyof args],
|
|
43
|
+
$
|
|
44
|
+
>
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export class Generic<params extends string[] = string[], def = unknown, $ = any>
|
|
48
|
+
extends Callable<GenericInstantiation<params, def, $>>
|
|
49
|
+
implements GenericProps<params, def, $>
|
|
50
|
+
{
|
|
51
|
+
readonly [arkKind] = "generic"
|
|
52
|
+
|
|
53
|
+
constructor(
|
|
54
|
+
public params: params,
|
|
55
|
+
public def: def,
|
|
56
|
+
// TODO: should be Scope<$>, but breaks inference
|
|
57
|
+
public $: SchemaScope<$>
|
|
58
|
+
) {
|
|
59
|
+
super((...args: unknown[]) => {
|
|
60
|
+
// const argNodes = flatMorph(params, (i, param: string) => [
|
|
61
|
+
// param,
|
|
62
|
+
// $.parseRoot(args[i])
|
|
63
|
+
// ])
|
|
64
|
+
// { args: argNodes }
|
|
65
|
+
args
|
|
66
|
+
return $.parseRoot(def) as never
|
|
67
|
+
})
|
|
68
|
+
}
|
|
69
|
+
}
|
package/main.ts
CHANGED
|
@@ -1,18 +1,6 @@
|
|
|
1
|
-
export {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
} from "
|
|
5
|
-
export
|
|
6
|
-
export {
|
|
7
|
-
ark,
|
|
8
|
-
arktypes,
|
|
9
|
-
declare,
|
|
10
|
-
define,
|
|
11
|
-
match,
|
|
12
|
-
scope,
|
|
13
|
-
type,
|
|
14
|
-
type Ark
|
|
15
|
-
} from "./ark.js"
|
|
16
|
-
export type { Module, Scope } from "./scope.js"
|
|
17
|
-
export { Type } from "./type.js"
|
|
18
|
-
export type { inferTypeRoot, validateTypeRoot } from "./type.js"
|
|
1
|
+
export { ArkError, ArkErrors, ArkTypeError } from "@arktype/schema"
|
|
2
|
+
export type { Ark, ArkConfig, Out } from "@arktype/schema"
|
|
3
|
+
export { ambient, ark, declare, define, match, type } from "./ark.js"
|
|
4
|
+
export { Module } from "./module.js"
|
|
5
|
+
export { scope, type Scope } from "./scope.js"
|
|
6
|
+
export { Type, type inferTypeRoot, type validateTypeRoot } from "./type.js"
|
package/match.ts
CHANGED
|
@@ -1,116 +1,218 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { Narrowed, distillOut, inferIntersection } from "@arktype/schema"
|
|
2
2
|
import type {
|
|
3
|
-
|
|
3
|
+
ErrorMessage,
|
|
4
4
|
isDisjoint,
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
5
|
+
numericStringKeyOf,
|
|
6
|
+
override,
|
|
7
|
+
propValueOf,
|
|
8
|
+
unionToTuple
|
|
9
9
|
} from "@arktype/util"
|
|
10
10
|
import type { Scope } from "./scope.js"
|
|
11
|
-
import {
|
|
11
|
+
import type { inferTypeRoot, validateTypeRoot } from "./type.js"
|
|
12
12
|
|
|
13
|
-
type
|
|
14
|
-
|
|
15
|
-
outConstraint: unknown
|
|
16
|
-
thens: readonly Fn[]
|
|
13
|
+
type MatchParserContext = {
|
|
14
|
+
thens: readonly ((In: unknown) => unknown)[]
|
|
17
15
|
$: unknown
|
|
16
|
+
exhaustiveOver: unknown
|
|
18
17
|
}
|
|
19
18
|
|
|
20
|
-
type
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
:
|
|
19
|
+
export type MatchParser<$> = CaseMatchParser<{
|
|
20
|
+
thens: []
|
|
21
|
+
// "match()" is the same as "match.only<unknown>()"
|
|
22
|
+
exhaustiveOver: unknown
|
|
23
|
+
$: $
|
|
24
|
+
}> & {
|
|
25
|
+
(): ChainableMatchParser<{
|
|
26
|
+
thens: []
|
|
27
|
+
// "match()" is the same as "match.only<unknown>()"
|
|
28
|
+
exhaustiveOver: unknown
|
|
29
|
+
$: $
|
|
30
|
+
}>
|
|
31
|
+
only: <inputs>() => ChainableMatchParser<{
|
|
32
|
+
thens: []
|
|
33
|
+
exhaustiveOver: inputs
|
|
34
|
+
$: $
|
|
35
|
+
}>
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
type matcherInputs<ctx extends MatchParserContext> = Parameters<
|
|
39
|
+
ctx["thens"][number]
|
|
40
|
+
>[0]
|
|
41
|
+
|
|
42
|
+
type getHandledBranches<ctx extends MatchParserContext> = Exclude<
|
|
43
|
+
matcherInputs<ctx>,
|
|
44
|
+
// TODO: add other anon
|
|
45
|
+
Narrowed
|
|
46
|
+
>
|
|
47
|
+
|
|
48
|
+
type getUnhandledBranches<ctx extends MatchParserContext> = distillOut<
|
|
49
|
+
Exclude<ctx["exhaustiveOver"], getHandledBranches<ctx>>
|
|
50
|
+
>
|
|
51
|
+
|
|
52
|
+
type addBranches<
|
|
53
|
+
ctx extends MatchParserContext,
|
|
54
|
+
branches extends unknown[]
|
|
55
|
+
> = override<ctx, { thens: [...ctx["thens"], ...branches] }>
|
|
56
|
+
|
|
57
|
+
type validateWhenDefinition<def, ctx extends MatchParserContext> =
|
|
58
|
+
def extends validateTypeRoot<def, ctx["$"]> ?
|
|
59
|
+
inferMatchBranch<def, ctx> extends getHandledBranches<ctx> ?
|
|
60
|
+
ErrorMessage<"This branch is redundant and will never be reached">
|
|
61
|
+
: def
|
|
62
|
+
: validateTypeRoot<def, ctx["$"]>
|
|
63
|
+
|
|
64
|
+
// infer the types handled by a match branch, which is identical to `inferTypeRoot` while properly
|
|
65
|
+
// excluding cases that are already handled by other branches
|
|
66
|
+
type inferMatchBranch<def, ctx extends MatchParserContext> = distillOut<
|
|
67
|
+
inferIntersection<getUnhandledBranches<ctx>, inferTypeRoot<def, ctx["$"]>>
|
|
68
|
+
>
|
|
69
|
+
|
|
70
|
+
export type ChainableMatchParser<ctx extends MatchParserContext> = {
|
|
71
|
+
// chainable methods
|
|
72
|
+
when: <def, ret>(
|
|
73
|
+
when: validateWhenDefinition<def, ctx>,
|
|
74
|
+
then: (In: inferMatchBranch<def, ctx>) => ret
|
|
75
|
+
) => ChainableMatchParser<
|
|
76
|
+
addBranches<ctx, [(In: inferMatchBranch<def, ctx>) => ret]>
|
|
77
|
+
>
|
|
78
|
+
cases: CaseMatchParser<ctx>
|
|
79
|
+
|
|
80
|
+
// finalizing methods
|
|
81
|
+
orThrow: () => finalizeMatchParser<
|
|
82
|
+
addBranches<ctx, [(In: getHandledBranches<ctx>) => never]>
|
|
83
|
+
>
|
|
84
|
+
default: MatchParserDefaultInvocation<ctx>
|
|
85
|
+
finalize: (
|
|
86
|
+
this: getUnhandledBranches<ctx> extends never ? ChainableMatchParser<ctx>
|
|
87
|
+
: ErrorMessage<"Cannot manually finalize a non-exhaustive matcher: consider adding a `.default` case, using one of the `.orX` methods, or using `match.only<T>`">
|
|
88
|
+
) => finalizeMatchParser<ctx>
|
|
28
89
|
}
|
|
29
90
|
|
|
30
|
-
type
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
91
|
+
type MatchParserDefaultInvocation<ctx extends MatchParserContext> = {
|
|
92
|
+
<f extends (In: getUnhandledBranches<ctx>) => unknown>(
|
|
93
|
+
f: f
|
|
94
|
+
): finalizeWithDefault<ctx, ReturnType<f>>
|
|
95
|
+
<const value>(value: value): finalizeWithDefault<ctx, value>
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
type validateCases<cases, ctx extends MatchParserContext> = {
|
|
99
|
+
[def in keyof cases | keyof ctx["$"] | "default"]?: def extends "default" ?
|
|
100
|
+
(In: getUnhandledBranches<ctx>) => unknown
|
|
101
|
+
: def extends validateWhenDefinition<def, ctx> ?
|
|
102
|
+
(In: inferMatchBranch<def, ctx>) => unknown
|
|
103
|
+
: validateWhenDefinition<def, ctx>
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
type errorCases<cases, ctx extends MatchParserContext> = {
|
|
107
|
+
[def in keyof cases]?: def extends "default" ?
|
|
108
|
+
(In: getUnhandledBranches<ctx>) => unknown
|
|
109
|
+
: def extends validateWhenDefinition<def, ctx> ?
|
|
110
|
+
(In: inferMatchBranch<def, ctx>) => unknown
|
|
111
|
+
: validateWhenDefinition<def, ctx>
|
|
38
112
|
} & {
|
|
39
113
|
[k in Exclude<keyof ctx["$"], keyof cases>]?: (
|
|
40
|
-
In: ctx
|
|
41
|
-
) =>
|
|
114
|
+
In: distillOut<inferIntersection<getUnhandledBranches<ctx>, ctx["$"][k]>>
|
|
115
|
+
) => unknown
|
|
42
116
|
} & {
|
|
43
|
-
default?: (In: ctx
|
|
117
|
+
default?: (In: getUnhandledBranches<ctx>) => unknown
|
|
44
118
|
}
|
|
45
119
|
|
|
46
|
-
export type CaseMatchParser<ctx extends
|
|
47
|
-
<cases>
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
replaceKey<ctx, "thens", [...ctx["thens"], ...unionToTuple<valueOf<cases>>]>
|
|
120
|
+
export type CaseMatchParser<ctx extends MatchParserContext> = <cases>(
|
|
121
|
+
def: cases extends validateCases<cases, ctx> ? cases : errorCases<cases, ctx>
|
|
122
|
+
) => cases extends { default: (...args: never[]) => infer defaultReturn } ?
|
|
123
|
+
finalizeWithDefault<
|
|
124
|
+
addBranches<ctx, unionToTuple<cases[Exclude<keyof cases, "default">]>>,
|
|
125
|
+
defaultReturn
|
|
53
126
|
>
|
|
54
|
-
|
|
127
|
+
: ChainableMatchParser<addBranches<ctx, unionToTuple<propValueOf<cases>>>>
|
|
55
128
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
<In = unknown, Out = unknown>(): ChainableMatchParser<{
|
|
63
|
-
inConstraint: In
|
|
64
|
-
outConstraint: Out
|
|
65
|
-
thens: []
|
|
66
|
-
$: $
|
|
67
|
-
}>
|
|
68
|
-
}
|
|
129
|
+
type finalizeWithDefault<
|
|
130
|
+
ctx extends MatchParserContext,
|
|
131
|
+
defaultReturn
|
|
132
|
+
> = finalizeMatchParser<
|
|
133
|
+
addBranches<ctx, [(_: getUnhandledBranches<ctx>) => defaultReturn]>
|
|
134
|
+
>
|
|
69
135
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
) => ctx["outConstraint"]
|
|
75
|
-
>(
|
|
76
|
-
when: validateTypeRoot<def, ctx["$"]>,
|
|
77
|
-
then: then
|
|
78
|
-
) => ChainableMatchParser<replaceKey<ctx, "thens", [...ctx["thens"], then]>>
|
|
136
|
+
type finalizeMatchParser<ctx extends MatchParserContext> = MatchInvocation<{
|
|
137
|
+
thens: ctx["thens"]
|
|
138
|
+
initialInputs: ctx["exhaustiveOver"]
|
|
139
|
+
}>
|
|
79
140
|
|
|
80
|
-
|
|
81
|
-
|
|
141
|
+
type MatchInvocationContext = {
|
|
142
|
+
thens: readonly ((...args: never[]) => unknown)[]
|
|
143
|
+
initialInputs: unknown
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export type MatchInvocation<ctx extends MatchInvocationContext> = <
|
|
147
|
+
data extends ctx["initialInputs"]
|
|
82
148
|
>(
|
|
83
149
|
data: data
|
|
84
150
|
) => {
|
|
85
|
-
[i in
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
>
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
}
|
|
115
|
-
|
|
151
|
+
[i in numericStringKeyOf<ctx["thens"]>]: isDisjoint<
|
|
152
|
+
data,
|
|
153
|
+
Parameters<ctx["thens"][i]>[0]
|
|
154
|
+
> extends true ?
|
|
155
|
+
never
|
|
156
|
+
: ReturnType<ctx["thens"][i]>
|
|
157
|
+
}[numericStringKeyOf<ctx["thens"]>]
|
|
158
|
+
|
|
159
|
+
export const createMatchParser = <$>($: Scope): MatchParser<$> => {
|
|
160
|
+
return (() => {}).bind($) as never
|
|
161
|
+
// const matchParser = (isRestricted: boolean) => {
|
|
162
|
+
// const handledCases: { when: RawSchema; then: Morph }[] = []
|
|
163
|
+
// let defaultCase: ((x: unknown) => unknown) | null = null
|
|
164
|
+
|
|
165
|
+
// const parser = {
|
|
166
|
+
// when: (when: unknown, then: Morph) => {
|
|
167
|
+
// handledCases.push({ when: $.parseRoot(when, {}), then })
|
|
168
|
+
|
|
169
|
+
// return parser
|
|
170
|
+
// },
|
|
171
|
+
|
|
172
|
+
// finalize: () => {
|
|
173
|
+
// // TODO: exhaustiveness checking
|
|
174
|
+
// const branches = handledCases.flatMap(({ when, then }) => {
|
|
175
|
+
// if (when.kind === "union") {
|
|
176
|
+
// return when.branches.map((branch) => ({
|
|
177
|
+
// from: branch,
|
|
178
|
+
// morph: then
|
|
179
|
+
// }))
|
|
180
|
+
// }
|
|
181
|
+
// if (when.kind === "morph") {
|
|
182
|
+
// return [{ from: when, morph: [when.morph, then] }]
|
|
183
|
+
// }
|
|
184
|
+
// return [{ from: when, morph: then }]
|
|
185
|
+
// })
|
|
186
|
+
// if (defaultCase) {
|
|
187
|
+
// branches.push({ from: keywordNodes.unknown, morph: defaultCase })
|
|
188
|
+
// }
|
|
189
|
+
// const matchers = $.node("union", {
|
|
190
|
+
// branches,
|
|
191
|
+
// ordered: true
|
|
192
|
+
// })
|
|
193
|
+
// return matchers.assert
|
|
194
|
+
// },
|
|
195
|
+
|
|
196
|
+
// orThrow: () => {
|
|
197
|
+
// // implicitly finalize, we don't need to do anything else because we throw either way
|
|
198
|
+
// return parser.finalize()
|
|
199
|
+
// },
|
|
200
|
+
|
|
201
|
+
// default: (x: unknown) => {
|
|
202
|
+
// if (x instanceof Function) {
|
|
203
|
+
// defaultCase = x as never
|
|
204
|
+
// } else {
|
|
205
|
+
// defaultCase = () => x
|
|
206
|
+
// }
|
|
207
|
+
|
|
208
|
+
// return parser.finalize()
|
|
209
|
+
// }
|
|
210
|
+
// }
|
|
211
|
+
|
|
212
|
+
// return parser
|
|
213
|
+
// }
|
|
214
|
+
|
|
215
|
+
// return Object.assign(() => matchParser(false), {
|
|
216
|
+
// only: () => matchParser(true)
|
|
217
|
+
// }) as never
|
|
116
218
|
}
|
package/module.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { PreparsedNodeResolution, arkKind } from "@arktype/schema"
|
|
2
|
+
import { DynamicBase, type isAnyOrNever } from "@arktype/util"
|
|
3
|
+
import type { Type } from "./type.js"
|
|
4
|
+
|
|
5
|
+
type exportScope<$> = {
|
|
6
|
+
[k in keyof $]: $[k] extends PreparsedNodeResolution ?
|
|
7
|
+
isAnyOrNever<$[k]> extends true ?
|
|
8
|
+
Type<$[k], $>
|
|
9
|
+
: $[k]
|
|
10
|
+
: Type<$[k], $>
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export class Module<$ = any> extends DynamicBase<exportScope<$>> {
|
|
14
|
+
declare readonly [arkKind]: "module"
|
|
15
|
+
}
|
package/out/ark.d.ts
CHANGED
|
@@ -1,29 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { type Ark, type ArkErrors, type inferred } from "@arktype/schema";
|
|
2
|
+
import type { Generic } from "./generic.js";
|
|
2
3
|
import type { MatchParser } from "./match.js";
|
|
3
|
-
import {
|
|
4
|
-
import
|
|
5
|
-
|
|
6
|
-
* explicitly typing them can improve responsiveness */
|
|
7
|
-
export type RootScope<exports> = Scope<{
|
|
8
|
-
exports: exports;
|
|
9
|
-
locals: {};
|
|
10
|
-
ambient: {};
|
|
11
|
-
}>;
|
|
12
|
-
export type ArkResolutions = {
|
|
13
|
-
exports: Ark;
|
|
14
|
-
locals: {};
|
|
15
|
-
ambient: Ark;
|
|
16
|
-
};
|
|
17
|
-
export type ParsingResolutions = {
|
|
18
|
-
exports: Parsing.infer;
|
|
19
|
-
locals: {};
|
|
20
|
-
ambient: {};
|
|
21
|
-
};
|
|
22
|
-
export type TsGenericsResolutions<$ = Ark> = {
|
|
23
|
-
exports: TsGenericsExports<$>;
|
|
24
|
-
locals: {};
|
|
25
|
-
ambient: {};
|
|
26
|
-
};
|
|
4
|
+
import type { Module } from "./module.js";
|
|
5
|
+
import { type Scope } from "./scope.js";
|
|
6
|
+
import type { DeclarationParser, DefinitionParser, TypeParser } from "./type.js";
|
|
27
7
|
type TsGenericsExports<$ = Ark> = {
|
|
28
8
|
Record: Generic<[
|
|
29
9
|
"K",
|
|
@@ -32,21 +12,17 @@ type TsGenericsExports<$ = Ark> = {
|
|
|
32
12
|
"[K]": "V";
|
|
33
13
|
}, $>;
|
|
34
14
|
};
|
|
35
|
-
export declare const tsGenerics: Module<
|
|
36
|
-
export declare const
|
|
37
|
-
export declare const
|
|
38
|
-
export
|
|
39
|
-
parse: Module<ParsingResolutions>;
|
|
40
|
-
}
|
|
41
|
-
export declare const scope: ScopeParser<{}, Ark>;
|
|
42
|
-
export declare const type: TypeParser<Ark>;
|
|
43
|
-
export declare const match: MatchParser<Ark>;
|
|
15
|
+
export declare const tsGenerics: Module<TsGenericsExports>;
|
|
16
|
+
export declare const ambient: Scope<Ark>;
|
|
17
|
+
export declare const ark: Module<Ark>;
|
|
18
|
+
export declare const type: TypeParser<{}>;
|
|
44
19
|
export declare namespace type {
|
|
45
|
-
type cast<
|
|
46
|
-
[inferred]?:
|
|
20
|
+
type cast<t> = {
|
|
21
|
+
[inferred]?: t;
|
|
47
22
|
};
|
|
23
|
+
type errors = ArkErrors;
|
|
48
24
|
}
|
|
49
|
-
export declare const
|
|
50
|
-
export declare const
|
|
25
|
+
export declare const match: MatchParser<{}>;
|
|
26
|
+
export declare const define: DefinitionParser<{}>;
|
|
27
|
+
export declare const declare: DeclarationParser<{}>;
|
|
51
28
|
export {};
|
|
52
|
-
//# sourceMappingURL=ark.d.ts.map
|
package/out/ark.js
CHANGED
|
@@ -1,19 +1,10 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { keywordNodes } from "@arktype/schema";
|
|
2
|
+
import { RawScope, scope } from "./scope.js";
|
|
3
3
|
export const tsGenerics = {};
|
|
4
|
-
export const
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
}).toAmbient();
|
|
12
|
-
export const arktypes = ark.export();
|
|
13
|
-
export const scope = ark.scope;
|
|
14
|
-
export const type = ark.type;
|
|
15
|
-
// TODO: cast needed?
|
|
16
|
-
export const match = ark.match;
|
|
17
|
-
export const define = ark.define;
|
|
18
|
-
export const declare = ark.declare;
|
|
19
|
-
//# sourceMappingURL=ark.js.map
|
|
4
|
+
export const ambient = scope(keywordNodes);
|
|
5
|
+
RawScope.ambient = ambient.raw;
|
|
6
|
+
export const ark = ambient.export();
|
|
7
|
+
export const type = ambient.type;
|
|
8
|
+
export const match = ambient.match;
|
|
9
|
+
export const define = ambient.define;
|
|
10
|
+
export const declare = ambient.declare;
|
package/out/ark.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ark.js","sourceRoot":"","sources":["../ark.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"ark.js","sourceRoot":"","sources":["../ark.ts"],"names":[],"mappings":"AAAA,OAAO,EAIN,YAAY,EACZ,MAAM,iBAAiB,CAAA;AAIxB,OAAO,EAAE,QAAQ,EAAc,KAAK,EAAE,MAAM,YAAY,CAAA;AAexD,MAAM,CAAC,MAAM,UAAU,GAAG,EAA+B,CAAA;AAEzD,MAAM,CAAC,MAAM,OAAO,GAAe,KAAK,CAAC,YAAY,CAAU,CAAA;AAE/D,QAAQ,CAAC,OAAO,GAAG,OAAO,CAAC,GAAG,CAAA;AAE9B,MAAM,CAAC,MAAM,GAAG,GAAgB,OAAO,CAAC,MAAM,EAAE,CAAA;AAEhD,MAAM,CAAC,MAAM,IAAI,GAAmB,OAAO,CAAC,IAAa,CAAA;AAUzD,MAAM,CAAC,MAAM,KAAK,GAAoB,OAAO,CAAC,KAAc,CAAA;AAE5D,MAAM,CAAC,MAAM,MAAM,GAAyB,OAAO,CAAC,MAAe,CAAA;AAEnE,MAAM,CAAC,MAAM,OAAO,GAA0B,OAAO,CAAC,OAAgB,CAAA"}
|
package/out/generic.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { type GenericNodeInstantiation, type GenericProps, type SchemaScope, arkKind } from "@arktype/schema";
|
|
2
|
+
import { Callable, type conform } from "@arktype/util";
|
|
3
|
+
import type { inferDefinition } from "./parser/definition.js";
|
|
4
|
+
import type { GenericParamsParseError, parseGenericParams } from "./parser/generic.js";
|
|
5
|
+
import type { Type, inferTypeRoot, validateTypeRoot } from "./type.js";
|
|
6
|
+
export type validateParameterString<params extends string> = parseGenericParams<params> extends GenericParamsParseError<infer message> ? message : params;
|
|
7
|
+
export type GenericTypeInstantiation<params extends string[] = string[], def = any, $ = any> = <args>(...args: conform<args, {
|
|
8
|
+
[i in keyof params]: validateTypeRoot<args[i & keyof args], $>;
|
|
9
|
+
}>) => Type<inferDefinition<def, $, bindGenericInstantiation<params, $, args>>, $>;
|
|
10
|
+
export type GenericInstantiation<params extends string[] = string[], def = any, $ = any> = GenericTypeInstantiation<params, def, $> & GenericNodeInstantiation<params, def, $>;
|
|
11
|
+
type bindGenericInstantiation<params extends string[], $, args> = {
|
|
12
|
+
[i in keyof params & `${number}` as params[i]]: inferTypeRoot<args[i & keyof args], $>;
|
|
13
|
+
};
|
|
14
|
+
export declare class Generic<params extends string[] = string[], def = unknown, $ = any> extends Callable<GenericInstantiation<params, def, $>> implements GenericProps<params, def, $> {
|
|
15
|
+
params: params;
|
|
16
|
+
def: def;
|
|
17
|
+
$: SchemaScope<$>;
|
|
18
|
+
readonly [arkKind] = "generic";
|
|
19
|
+
constructor(params: params, def: def, $: SchemaScope<$>);
|
|
20
|
+
}
|
|
21
|
+
export {};
|
package/out/generic.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { arkKind } from "@arktype/schema";
|
|
2
|
+
import { Callable } from "@arktype/util";
|
|
3
|
+
export class Generic extends Callable {
|
|
4
|
+
params;
|
|
5
|
+
def;
|
|
6
|
+
$;
|
|
7
|
+
[arkKind] = "generic";
|
|
8
|
+
constructor(params, def,
|
|
9
|
+
// TODO: should be Scope<$>, but breaks inference
|
|
10
|
+
$) {
|
|
11
|
+
super((...args) => {
|
|
12
|
+
// const argNodes = flatMorph(params, (i, param: string) => [
|
|
13
|
+
// param,
|
|
14
|
+
// $.parseRoot(args[i])
|
|
15
|
+
// ])
|
|
16
|
+
// { args: argNodes }
|
|
17
|
+
args;
|
|
18
|
+
return $.parseRoot(def);
|
|
19
|
+
});
|
|
20
|
+
this.params = params;
|
|
21
|
+
this.def = def;
|
|
22
|
+
this.$ = $;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"generic.js","sourceRoot":"","sources":["../generic.ts"],"names":[],"mappings":"AAAA,OAAO,EAIN,OAAO,EACP,MAAM,iBAAiB,CAAA;AACxB,OAAO,EAAE,QAAQ,EAAgB,MAAM,eAAe,CAAA;AAwCtD,MAAM,OAAO,OACZ,SAAQ,QAA8C;IAM9C;IACA;IAEA;IANC,CAAC,OAAO,CAAC,GAAG,SAAS,CAAA;IAE9B,YACQ,MAAc,EACd,GAAQ;IACf,iDAAiD;IAC1C,CAAiB;QAExB,KAAK,CAAC,CAAC,GAAG,IAAe,EAAE,EAAE;YAC5B,6DAA6D;YAC7D,UAAU;YACV,wBAAwB;YACxB,KAAK;YACL,qBAAqB;YACrB,IAAI,CAAA;YACJ,OAAO,CAAC,CAAC,SAAS,CAAC,GAAG,CAAU,CAAA;QACjC,CAAC,CAAC,CAAA;QAbK,WAAM,GAAN,MAAM,CAAQ;QACd,QAAG,GAAH,GAAG,CAAK;QAER,MAAC,GAAD,CAAC,CAAgB;IAWzB,CAAC;CACD"}
|
package/out/main.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
export {
|
|
2
|
-
export type {
|
|
3
|
-
export {
|
|
4
|
-
export
|
|
5
|
-
export {
|
|
6
|
-
export type
|
|
7
|
-
//# sourceMappingURL=main.d.ts.map
|
|
1
|
+
export { ArkError, ArkErrors, ArkTypeError } from "@arktype/schema";
|
|
2
|
+
export type { Ark, ArkConfig, Out } from "@arktype/schema";
|
|
3
|
+
export { ambient, ark, declare, define, match, type } from "./ark.js";
|
|
4
|
+
export { Module } from "./module.js";
|
|
5
|
+
export { scope, type Scope } from "./scope.js";
|
|
6
|
+
export { Type, type inferTypeRoot, type validateTypeRoot } from "./type.js";
|
package/out/main.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
export { ArkErrors
|
|
2
|
-
export {
|
|
1
|
+
export { ArkError, ArkErrors, ArkTypeError } from "@arktype/schema";
|
|
2
|
+
export { ambient, ark, declare, define, match, type } from "./ark.js";
|
|
3
|
+
export { Module } from "./module.js";
|
|
4
|
+
export { scope } from "./scope.js";
|
|
3
5
|
export { Type } from "./type.js";
|
|
4
|
-
//# sourceMappingURL=main.js.map
|
package/out/main.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"main.js","sourceRoot":"","sources":["../main.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"main.js","sourceRoot":"","sources":["../main.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAA;AAEnE,OAAO,EAAE,OAAO,EAAE,GAAG,EAAE,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM,UAAU,CAAA;AACrE,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AACpC,OAAO,EAAE,KAAK,EAAc,MAAM,YAAY,CAAA;AAC9C,OAAO,EAAE,IAAI,EAA6C,MAAM,WAAW,CAAA"}
|