arktype 2.0.0-dev.2 → 2.0.0-dev.21
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/LICENSE +7 -0
- package/out/api.d.ts +6 -0
- package/out/api.js +5 -0
- package/out/ark.d.ts +15 -39
- package/out/ark.js +9 -18
- package/out/config.d.ts +1 -0
- package/out/config.js +2 -0
- package/out/generic.d.ts +21 -0
- package/out/generic.js +24 -0
- package/out/match.d.ts +18 -24
- package/out/match.js +50 -60
- package/out/module.d.ts +11 -0
- package/out/module.js +2 -0
- package/out/parser/definition.d.ts +13 -15
- package/out/parser/definition.js +10 -13
- package/out/parser/generic.d.ts +5 -7
- package/out/parser/generic.js +7 -11
- package/out/parser/objectLiteral.d.ts +57 -18
- package/out/parser/objectLiteral.js +93 -52
- package/out/parser/semantic/bounds.d.ts +10 -8
- package/out/parser/semantic/bounds.js +0 -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/utils.d.ts +3 -7
- package/out/parser/semantic/utils.js +0 -1
- package/out/parser/semantic/validate.d.ts +16 -13
- package/out/parser/semantic/validate.js +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/shared.d.ts +14 -28
- package/out/parser/string/reduce/shared.js +4 -3
- package/out/parser/string/reduce/static.d.ts +26 -23
- package/out/parser/string/reduce/static.js +0 -1
- package/out/parser/string/shift/operand/date.d.ts +2 -2
- package/out/parser/string/shift/operand/date.js +4 -7
- package/out/parser/string/shift/operand/enclosed.d.ts +3 -4
- package/out/parser/string/shift/operand/enclosed.js +14 -14
- 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/operand.d.ts +3 -4
- package/out/parser/string/shift/operand/operand.js +9 -13
- package/out/parser/string/shift/operand/unenclosed.d.ts +15 -20
- package/out/parser/string/shift/operand/unenclosed.js +24 -36
- package/out/parser/string/shift/operator/bounds.d.ts +8 -12
- package/out/parser/string/shift/operator/bounds.js +45 -56
- package/out/parser/string/shift/operator/divisor.d.ts +1 -3
- package/out/parser/string/shift/operator/divisor.js +1 -3
- package/out/parser/string/shift/operator/operator.d.ts +1 -2
- package/out/parser/string/shift/operator/operator.js +12 -19
- package/out/parser/string/shift/scanner.d.ts +8 -7
- package/out/parser/string/shift/scanner.js +17 -16
- package/out/parser/string/string.d.ts +9 -7
- package/out/parser/string/string.js +1 -3
- package/out/parser/tuple.d.ts +101 -42
- package/out/parser/tuple.js +107 -86
- package/out/scope.d.ts +44 -115
- package/out/scope.js +39 -188
- package/out/type.d.ts +63 -61
- package/out/type.js +30 -135
- package/package.json +33 -39
- package/__tests__/array.test.ts +0 -310
- package/__tests__/astUtils.test.ts +0 -17
- package/__tests__/badDefinitionType.test.ts +0 -51
- package/__tests__/basis.test.ts +0 -46
- package/__tests__/bounds.test.ts +0 -388
- package/__tests__/cast.test.ts +0 -32
- package/__tests__/completions.test.ts +0 -46
- package/__tests__/config.test.ts +0 -77
- package/__tests__/cyclic.test.ts +0 -144
- package/__tests__/dateLiteral.test.ts +0 -32
- package/__tests__/declared.test.ts +0 -124
- package/__tests__/discrimination.test.ts +0 -74
- package/__tests__/divisor.test.ts +0 -90
- package/__tests__/enclosed.test.ts +0 -93
- package/__tests__/examples.test.ts +0 -56
- package/__tests__/expressions.test.ts +0 -138
- package/__tests__/generateBenchData.ts +0 -89
- package/__tests__/generated/cyclic.ts +0 -3058
- package/__tests__/generic.test.ts +0 -294
- package/__tests__/group.test.ts +0 -72
- package/__tests__/hkt.test.ts +0 -22
- package/__tests__/imports.test.ts +0 -113
- package/__tests__/instanceof.test.ts +0 -102
- package/__tests__/intersection.test.ts +0 -104
- package/__tests__/jsdoc.test.ts +0 -64
- package/__tests__/keyTraversal.test.ts +0 -74
- package/__tests__/keyof.test.ts +0 -134
- package/__tests__/keywords.test.ts +0 -188
- package/__tests__/literal.test.ts +0 -51
- package/__tests__/match.bench.ts +0 -31
- package/__tests__/match.test.ts +0 -224
- package/__tests__/methods.bench.ts +0 -22
- package/__tests__/morph.test.ts +0 -409
- package/__tests__/narrow.test.ts +0 -102
- package/__tests__/nodeDefinitions.test.ts +0 -190
- package/__tests__/object.bench.ts +0 -28
- package/__tests__/objectLiteral.test.ts +0 -301
- package/__tests__/operand.bench.ts +0 -45
- package/__tests__/operator.bench.ts +0 -130
- package/__tests__/regex.test.ts +0 -58
- package/__tests__/scope.bench.ts +0 -39
- package/__tests__/scope.test.ts +0 -140
- package/__tests__/string.test.ts +0 -48
- package/__tests__/submodule.test.ts +0 -99
- package/__tests__/this.test.ts +0 -57
- package/__tests__/thunk.test.ts +0 -80
- package/__tests__/traverse.test.ts +0 -119
- package/__tests__/tsGenerics.test.ts +0 -12
- package/__tests__/typeMethods.test.ts +0 -69
- package/__tests__/typeReference.test.ts +0 -39
- package/__tests__/unenclosed.test.ts +0 -120
- package/__tests__/union.test.ts +0 -147
- package/ark.ts +0 -91
- package/main.ts +0 -18
- package/match.ts +0 -243
- package/out/ark.d.ts.map +0 -1
- package/out/ark.js.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 +0 -7
- package/out/main.d.ts.map +0 -1
- package/out/main.js +0 -4
- package/out/main.js.map +0 -1
- package/out/match.d.ts.map +0 -1
- package/out/match.js.map +0 -1
- package/out/parser/definition.d.ts.map +0 -1
- package/out/parser/definition.js.map +0 -1
- package/out/parser/generic.d.ts.map +0 -1
- package/out/parser/generic.js.map +0 -1
- package/out/parser/objectLiteral.d.ts.map +0 -1
- package/out/parser/objectLiteral.js.map +0 -1
- package/out/parser/semantic/bounds.d.ts.map +0 -1
- package/out/parser/semantic/bounds.js.map +0 -1
- package/out/parser/semantic/divisor.d.ts.map +0 -1
- package/out/parser/semantic/divisor.js.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/utils.js.map +0 -1
- package/out/parser/semantic/validate.d.ts.map +0 -1
- package/out/parser/semantic/validate.js.map +0 -1
- package/out/parser/shared.d.ts +0 -56
- package/out/parser/shared.d.ts.map +0 -1
- package/out/parser/shared.js +0 -55
- package/out/parser/shared.js.map +0 -1
- package/out/parser/string/reduce/dynamic.d.ts.map +0 -1
- package/out/parser/string/reduce/dynamic.js.map +0 -1
- package/out/parser/string/reduce/shared.d.ts.map +0 -1
- package/out/parser/string/reduce/shared.js.map +0 -1
- package/out/parser/string/reduce/static.d.ts.map +0 -1
- package/out/parser/string/reduce/static.js.map +0 -1
- package/out/parser/string/shift/operand/date.d.ts.map +0 -1
- package/out/parser/string/shift/operand/date.js.map +0 -1
- package/out/parser/string/shift/operand/enclosed.d.ts.map +0 -1
- package/out/parser/string/shift/operand/enclosed.js.map +0 -1
- package/out/parser/string/shift/operand/genericArgs.d.ts.map +0 -1
- package/out/parser/string/shift/operand/genericArgs.js.map +0 -1
- package/out/parser/string/shift/operand/operand.d.ts.map +0 -1
- package/out/parser/string/shift/operand/operand.js.map +0 -1
- package/out/parser/string/shift/operand/unenclosed.d.ts.map +0 -1
- package/out/parser/string/shift/operand/unenclosed.js.map +0 -1
- package/out/parser/string/shift/operator/bounds.d.ts.map +0 -1
- package/out/parser/string/shift/operator/bounds.js.map +0 -1
- package/out/parser/string/shift/operator/divisor.d.ts.map +0 -1
- package/out/parser/string/shift/operator/divisor.js.map +0 -1
- package/out/parser/string/shift/operator/operator.d.ts.map +0 -1
- package/out/parser/string/shift/operator/operator.js.map +0 -1
- package/out/parser/string/shift/scanner.d.ts.map +0 -1
- package/out/parser/string/shift/scanner.js.map +0 -1
- package/out/parser/string/string.d.ts.map +0 -1
- package/out/parser/string/string.js.map +0 -1
- package/out/parser/tuple.d.ts.map +0 -1
- package/out/parser/tuple.js.map +0 -1
- package/out/scope.d.ts.map +0 -1
- package/out/scope.js.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/type.js.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/definition.ts +0 -159
- package/parser/generic.ts +0 -68
- package/parser/objectLiteral.ts +0 -197
- package/parser/semantic/bounds.ts +0 -61
- package/parser/semantic/divisor.ts +0 -13
- package/parser/semantic/intersections.ts +0 -46
- package/parser/semantic/semantic.ts +0 -143
- package/parser/semantic/utils.ts +0 -26
- package/parser/semantic/validate.ts +0 -109
- package/parser/shared.ts +0 -167
- package/parser/string/reduce/dynamic.ts +0 -196
- package/parser/string/reduce/shared.ts +0 -101
- package/parser/string/reduce/static.ts +0 -298
- package/parser/string/shift/operand/date.ts +0 -54
- package/parser/string/shift/operand/enclosed.ts +0 -118
- package/parser/string/shift/operand/genericArgs.ts +0 -127
- package/parser/string/shift/operand/operand.ts +0 -48
- package/parser/string/shift/operand/unenclosed.ts +0 -268
- package/parser/string/shift/operator/bounds.ts +0 -264
- package/parser/string/shift/operator/divisor.ts +0 -36
- package/parser/string/shift/operator/operator.ts +0 -82
- package/parser/string/shift/scanner.ts +0 -209
- package/parser/string/string.ts +0 -90
- package/parser/tuple.ts +0 -475
- package/scope.ts +0 -548
- package/type.ts +0 -349
- package/util.ts +0 -36
package/type.ts
DELETED
|
@@ -1,349 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
inferred,
|
|
3
|
-
keywords,
|
|
4
|
-
type ArkResult,
|
|
5
|
-
type BaseMeta,
|
|
6
|
-
type Morph,
|
|
7
|
-
type Out,
|
|
8
|
-
type Predicate,
|
|
9
|
-
type TypeNode,
|
|
10
|
-
type distill,
|
|
11
|
-
type extractIn,
|
|
12
|
-
type extractOut,
|
|
13
|
-
type includesMorphs,
|
|
14
|
-
type inferMorphOut,
|
|
15
|
-
type inferNarrow
|
|
16
|
-
} from "@arktype/schema"
|
|
17
|
-
import {
|
|
18
|
-
Callable,
|
|
19
|
-
map,
|
|
20
|
-
type Constructor,
|
|
21
|
-
type Json,
|
|
22
|
-
type conform
|
|
23
|
-
} from "@arktype/util"
|
|
24
|
-
import type {
|
|
25
|
-
inferDefinition,
|
|
26
|
-
validateDeclared,
|
|
27
|
-
validateDefinition
|
|
28
|
-
} from "./parser/definition.js"
|
|
29
|
-
import {
|
|
30
|
-
parseGenericParams,
|
|
31
|
-
type GenericParamsParseError
|
|
32
|
-
} from "./parser/generic.js"
|
|
33
|
-
import type { inferIntersection } from "./parser/semantic/intersections.js"
|
|
34
|
-
import { configureShallowDescendants } from "./parser/shared.js"
|
|
35
|
-
import type {
|
|
36
|
-
IndexOneOperator,
|
|
37
|
-
IndexZeroOperator,
|
|
38
|
-
TupleInfixOperator
|
|
39
|
-
} from "./parser/tuple.js"
|
|
40
|
-
import type { Scope, bindThis } from "./scope.js"
|
|
41
|
-
import { arkKind } from "./util.js"
|
|
42
|
-
|
|
43
|
-
export type TypeParser<$> = {
|
|
44
|
-
// Parse and check the definition, returning either the original input for a
|
|
45
|
-
// valid definition or a string representing an error message.
|
|
46
|
-
<const def>(def: validateTypeRoot<def, $>): Type<inferTypeRoot<def, $>, $>
|
|
47
|
-
|
|
48
|
-
// Spread version of a tuple expression
|
|
49
|
-
<const zero, const one, const rest extends readonly unknown[]>(
|
|
50
|
-
_0: zero extends IndexZeroOperator ? zero : validateTypeRoot<zero, $>,
|
|
51
|
-
_1: zero extends "keyof"
|
|
52
|
-
? validateTypeRoot<one, $>
|
|
53
|
-
: zero extends "instanceof"
|
|
54
|
-
? conform<one, Constructor>
|
|
55
|
-
: zero extends "==="
|
|
56
|
-
? conform<one, unknown>
|
|
57
|
-
: conform<one, IndexOneOperator>,
|
|
58
|
-
..._2: zero extends "==="
|
|
59
|
-
? rest
|
|
60
|
-
: zero extends "instanceof"
|
|
61
|
-
? conform<rest, readonly Constructor[]>
|
|
62
|
-
: one extends TupleInfixOperator
|
|
63
|
-
? one extends ":"
|
|
64
|
-
? [Predicate<extractIn<inferTypeRoot<zero, $>>>]
|
|
65
|
-
: one extends "=>"
|
|
66
|
-
? // TODO: centralize
|
|
67
|
-
[Morph<extractOut<inferTypeRoot<zero, $>>, unknown>]
|
|
68
|
-
: one extends "@"
|
|
69
|
-
? [string | BaseMeta]
|
|
70
|
-
: [validateTypeRoot<rest[0], $>]
|
|
71
|
-
: []
|
|
72
|
-
): Type<inferTypeRoot<[zero, one, ...rest], $>, $>
|
|
73
|
-
|
|
74
|
-
<params extends string, const def>(
|
|
75
|
-
params: `<${validateParameterString<params>}>`,
|
|
76
|
-
def: validateDefinition<
|
|
77
|
-
def,
|
|
78
|
-
$,
|
|
79
|
-
{
|
|
80
|
-
[param in parseGenericParams<params>[number]]: unknown
|
|
81
|
-
}
|
|
82
|
-
>
|
|
83
|
-
): Generic<parseGenericParams<params>, def, $>
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
export type DeclarationParser<$> = <preinferred>() => {
|
|
87
|
-
// for some reason, making this a const parameter breaks preinferred validation
|
|
88
|
-
type: <def>(
|
|
89
|
-
def: validateDeclared<preinferred, def, $, bindThis<def>>
|
|
90
|
-
) => Type<preinferred, $>
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
export const createTypeParser = <$>(scope: Scope): TypeParser<$> => {
|
|
94
|
-
const parser = (...args: unknown[]): Type | Generic => {
|
|
95
|
-
if (args.length === 1) {
|
|
96
|
-
// treat as a simple definition
|
|
97
|
-
return new Type(args[0], scope)
|
|
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
|
-
return validateUninstantiatedGeneric(generic(params, def, scope) as never)
|
|
110
|
-
}
|
|
111
|
-
// otherwise, treat as a tuple expression. technically, this also allows
|
|
112
|
-
// non-expression tuple definitions to be parsed, but it's not a supported
|
|
113
|
-
// part of the API as specified by the associated types
|
|
114
|
-
return new Type(args, scope)
|
|
115
|
-
}
|
|
116
|
-
return parser as never
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
export type DefinitionParser<$> = <def>(
|
|
120
|
-
def: validateDefinition<def, $, bindThis<def>>
|
|
121
|
-
) => def
|
|
122
|
-
|
|
123
|
-
export class Type<t = unknown, $ = any> extends Callable<
|
|
124
|
-
(data: unknown) => ArkResult<distill<extractOut<t>>>
|
|
125
|
-
> {
|
|
126
|
-
declare [inferred]: t
|
|
127
|
-
// TODO: in/out?
|
|
128
|
-
declare infer: distill<extractOut<t>>
|
|
129
|
-
|
|
130
|
-
root: TypeNode<t>
|
|
131
|
-
allows: this["root"]["allows"]
|
|
132
|
-
description: string
|
|
133
|
-
json: Json
|
|
134
|
-
|
|
135
|
-
constructor(
|
|
136
|
-
public definition: unknown,
|
|
137
|
-
public scope: Scope
|
|
138
|
-
) {
|
|
139
|
-
const root = parseTypeRoot(definition, scope) as TypeNode<t>
|
|
140
|
-
super(root.apply, root)
|
|
141
|
-
this.root = root
|
|
142
|
-
this.allows = root.allows
|
|
143
|
-
this.json = root.json
|
|
144
|
-
this.description = this.root.description
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
configure(configOrDescription: BaseMeta | string): this {
|
|
148
|
-
return new Type(
|
|
149
|
-
configureShallowDescendants(this.root, configOrDescription),
|
|
150
|
-
this.scope
|
|
151
|
-
) as never
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
describe(description: string): this {
|
|
155
|
-
return this.configure(description)
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
// TODO: should return out
|
|
159
|
-
from(literal: this["in"]["infer"]) {
|
|
160
|
-
return literal
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
// TODO: Morph intersections, ordering
|
|
164
|
-
and<def>(
|
|
165
|
-
def: validateTypeRoot<def, $>
|
|
166
|
-
): Type<inferIntersection<t, inferTypeRoot<def, $>>, $> {
|
|
167
|
-
return new Type(
|
|
168
|
-
this.root.and(parseTypeRoot(def, this.scope)),
|
|
169
|
-
this.scope
|
|
170
|
-
) as never
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
or<def>(def: validateTypeRoot<def, $>): Type<t | inferTypeRoot<def, $>, $> {
|
|
174
|
-
return new Type(
|
|
175
|
-
this.root.or(parseTypeRoot(def, this.scope)),
|
|
176
|
-
this.scope
|
|
177
|
-
) as never
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
// TODO: standardize these
|
|
181
|
-
morph<morph extends Morph<this["infer"]>>(
|
|
182
|
-
morph: morph
|
|
183
|
-
): Type<(In: this["in"]["infer"]) => Out<inferMorphOut<ReturnType<morph>>>, $>
|
|
184
|
-
morph<morph extends Morph<this["infer"]>, def>(
|
|
185
|
-
morph: morph,
|
|
186
|
-
outValidator: validateTypeRoot<def, $>
|
|
187
|
-
): Type<
|
|
188
|
-
(In: this["in"]["infer"]) => Out<
|
|
189
|
-
// TODO: validate overlapping
|
|
190
|
-
// inferMorphOut<ReturnType<morph>> &
|
|
191
|
-
extractOut<inferTypeRoot<def, $>>
|
|
192
|
-
>,
|
|
193
|
-
$
|
|
194
|
-
>
|
|
195
|
-
morph(morph: Morph, outValidator?: unknown) {
|
|
196
|
-
// TODO: tuple expression for out validator
|
|
197
|
-
outValidator
|
|
198
|
-
return this as never
|
|
199
|
-
// return new Type(
|
|
200
|
-
// this.root.constrain("morph", morph),
|
|
201
|
-
// this.scope
|
|
202
|
-
// ) as never
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
// TODO: based on below, should maybe narrow morph output if used after
|
|
206
|
-
narrow<def extends Predicate<extractOut<t>>>(
|
|
207
|
-
def: def
|
|
208
|
-
): Type<
|
|
209
|
-
includesMorphs<t> extends true
|
|
210
|
-
? (In: this["in"]["infer"]) => Out<inferNarrow<this["infer"], def>>
|
|
211
|
-
: inferNarrow<this["infer"], def>,
|
|
212
|
-
$
|
|
213
|
-
> {
|
|
214
|
-
return new Type(this.root.constrain("predicate", def), this.scope) as never
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
array(): Type<t[], $> {
|
|
218
|
-
return new Type(this.root.array(), this.scope) as never
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
keyof(): Type<keyof this["in"]["infer"], $> {
|
|
222
|
-
return new Type(this.root.keyof(), this.scope) as never
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
assert(data: unknown): this["infer"] {
|
|
226
|
-
const result = this.call(null, data)
|
|
227
|
-
return result.errors ? result.errors.throw() : result.out
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
equals<def>(
|
|
231
|
-
other: validateTypeRoot<def, $>
|
|
232
|
-
): this is Type<inferTypeRoot<def, $>, $> {
|
|
233
|
-
return this.root.equals(parseTypeRoot(other, this.scope))
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
extends<def>(
|
|
237
|
-
other: validateTypeRoot<def, $>
|
|
238
|
-
): this is Type<inferTypeRoot<def, $>, $> {
|
|
239
|
-
return this.root.extends(parseTypeRoot(other, this.scope))
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
private inCache?: Type<extractIn<t>, $>;
|
|
243
|
-
get in() {
|
|
244
|
-
this.inCache ??= new Type(this.root.in, this.scope) as never
|
|
245
|
-
return this.inCache
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
outCache?: Type<extractOut<t>, $>
|
|
249
|
-
get out() {
|
|
250
|
-
this.outCache ??= new Type(this.root.out, this.scope) as never
|
|
251
|
-
return this.outCache
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
toString() {
|
|
255
|
-
return this.description
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
const parseTypeRoot = (def: unknown, scope: Scope, args?: BoundArgs) =>
|
|
260
|
-
scope.parseDefinition(def, {
|
|
261
|
-
args: args ?? scope.bindThis(),
|
|
262
|
-
baseName: "type"
|
|
263
|
-
})
|
|
264
|
-
|
|
265
|
-
export type validateTypeRoot<def, $> = validateDefinition<def, $, bindThis<def>>
|
|
266
|
-
|
|
267
|
-
export type inferTypeRoot<def, $> = inferDefinition<def, $, bindThis<def>>
|
|
268
|
-
|
|
269
|
-
type validateParameterString<params extends string> =
|
|
270
|
-
parseGenericParams<params> extends GenericParamsParseError<infer message>
|
|
271
|
-
? message
|
|
272
|
-
: params
|
|
273
|
-
|
|
274
|
-
export const validateUninstantiatedGeneric = (g: Generic) => {
|
|
275
|
-
// the unconstrained instantiation of the generic is not used for now
|
|
276
|
-
// other than to eagerly validate that the def does not contain any errors
|
|
277
|
-
g.scope.parseDefinition(
|
|
278
|
-
g.definition,
|
|
279
|
-
// once we support constraints on generic parameters, we'd use
|
|
280
|
-
// the base type here: https://github.com/arktypeio/arktype/issues/796
|
|
281
|
-
{
|
|
282
|
-
baseName: "generic",
|
|
283
|
-
args: map(g.parameters, (_, name) => [name, keywords.unknown])
|
|
284
|
-
}
|
|
285
|
-
)
|
|
286
|
-
return g
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
export const generic = (
|
|
290
|
-
parameters: string[],
|
|
291
|
-
definition: unknown,
|
|
292
|
-
scope: Scope
|
|
293
|
-
) => {
|
|
294
|
-
return Object.assign(
|
|
295
|
-
(...args: unknown[]) => {
|
|
296
|
-
const argNodes = map(parameters, (i, param) => [
|
|
297
|
-
param,
|
|
298
|
-
parseTypeRoot(args[i], scope)
|
|
299
|
-
])
|
|
300
|
-
const root = parseTypeRoot(definition, scope, argNodes)
|
|
301
|
-
return new Type(root, scope)
|
|
302
|
-
},
|
|
303
|
-
{
|
|
304
|
-
[arkKind]: "generic",
|
|
305
|
-
parameters,
|
|
306
|
-
definition,
|
|
307
|
-
scope
|
|
308
|
-
// $ is only needed at compile-time
|
|
309
|
-
} satisfies Omit<GenericProps, "$">
|
|
310
|
-
) as unknown as Generic
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
// Comparing to Generic directly doesn't work well, so we compare to only its props
|
|
314
|
-
export type GenericProps<
|
|
315
|
-
params extends string[] = string[],
|
|
316
|
-
def = unknown,
|
|
317
|
-
$ = any
|
|
318
|
-
> = {
|
|
319
|
-
[arkKind]: "generic"
|
|
320
|
-
$: $
|
|
321
|
-
parameters: params
|
|
322
|
-
definition: def
|
|
323
|
-
scope: Scope
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
export type BoundArgs = Record<string, TypeNode>
|
|
327
|
-
|
|
328
|
-
// TODO: Fix external reference (i.e. if this is attached to a scope, then args are defined using it)
|
|
329
|
-
export type Generic<
|
|
330
|
-
params extends string[] = string[],
|
|
331
|
-
def = unknown,
|
|
332
|
-
$ = any
|
|
333
|
-
> = {
|
|
334
|
-
<args>(
|
|
335
|
-
...args: conform<
|
|
336
|
-
args,
|
|
337
|
-
{
|
|
338
|
-
[i in keyof params]: validateTypeRoot<args[i & keyof args], $>
|
|
339
|
-
}
|
|
340
|
-
>
|
|
341
|
-
): Type<inferDefinition<def, $, bindGenericInstantiation<params, $, args>>, $>
|
|
342
|
-
} & GenericProps<params, def, $>
|
|
343
|
-
|
|
344
|
-
type bindGenericInstantiation<params extends string[], $, args> = {
|
|
345
|
-
[i in keyof params & `${number}` as params[i]]: inferTypeRoot<
|
|
346
|
-
args[i & keyof args],
|
|
347
|
-
$
|
|
348
|
-
>
|
|
349
|
-
}
|
package/util.ts
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import type { Module, Scope } from "./scope.js"
|
|
2
|
-
import type { Generic, Type } from "./type.js"
|
|
3
|
-
|
|
4
|
-
export const arkKind = Symbol("ArkTypeInternalKind")
|
|
5
|
-
|
|
6
|
-
export type ArkKinds = {
|
|
7
|
-
type: Type
|
|
8
|
-
scope: Scope
|
|
9
|
-
generic: Generic
|
|
10
|
-
module: Module
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export type ArkKind = keyof ArkKinds
|
|
14
|
-
|
|
15
|
-
export const addArkKind = <kind extends ArkKind>(
|
|
16
|
-
value: Omit<ArkKinds[kind], arkKind> & {
|
|
17
|
-
[arkKind]?: kind
|
|
18
|
-
},
|
|
19
|
-
kind: kind
|
|
20
|
-
): ArkKinds[kind] =>
|
|
21
|
-
Object.defineProperty(value, arkKind, {
|
|
22
|
-
value: kind,
|
|
23
|
-
enumerable: false
|
|
24
|
-
}) as never
|
|
25
|
-
|
|
26
|
-
export type addArkKind<
|
|
27
|
-
kind extends ArkKind,
|
|
28
|
-
t extends Omit<ArkKinds[kind], arkKind>
|
|
29
|
-
> = t & { [arkKind]: kind }
|
|
30
|
-
|
|
31
|
-
export type arkKind = typeof arkKind
|
|
32
|
-
|
|
33
|
-
export const hasArkKind = <kind extends ArkKind>(
|
|
34
|
-
value: unknown,
|
|
35
|
-
kind: kind
|
|
36
|
-
): value is ArkKinds[kind] => (value as any)?.[arkKind] === kind
|