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/type.ts
CHANGED
|
@@ -1,75 +1,72 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
keywords,
|
|
4
|
-
type ArkResult,
|
|
2
|
+
ArkErrors,
|
|
5
3
|
type BaseMeta,
|
|
6
|
-
type
|
|
4
|
+
type BaseRoot,
|
|
5
|
+
type Disjoint,
|
|
7
6
|
type Morph,
|
|
7
|
+
type NodeDef,
|
|
8
8
|
type Out,
|
|
9
9
|
type Predicate,
|
|
10
|
-
type
|
|
11
|
-
|
|
12
|
-
type
|
|
13
|
-
type
|
|
10
|
+
type PrimitiveConstraintKind,
|
|
11
|
+
RawSchema,
|
|
12
|
+
type Schema,
|
|
13
|
+
type ambient,
|
|
14
|
+
type constrain,
|
|
15
|
+
type constraintKindOf,
|
|
16
|
+
type distillConstrainableOut,
|
|
17
|
+
type distillIn,
|
|
18
|
+
type distillOut,
|
|
14
19
|
type includesMorphs,
|
|
20
|
+
type inferIntersection,
|
|
15
21
|
type inferMorphOut,
|
|
16
|
-
type inferNarrow
|
|
22
|
+
type inferNarrow,
|
|
23
|
+
type inferPipes,
|
|
24
|
+
type internalImplementationOf
|
|
17
25
|
} from "@arktype/schema"
|
|
18
26
|
import {
|
|
19
27
|
Callable,
|
|
20
|
-
CastableBase,
|
|
21
|
-
map,
|
|
22
28
|
type Constructor,
|
|
23
|
-
type
|
|
29
|
+
type array,
|
|
24
30
|
type conform
|
|
25
31
|
} from "@arktype/util"
|
|
32
|
+
import { Generic, type validateParameterString } from "./generic.js"
|
|
26
33
|
import type {
|
|
27
34
|
inferDefinition,
|
|
28
35
|
validateDeclared,
|
|
29
36
|
validateDefinition
|
|
30
37
|
} from "./parser/definition.js"
|
|
31
|
-
import {
|
|
32
|
-
parseGenericParams,
|
|
33
|
-
type GenericParamsParseError
|
|
34
|
-
} from "./parser/generic.js"
|
|
35
|
-
import type { inferIntersection } from "./parser/semantic/intersections.js"
|
|
38
|
+
import { parseGenericParams } from "./parser/generic.js"
|
|
36
39
|
import type {
|
|
37
40
|
IndexOneOperator,
|
|
38
41
|
IndexZeroOperator,
|
|
39
42
|
TupleInfixOperator
|
|
40
43
|
} from "./parser/tuple.js"
|
|
41
|
-
import type { Scope, bindThis } from "./scope.js"
|
|
42
|
-
|
|
44
|
+
import type { RawScope, Scope, bindThis } from "./scope.js"
|
|
45
|
+
|
|
46
|
+
export interface TypeParserAttachments {
|
|
47
|
+
errors: typeof ArkErrors
|
|
48
|
+
}
|
|
43
49
|
|
|
44
|
-
export
|
|
50
|
+
export interface TypeParser<$> extends TypeParserAttachments {
|
|
45
51
|
// Parse and check the definition, returning either the original input for a
|
|
46
52
|
// valid definition or a string representing an error message.
|
|
47
53
|
<const def>(def: validateTypeRoot<def, $>): Type<inferTypeRoot<def, $>, $>
|
|
48
54
|
|
|
49
55
|
// Spread version of a tuple expression
|
|
50
|
-
<const zero, const one, const rest extends
|
|
56
|
+
<const zero, const one, const rest extends array>(
|
|
51
57
|
_0: zero extends IndexZeroOperator ? zero : validateTypeRoot<zero, $>,
|
|
52
|
-
_1: zero extends "keyof"
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
?
|
|
61
|
-
:
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
? one extends ":"
|
|
65
|
-
? [Predicate<extractIn<inferTypeRoot<zero, $>>>]
|
|
66
|
-
: one extends "=>"
|
|
67
|
-
? // TODO: centralize
|
|
68
|
-
[Morph<extractOut<inferTypeRoot<zero, $>>, unknown>]
|
|
69
|
-
: one extends "@"
|
|
70
|
-
? [string | BaseMeta]
|
|
71
|
-
: [validateTypeRoot<rest[0], $>]
|
|
72
|
-
: []
|
|
58
|
+
_1: zero extends "keyof" ? validateTypeRoot<one, $>
|
|
59
|
+
: zero extends "instanceof" ? conform<one, Constructor>
|
|
60
|
+
: zero extends "===" ? conform<one, unknown>
|
|
61
|
+
: conform<one, IndexOneOperator>,
|
|
62
|
+
..._2: zero extends "===" ? rest
|
|
63
|
+
: zero extends "instanceof" ? conform<rest, readonly Constructor[]>
|
|
64
|
+
: one extends TupleInfixOperator ?
|
|
65
|
+
one extends ":" ? [Predicate<distillIn<inferTypeRoot<zero, $>>>]
|
|
66
|
+
: one extends "=>" ? [Morph<distillOut<inferTypeRoot<zero, $>>, unknown>]
|
|
67
|
+
: one extends "@" ? [string | BaseMeta]
|
|
68
|
+
: [validateTypeRoot<rest[0], $>]
|
|
69
|
+
: []
|
|
73
70
|
): Type<inferTypeRoot<[zero, one, ...rest], $>, $>
|
|
74
71
|
|
|
75
72
|
<params extends string, const def>(
|
|
@@ -84,264 +81,186 @@ export type TypeParser<$> = {
|
|
|
84
81
|
): Generic<parseGenericParams<params>, def, $>
|
|
85
82
|
}
|
|
86
83
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
def: validateDeclared<preinferred, def, $, bindThis<def>>
|
|
91
|
-
) => Type<preinferred, $>
|
|
92
|
-
}
|
|
84
|
+
const typeParserAttachments = Object.freeze({
|
|
85
|
+
errors: ArkErrors
|
|
86
|
+
} satisfies TypeParserAttachments)
|
|
93
87
|
|
|
94
|
-
export
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
88
|
+
export class RawTypeParser extends Callable<
|
|
89
|
+
(...args: unknown[]) => RawSchema | Generic,
|
|
90
|
+
TypeParserAttachments
|
|
91
|
+
> {
|
|
92
|
+
constructor($: RawScope) {
|
|
93
|
+
super(
|
|
94
|
+
(...args) => {
|
|
95
|
+
if (args.length === 1) {
|
|
96
|
+
// treat as a simple definition
|
|
97
|
+
return $.parseRoot(args[0])
|
|
98
|
+
}
|
|
99
|
+
if (
|
|
100
|
+
args.length === 2 &&
|
|
101
|
+
typeof args[0] === "string" &&
|
|
102
|
+
args[0][0] === "<" &&
|
|
103
|
+
args[0].at(-1) === ">"
|
|
104
|
+
) {
|
|
105
|
+
// if there are exactly two args, the first of which looks like <${string}>,
|
|
106
|
+
// treat as a generic
|
|
107
|
+
const params = parseGenericParams(args[0].slice(1, -1))
|
|
108
|
+
const def = args[1]
|
|
109
|
+
// TODO: validateUninstantiatedGeneric, remove this cast
|
|
110
|
+
return new Generic(params, def, $ as never) as never
|
|
111
|
+
}
|
|
112
|
+
// otherwise, treat as a tuple expression. technically, this also allows
|
|
113
|
+
// non-expression tuple definitions to be parsed, but it's not a supported
|
|
114
|
+
// part of the API as specified by the associated types
|
|
115
|
+
return $.parseRoot(args)
|
|
116
|
+
},
|
|
117
|
+
{ bind: $, attach: typeParserAttachments }
|
|
118
|
+
)
|
|
116
119
|
}
|
|
117
|
-
return parser as never
|
|
118
120
|
}
|
|
119
121
|
|
|
120
|
-
export type
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
keys?: KeyCheckKind
|
|
126
|
-
mustBe?: string
|
|
122
|
+
export type DeclarationParser<$> = <preinferred>() => {
|
|
123
|
+
// for some reason, making this a const parameter breaks preinferred validation
|
|
124
|
+
type: <def>(
|
|
125
|
+
def: validateDeclared<preinferred, def, $ & ambient, bindThis<def>>
|
|
126
|
+
) => Type<preinferred, $>
|
|
127
127
|
}
|
|
128
128
|
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
129
|
+
// this is declared as a class internally so we can ensure all "abstract"
|
|
130
|
+
// methods of BaseRoot are overridden, but we end up exporting it as an interface
|
|
131
|
+
// to ensure it is not accessed as a runtime value
|
|
132
|
+
declare class _Type<t = unknown, $ = any>
|
|
133
|
+
extends BaseRoot<t, $>
|
|
134
|
+
implements internalImplementationOf<Schema>
|
|
135
|
+
{
|
|
136
|
+
$: Scope<$>;
|
|
135
137
|
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
allows: this["root"]["allows"]
|
|
139
|
-
expected: string
|
|
140
|
-
json: Json
|
|
138
|
+
get in(): Type<this["tIn"], $>
|
|
139
|
+
get out(): Type<this["tOut"], $>
|
|
141
140
|
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
) {
|
|
146
|
-
const root = parseTypeRoot(definition, scope) as TypeNode<t>
|
|
147
|
-
super(root.apply, root)
|
|
148
|
-
this.root = root
|
|
149
|
-
this.allows = root.allows
|
|
150
|
-
this.config = scope.config
|
|
151
|
-
this.json = root.json
|
|
152
|
-
this.expected = this.root.description
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
configure(config: TypeConfig) {
|
|
156
|
-
this.config = { ...this.config, ...config }
|
|
157
|
-
return this
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
// TODO: should return out
|
|
161
|
-
from(literal: this["in"]["infer"]) {
|
|
162
|
-
return literal
|
|
163
|
-
}
|
|
141
|
+
intersect<def>(
|
|
142
|
+
def: validateTypeRoot<def, $>
|
|
143
|
+
): Type<inferIntersection<t, inferTypeRoot<def, $>>> | Disjoint
|
|
164
144
|
|
|
165
|
-
// TODO: Morph intersections, ordering
|
|
166
145
|
and<def>(
|
|
167
146
|
def: validateTypeRoot<def, $>
|
|
168
|
-
): Type<inferIntersection<t, inferTypeRoot<def, $>>, $>
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
): Type<
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
147
|
+
): Type<inferIntersection<t, inferTypeRoot<def, $>>, $>
|
|
148
|
+
|
|
149
|
+
or<def>(def: validateTypeRoot<def, $>): Type<t | inferTypeRoot<def, $>, $>
|
|
150
|
+
|
|
151
|
+
array(): Type<t[], $>
|
|
152
|
+
|
|
153
|
+
keyof(): Type<keyof this["inferIn"], $>
|
|
154
|
+
|
|
155
|
+
pipe<a extends Morph<this["infer"]>>(a: a): Type<inferPipes<t, [a]>, $>
|
|
156
|
+
pipe<a extends Morph<this["infer"]>, b extends Morph<inferMorphOut<a>>>(
|
|
157
|
+
a: a,
|
|
158
|
+
b: b
|
|
159
|
+
): Type<inferPipes<t, [a, b]>, $>
|
|
160
|
+
pipe<
|
|
161
|
+
a extends Morph<this["infer"]>,
|
|
162
|
+
b extends Morph<inferMorphOut<a>>,
|
|
163
|
+
c extends Morph<inferMorphOut<b>>
|
|
164
|
+
>(a: a, b: b, c: c): Type<inferPipes<t, [a, b, c]>, $>
|
|
165
|
+
pipe<
|
|
166
|
+
a extends Morph<this["infer"]>,
|
|
167
|
+
b extends Morph<inferMorphOut<a>>,
|
|
168
|
+
c extends Morph<inferMorphOut<b>>,
|
|
169
|
+
d extends Morph<inferMorphOut<c>>
|
|
170
|
+
>(a: a, b: b, c: c, d: d): Type<inferPipes<t, [a, b, c, d]>, $>
|
|
171
|
+
pipe<
|
|
172
|
+
a extends Morph<this["infer"]>,
|
|
173
|
+
b extends Morph<inferMorphOut<a>>,
|
|
174
|
+
c extends Morph<inferMorphOut<b>>,
|
|
175
|
+
d extends Morph<inferMorphOut<c>>,
|
|
176
|
+
e extends Morph<inferMorphOut<d>>
|
|
177
|
+
>(a: a, b: b, c: c, d: d, e: e): Type<inferPipes<t, [a, b, c, d, e]>, $>
|
|
178
|
+
pipe<
|
|
179
|
+
a extends Morph<this["infer"]>,
|
|
180
|
+
b extends Morph<inferMorphOut<a>>,
|
|
181
|
+
c extends Morph<inferMorphOut<b>>,
|
|
182
|
+
d extends Morph<inferMorphOut<c>>,
|
|
183
|
+
e extends Morph<inferMorphOut<d>>,
|
|
184
|
+
f extends Morph<inferMorphOut<e>>
|
|
185
|
+
>(
|
|
186
|
+
a: a,
|
|
187
|
+
b: b,
|
|
188
|
+
c: c,
|
|
189
|
+
d: d,
|
|
190
|
+
e: e,
|
|
191
|
+
f: f
|
|
192
|
+
): Type<inferPipes<t, [a, b, c, d, e, f]>, $>
|
|
193
|
+
pipe<
|
|
194
|
+
a extends Morph<this["infer"]>,
|
|
195
|
+
b extends Morph<inferMorphOut<a>>,
|
|
196
|
+
c extends Morph<inferMorphOut<b>>,
|
|
197
|
+
d extends Morph<inferMorphOut<c>>,
|
|
198
|
+
e extends Morph<inferMorphOut<d>>,
|
|
199
|
+
f extends Morph<inferMorphOut<e>>,
|
|
200
|
+
g extends Morph<inferMorphOut<f>>
|
|
201
|
+
>(
|
|
202
|
+
a: a,
|
|
203
|
+
b: b,
|
|
204
|
+
c: c,
|
|
205
|
+
d: d,
|
|
206
|
+
e: e,
|
|
207
|
+
f: f,
|
|
208
|
+
g: g
|
|
209
|
+
): Type<inferPipes<t, [a, b, c, d, e, f, g]>, $>
|
|
206
210
|
|
|
207
211
|
// TODO: based on below, should maybe narrow morph output if used after
|
|
208
|
-
narrow<def extends Predicate<
|
|
212
|
+
narrow<def extends Predicate<distillConstrainableOut<t>>>(
|
|
209
213
|
def: def
|
|
210
214
|
): Type<
|
|
211
|
-
includesMorphs<t> extends true
|
|
212
|
-
|
|
213
|
-
|
|
215
|
+
includesMorphs<t> extends true ?
|
|
216
|
+
(In: this["tIn"]) => Out<inferNarrow<this["infer"], def>>
|
|
217
|
+
: inferNarrow<this["infer"], def>,
|
|
214
218
|
$
|
|
215
|
-
>
|
|
216
|
-
return new Type(this.root.constrain("predicate", def), this.scope) as never
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
array(): Type<t[], $> {
|
|
220
|
-
return new Type(this.root.array(), this.scope) as never
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
keyof(): Type<keyof this["in"]["infer"], $> {
|
|
224
|
-
return new Type(this.root.keyof(), this.scope) as never
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
assert(data: unknown): this["infer"] {
|
|
228
|
-
const result = this.call(null, data)
|
|
229
|
-
return result.errors ? result.errors.throw() : result.out
|
|
230
|
-
}
|
|
219
|
+
>
|
|
231
220
|
|
|
232
221
|
equals<def>(
|
|
233
|
-
|
|
234
|
-
): this is Type<inferTypeRoot<def
|
|
235
|
-
return this.root.equals(parseTypeRoot(other, this.scope))
|
|
236
|
-
}
|
|
222
|
+
def: validateTypeRoot<def, $>
|
|
223
|
+
): this is Type<inferTypeRoot<def>, $>
|
|
237
224
|
|
|
225
|
+
// TODO: i/o
|
|
226
|
+
extract<def>(r: validateTypeRoot<def, $>): Type<t, $>
|
|
227
|
+
exclude<def>(r: validateTypeRoot<def, $>): Type<t, $>
|
|
238
228
|
extends<def>(
|
|
239
229
|
other: validateTypeRoot<def, $>
|
|
240
|
-
): this is Type<inferTypeRoot<def
|
|
241
|
-
return this.root.extends(parseTypeRoot(other, this.scope))
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
private inCache?: Type<extractIn<t>, $>;
|
|
245
|
-
get in() {
|
|
246
|
-
this.inCache ??= new Type(this.root.in, this.scope) as never
|
|
247
|
-
return this.inCache
|
|
248
|
-
}
|
|
230
|
+
): this is Type<inferTypeRoot<def>, $>
|
|
249
231
|
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
232
|
+
constrain<
|
|
233
|
+
kind extends PrimitiveConstraintKind,
|
|
234
|
+
const def extends NodeDef<kind>
|
|
235
|
+
>(
|
|
236
|
+
kind: conform<kind, constraintKindOf<this["inferIn"]>>,
|
|
237
|
+
def: def
|
|
238
|
+
): Type<constrain<t, kind, def>, $>
|
|
255
239
|
}
|
|
256
240
|
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
export type validateTypeRoot<def, $> = validateDefinition<def, $, bindThis<def>>
|
|
264
|
-
|
|
265
|
-
export type inferTypeRoot<def, $> = inferDefinition<def, $, bindThis<def>>
|
|
266
|
-
|
|
267
|
-
type validateParameterString<params extends string> =
|
|
268
|
-
parseGenericParams<params> extends GenericParamsParseError<infer message>
|
|
269
|
-
? message
|
|
270
|
-
: params
|
|
241
|
+
export interface Type<
|
|
242
|
+
/** @ts-expect-error allow instantiation assignment to the base type */
|
|
243
|
+
out t = unknown,
|
|
244
|
+
$ = any
|
|
245
|
+
> extends _Type<t, $> {}
|
|
271
246
|
|
|
272
|
-
export
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
g.definition,
|
|
277
|
-
// once we support constraints on generic parameters, we'd use
|
|
278
|
-
// the base type here: https://github.com/arktypeio/arktype/issues/796
|
|
279
|
-
{
|
|
280
|
-
baseName: "generic",
|
|
281
|
-
args: map(g.parameters, (_, name) => [name, keywords.unknown])
|
|
282
|
-
}
|
|
283
|
-
)
|
|
284
|
-
return g
|
|
285
|
-
}
|
|
247
|
+
export type TypeConstructor<t = unknown, $ = any> = new (
|
|
248
|
+
def: unknown,
|
|
249
|
+
$: Scope<$>
|
|
250
|
+
) => Type<t, $>
|
|
286
251
|
|
|
287
|
-
export const
|
|
288
|
-
parameters: string[],
|
|
289
|
-
definition: unknown,
|
|
290
|
-
scope: Scope
|
|
291
|
-
) => {
|
|
292
|
-
return Object.assign(
|
|
293
|
-
(...args: unknown[]) => {
|
|
294
|
-
const argNodes = map(parameters, (i, param) => [
|
|
295
|
-
param,
|
|
296
|
-
parseTypeRoot(args[i], scope)
|
|
297
|
-
])
|
|
298
|
-
const root = parseTypeRoot(definition, scope, argNodes)
|
|
299
|
-
return new Type(root, scope)
|
|
300
|
-
},
|
|
301
|
-
{
|
|
302
|
-
[arkKind]: "generic",
|
|
303
|
-
parameters,
|
|
304
|
-
definition,
|
|
305
|
-
scope
|
|
306
|
-
// $ is only needed at compile-time
|
|
307
|
-
} satisfies Omit<GenericProps, "$">
|
|
308
|
-
) as unknown as Generic
|
|
309
|
-
}
|
|
252
|
+
export const Type: TypeConstructor = RawSchema as never
|
|
310
253
|
|
|
311
|
-
|
|
312
|
-
export type GenericProps<
|
|
313
|
-
params extends string[] = string[],
|
|
314
|
-
def = unknown,
|
|
315
|
-
$ = any
|
|
316
|
-
> = {
|
|
317
|
-
[arkKind]: "generic"
|
|
318
|
-
$: $
|
|
319
|
-
parameters: params
|
|
320
|
-
definition: def
|
|
321
|
-
scope: Scope
|
|
322
|
-
}
|
|
254
|
+
export type DefinitionParser<$> = <def>(def: validateTypeRoot<def, $>) => def
|
|
323
255
|
|
|
324
|
-
export type
|
|
256
|
+
export type validateTypeRoot<def, $ = {}> = validateDefinition<
|
|
257
|
+
def,
|
|
258
|
+
$ & ambient,
|
|
259
|
+
bindThis<def>
|
|
260
|
+
>
|
|
325
261
|
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
def
|
|
330
|
-
|
|
331
|
-
> = {
|
|
332
|
-
<args>(
|
|
333
|
-
...args: conform<
|
|
334
|
-
args,
|
|
335
|
-
{
|
|
336
|
-
[i in keyof params]: validateTypeRoot<args[i & keyof args], $>
|
|
337
|
-
}
|
|
338
|
-
>
|
|
339
|
-
): Type<inferDefinition<def, $, bindGenericInstantiation<params, $, args>>, $>
|
|
340
|
-
} & GenericProps<params, def, $>
|
|
341
|
-
|
|
342
|
-
type bindGenericInstantiation<params extends string[], $, args> = {
|
|
343
|
-
[i in keyof params & `${number}` as params[i]]: inferTypeRoot<
|
|
344
|
-
args[i & keyof args],
|
|
345
|
-
$
|
|
346
|
-
>
|
|
347
|
-
}
|
|
262
|
+
export type inferTypeRoot<def, $ = {}> = inferDefinition<
|
|
263
|
+
def,
|
|
264
|
+
$ & ambient,
|
|
265
|
+
bindThis<def>
|
|
266
|
+
>
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import { attest } from "@arktype/attest"
|
|
2
|
-
import type { astToString } from "../parser/semantic/utils.js"
|
|
3
|
-
|
|
4
|
-
describe("astToString", () => {
|
|
5
|
-
it("no parentheses if nested ast is an array", () => {
|
|
6
|
-
const t = {} as unknown as astToString<[["number", "[]"], "|", "number"]>
|
|
7
|
-
attest<"'number[]|number'">(t)
|
|
8
|
-
})
|
|
9
|
-
it("parentheses if nested ast is an infix expression", () => {
|
|
10
|
-
const t = {} as unknown as astToString<[["0", "|", "1"], "|", "string"]>
|
|
11
|
-
attest<"'(0|1)|string'">(t)
|
|
12
|
-
})
|
|
13
|
-
it("defaults to '...' if input is bad", () => {
|
|
14
|
-
const t = {} as unknown as astToString<["0", "///", "1"]>
|
|
15
|
-
attest<"'...'">(t)
|
|
16
|
-
})
|
|
17
|
-
})
|