arktype 2.0.0-dev.10 → 2.0.0-dev.11
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/{main.ts → out/api.d.ts} +6 -6
- package/out/api.js +5 -0
- package/out/ark.d.ts +1 -1
- package/out/config.d.ts +1 -0
- package/out/config.js +2 -0
- package/out/generic.d.ts +3 -3
- package/out/match.js +1 -1
- package/out/module.d.ts +4 -2
- package/out/parser/definition.d.ts +10 -8
- package/out/parser/definition.js +2 -2
- package/out/parser/objectLiteral.d.ts +8 -13
- package/out/parser/objectLiteral.js +20 -23
- package/out/parser/semantic/bounds.d.ts +3 -3
- package/out/parser/semantic/divisor.d.ts +2 -2
- package/out/parser/semantic/infer.d.ts +1 -1
- package/out/parser/semantic/validate.d.ts +6 -4
- package/out/parser/string/reduce/dynamic.d.ts +6 -6
- package/out/parser/string/reduce/shared.d.ts +16 -16
- package/out/parser/string/reduce/shared.js +4 -2
- package/out/parser/string/shift/operand/date.d.ts +2 -2
- package/out/parser/string/shift/operand/enclosed.d.ts +17 -3
- package/out/parser/string/shift/operand/enclosed.js +2 -1
- package/out/parser/string/shift/operand/genericArgs.d.ts +4 -4
- package/out/parser/string/shift/operand/unenclosed.d.ts +8 -6
- package/out/parser/string/shift/operand/unenclosed.js +2 -2
- package/out/parser/string/shift/operator/bounds.d.ts +5 -5
- package/out/parser/string/shift/operator/bounds.js +3 -3
- package/out/parser/string/shift/operator/divisor.d.ts +1 -1
- package/out/parser/string/shift/operator/operator.d.ts +1 -1
- package/out/parser/string/string.d.ts +2 -2
- package/out/parser/tuple.d.ts +11 -11
- package/out/parser/tuple.js +3 -3
- package/out/scope.d.ts +13 -11
- package/out/scope.js +5 -5
- package/out/type.d.ts +4 -4
- package/out/type.js +2 -2
- package/package.json +8 -10
- package/__tests__/array.test.ts +0 -391
- package/__tests__/badDefinitionType.test.ts +0 -58
- package/__tests__/basis.test.ts +0 -50
- package/__tests__/bounds.test.ts +0 -318
- package/__tests__/cast.test.ts +0 -37
- package/__tests__/completions.test.ts +0 -51
- package/__tests__/config.test.ts +0 -96
- package/__tests__/dateLiteral.test.ts +0 -36
- package/__tests__/declared.test.ts +0 -124
- package/__tests__/discrimination.test.ts +0 -74
- package/__tests__/divisor.test.ts +0 -114
- package/__tests__/enclosed.test.ts +0 -107
- package/__tests__/examples.test.ts +0 -56
- package/__tests__/expressions.test.ts +0 -210
- package/__tests__/generateBenchData.ts +0 -89
- package/__tests__/generated/cyclic.ts +0 -3058
- package/__tests__/generic.test.ts +0 -313
- package/__tests__/group.test.ts +0 -85
- package/__tests__/hkt.test.ts +0 -22
- package/__tests__/imports.test.ts +0 -126
- package/__tests__/instanceof.test.ts +0 -104
- package/__tests__/intersection.test.ts +0 -108
- package/__tests__/keyTraversal.test.ts +0 -77
- package/__tests__/keyof.test.ts +0 -150
- package/__tests__/keywords.test.ts +0 -214
- package/__tests__/literal.test.ts +0 -90
- package/__tests__/match.bench.ts +0 -31
- package/__tests__/match.test.ts +0 -234
- package/__tests__/methods.bench.ts +0 -22
- package/__tests__/narrow.test.ts +0 -100
- package/__tests__/object.bench.ts +0 -28
- package/__tests__/objectLiteral.test.ts +0 -442
- package/__tests__/operand.bench.ts +0 -45
- package/__tests__/operator.bench.ts +0 -130
- package/__tests__/pipe.test.ts +0 -501
- package/__tests__/realWorld.test.ts +0 -63
- package/__tests__/regex.test.ts +0 -68
- package/__tests__/scope.bench.ts +0 -39
- package/__tests__/scope.test.ts +0 -374
- package/__tests__/serialization.test.ts +0 -19
- package/__tests__/string.test.ts +0 -52
- package/__tests__/submodule.test.ts +0 -86
- package/__tests__/this.test.ts +0 -83
- package/__tests__/thunk.test.ts +0 -86
- package/__tests__/traverse.test.ts +0 -141
- package/__tests__/tsGenerics.test.ts +0 -12
- package/__tests__/type.test.ts +0 -36
- package/__tests__/typeReference.test.ts +0 -39
- package/__tests__/unenclosed.test.ts +0 -136
- package/__tests__/union.test.ts +0 -214
- package/ark.ts +0 -47
- package/generic.ts +0 -69
- package/match.ts +0 -218
- package/module.ts +0 -15
- package/out/ark.js.map +0 -1
- package/out/generic.js.map +0 -1
- package/out/main.js.map +0 -1
- package/out/match.js.map +0 -1
- package/out/module.js.map +0 -1
- package/out/parser/definition.js.map +0 -1
- package/out/parser/generic.js.map +0 -1
- package/out/parser/objectLiteral.js.map +0 -1
- package/out/parser/semantic/bounds.js.map +0 -1
- package/out/parser/semantic/divisor.js.map +0 -1
- package/out/parser/semantic/infer.js.map +0 -1
- package/out/parser/semantic/utils.js.map +0 -1
- package/out/parser/semantic/validate.js.map +0 -1
- package/out/parser/string/reduce/dynamic.js.map +0 -1
- package/out/parser/string/reduce/shared.js.map +0 -1
- package/out/parser/string/reduce/static.js.map +0 -1
- package/out/parser/string/shift/operand/date.js.map +0 -1
- package/out/parser/string/shift/operand/enclosed.js.map +0 -1
- package/out/parser/string/shift/operand/genericArgs.js.map +0 -1
- package/out/parser/string/shift/operand/operand.js.map +0 -1
- package/out/parser/string/shift/operand/unenclosed.js.map +0 -1
- package/out/parser/string/shift/operator/bounds.js.map +0 -1
- package/out/parser/string/shift/operator/divisor.js.map +0 -1
- package/out/parser/string/shift/operator/operator.js.map +0 -1
- package/out/parser/string/shift/scanner.js.map +0 -1
- package/out/parser/string/string.js.map +0 -1
- package/out/parser/tuple.js.map +0 -1
- package/out/scope.js.map +0 -1
- package/out/type.js.map +0 -1
- package/parser/definition.ts +0 -143
- package/parser/generic.ts +0 -66
- package/parser/objectLiteral.ts +0 -233
- package/parser/semantic/bounds.ts +0 -62
- package/parser/semantic/divisor.ts +0 -11
- package/parser/semantic/infer.ts +0 -141
- package/parser/semantic/utils.ts +0 -15
- package/parser/semantic/validate.ts +0 -117
- package/parser/string/reduce/dynamic.ts +0 -208
- package/parser/string/reduce/shared.ts +0 -99
- package/parser/string/reduce/static.ts +0 -296
- package/parser/string/shift/operand/date.ts +0 -55
- package/parser/string/shift/operand/enclosed.ts +0 -123
- package/parser/string/shift/operand/genericArgs.ts +0 -128
- package/parser/string/shift/operand/operand.ts +0 -42
- package/parser/string/shift/operand/unenclosed.ts +0 -217
- package/parser/string/shift/operator/bounds.ts +0 -270
- package/parser/string/shift/operator/divisor.ts +0 -33
- package/parser/string/shift/operator/operator.ts +0 -71
- package/parser/string/shift/scanner.ts +0 -210
- package/parser/string/string.ts +0 -85
- package/parser/tuple.ts +0 -542
- package/scope.ts +0 -291
- package/type.ts +0 -266
package/scope.ts
DELETED
|
@@ -1,291 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
type ArkConfig,
|
|
3
|
-
type GenericProps,
|
|
4
|
-
type PreparsedNodeResolution,
|
|
5
|
-
type PrivateDeclaration,
|
|
6
|
-
type RawSchema,
|
|
7
|
-
type RawSchemaResolutions,
|
|
8
|
-
RawSchemaScope,
|
|
9
|
-
type SchemaScope,
|
|
10
|
-
type UnknownSchema,
|
|
11
|
-
type ambient,
|
|
12
|
-
type arkKind,
|
|
13
|
-
type destructuredExportContext,
|
|
14
|
-
type destructuredImportContext,
|
|
15
|
-
type exportedNameOf,
|
|
16
|
-
hasArkKind,
|
|
17
|
-
type writeDuplicateAliasError
|
|
18
|
-
} from "@arktype/schema"
|
|
19
|
-
import {
|
|
20
|
-
type Dict,
|
|
21
|
-
domainOf,
|
|
22
|
-
hasDomain,
|
|
23
|
-
type isAnyOrNever,
|
|
24
|
-
isThunk,
|
|
25
|
-
type keyError,
|
|
26
|
-
type nominal,
|
|
27
|
-
type show,
|
|
28
|
-
throwParseError
|
|
29
|
-
} from "@arktype/util"
|
|
30
|
-
import type { type } from "./ark.js"
|
|
31
|
-
import { Generic } from "./generic.js"
|
|
32
|
-
import { type MatchParser, createMatchParser } from "./match.js"
|
|
33
|
-
import type { Module } from "./module.js"
|
|
34
|
-
import {
|
|
35
|
-
type inferDefinition,
|
|
36
|
-
parseObject,
|
|
37
|
-
type validateDefinition,
|
|
38
|
-
writeBadDefinitionTypeMessage
|
|
39
|
-
} from "./parser/definition.js"
|
|
40
|
-
import {
|
|
41
|
-
type GenericDeclaration,
|
|
42
|
-
type GenericParamsParseError,
|
|
43
|
-
parseGenericParams
|
|
44
|
-
} from "./parser/generic.js"
|
|
45
|
-
import { DynamicState } from "./parser/string/reduce/dynamic.js"
|
|
46
|
-
import { fullStringParse } from "./parser/string/string.js"
|
|
47
|
-
import {
|
|
48
|
-
type DeclarationParser,
|
|
49
|
-
type DefinitionParser,
|
|
50
|
-
RawTypeParser,
|
|
51
|
-
type Type,
|
|
52
|
-
type TypeParser
|
|
53
|
-
} from "./type.js"
|
|
54
|
-
|
|
55
|
-
export type ScopeParser = <const def>(
|
|
56
|
-
def: validateScope<def>,
|
|
57
|
-
config?: ArkConfig
|
|
58
|
-
) => Scope<inferBootstrapped<bootstrapAliases<def>>>
|
|
59
|
-
|
|
60
|
-
type validateScope<def> = {
|
|
61
|
-
[k in keyof def]: k extends symbol ?
|
|
62
|
-
// this should only occur when importing/exporting modules, and those
|
|
63
|
-
// keys should be ignored
|
|
64
|
-
unknown
|
|
65
|
-
: parseScopeKey<k>["params"] extends [] ?
|
|
66
|
-
// Not including Type here directly breaks inference
|
|
67
|
-
def[k] extends Type | PreparsedResolution ? def[k]
|
|
68
|
-
: k extends PrivateDeclaration<infer name extends keyof def & string> ?
|
|
69
|
-
keyError<writeDuplicateAliasError<name>>
|
|
70
|
-
: validateDefinition<def[k], ambient & bootstrapAliases<def>, {}>
|
|
71
|
-
: parseScopeKey<k>["params"] extends GenericParamsParseError ?
|
|
72
|
-
// use the full nominal type here to avoid an overlap between the
|
|
73
|
-
// error message and a possible value for the property
|
|
74
|
-
parseScopeKey<k>["params"][0]
|
|
75
|
-
: validateDefinition<
|
|
76
|
-
def[k],
|
|
77
|
-
ambient & bootstrapAliases<def>,
|
|
78
|
-
{
|
|
79
|
-
// once we support constraints on generic parameters, we'd use
|
|
80
|
-
// the base type here: https://github.com/arktypeio/arktype/issues/796
|
|
81
|
-
[param in parseScopeKey<k>["params"][number]]: unknown
|
|
82
|
-
}
|
|
83
|
-
>
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
export type bindThis<def> = { this: Def<def> }
|
|
87
|
-
|
|
88
|
-
/** nominal type for an unparsed definition used during scope bootstrapping */
|
|
89
|
-
type Def<def = {}> = nominal<def, "unparsed">
|
|
90
|
-
|
|
91
|
-
/** sentinel indicating a scope that will be associated with a generic has not yet been parsed */
|
|
92
|
-
export type UnparsedScope = "$"
|
|
93
|
-
|
|
94
|
-
/** These are legal as values of a scope but not as definitions in other contexts */
|
|
95
|
-
type PreparsedResolution = PreparsedNodeResolution
|
|
96
|
-
|
|
97
|
-
type bootstrapAliases<def> = {
|
|
98
|
-
[k in Exclude<
|
|
99
|
-
keyof def,
|
|
100
|
-
// avoid inferring nominal symbols, e.g. arkKind from Module
|
|
101
|
-
GenericDeclaration | symbol
|
|
102
|
-
>]: def[k] extends PreparsedResolution ? def[k]
|
|
103
|
-
: def[k] extends (() => infer thunkReturn extends PreparsedResolution) ?
|
|
104
|
-
thunkReturn
|
|
105
|
-
: Def<def[k]>
|
|
106
|
-
} & {
|
|
107
|
-
[k in keyof def & GenericDeclaration as extractGenericName<k>]: GenericProps<
|
|
108
|
-
parseGenericParams<extractGenericParameters<k>>,
|
|
109
|
-
def[k],
|
|
110
|
-
UnparsedScope
|
|
111
|
-
>
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
type inferBootstrapped<$> = show<{
|
|
115
|
-
[name in keyof $]: $[name] extends Def<infer def> ?
|
|
116
|
-
inferDefinition<def, $ & ambient, {}>
|
|
117
|
-
: $[name] extends GenericProps<infer params, infer def> ?
|
|
118
|
-
// add the scope in which the generic was defined here
|
|
119
|
-
Generic<params, def, $>
|
|
120
|
-
: // otherwise should be a submodule
|
|
121
|
-
$[name]
|
|
122
|
-
}>
|
|
123
|
-
|
|
124
|
-
type extractGenericName<k> =
|
|
125
|
-
k extends GenericDeclaration<infer name> ? name : never
|
|
126
|
-
|
|
127
|
-
type extractGenericParameters<k> =
|
|
128
|
-
k extends GenericDeclaration<string, infer params> ? params : never
|
|
129
|
-
|
|
130
|
-
export type resolve<reference extends keyof $ | keyof args, $, args> =
|
|
131
|
-
(
|
|
132
|
-
reference extends keyof args ?
|
|
133
|
-
args[reference]
|
|
134
|
-
: $[reference & keyof $]
|
|
135
|
-
) extends infer resolution ?
|
|
136
|
-
isAnyOrNever<resolution> extends true ? resolution
|
|
137
|
-
: resolution extends Def<infer def> ? inferDefinition<def, $, args>
|
|
138
|
-
: resolution
|
|
139
|
-
: never
|
|
140
|
-
|
|
141
|
-
export type moduleKeyOf<$> = {
|
|
142
|
-
// TODO: check against module directly?
|
|
143
|
-
[k in keyof $]: $[k] extends { [arkKind]: "module" } ? k & string : never
|
|
144
|
-
}[keyof $]
|
|
145
|
-
|
|
146
|
-
export type tryInferSubmoduleReference<$, token> =
|
|
147
|
-
token extends `${infer submodule extends moduleKeyOf<$>}.${infer subalias}` ?
|
|
148
|
-
subalias extends keyof $[submodule] ?
|
|
149
|
-
$[submodule][subalias] extends type.cast<infer t> ?
|
|
150
|
-
t
|
|
151
|
-
: never
|
|
152
|
-
: never
|
|
153
|
-
: never
|
|
154
|
-
|
|
155
|
-
export interface ParseContext {
|
|
156
|
-
$: RawScope
|
|
157
|
-
args?: Record<string, UnknownSchema>
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
export const scope: ScopeParser = ((def: Dict, config: ArkConfig = {}) =>
|
|
161
|
-
new RawScope(def, config)) as never
|
|
162
|
-
|
|
163
|
-
export interface Scope<$ = any> extends SchemaScope<$> {
|
|
164
|
-
type: TypeParser<$>
|
|
165
|
-
|
|
166
|
-
match: MatchParser<$>
|
|
167
|
-
|
|
168
|
-
declare: DeclarationParser<$>
|
|
169
|
-
|
|
170
|
-
define: DefinitionParser<$>
|
|
171
|
-
|
|
172
|
-
import<names extends exportedNameOf<$>[]>(
|
|
173
|
-
...names: names
|
|
174
|
-
): Module<show<destructuredImportContext<$, names>>>
|
|
175
|
-
|
|
176
|
-
export<names extends exportedNameOf<$>[]>(
|
|
177
|
-
...names: names
|
|
178
|
-
): Module<show<destructuredExportContext<$, names>>>
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
export class RawScope<
|
|
182
|
-
$ extends RawSchemaResolutions = RawSchemaResolutions
|
|
183
|
-
> extends RawSchemaScope<$> {
|
|
184
|
-
private parseCache: Record<string, RawSchema> = {}
|
|
185
|
-
|
|
186
|
-
constructor(def: Record<string, unknown>, config?: ArkConfig) {
|
|
187
|
-
const aliases: Record<string, unknown> = {}
|
|
188
|
-
for (const k in def) {
|
|
189
|
-
const parsedKey = parseScopeKey(k)
|
|
190
|
-
aliases[parsedKey.name] =
|
|
191
|
-
parsedKey.params.length ?
|
|
192
|
-
// TODO: this
|
|
193
|
-
new Generic(parsedKey.params, def[k], {} as never)
|
|
194
|
-
: def[k]
|
|
195
|
-
}
|
|
196
|
-
super(aliases, config)
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
type = new RawTypeParser(this as never)
|
|
200
|
-
|
|
201
|
-
match: MatchParser<$> = createMatchParser(this as never) as never
|
|
202
|
-
|
|
203
|
-
declare = (() => ({
|
|
204
|
-
type: this.type
|
|
205
|
-
})).bind(this)
|
|
206
|
-
|
|
207
|
-
define = ((def: unknown) => def).bind(this)
|
|
208
|
-
|
|
209
|
-
override preparseRoot(def: unknown): unknown {
|
|
210
|
-
if (isThunk(def) && !hasArkKind(def, "generic")) return def()
|
|
211
|
-
|
|
212
|
-
return def
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
override parseRoot(def: unknown): RawSchema {
|
|
216
|
-
// args: { this: {} as RawSchema },
|
|
217
|
-
return this.parse(def, {
|
|
218
|
-
$: this as never,
|
|
219
|
-
args: {}
|
|
220
|
-
// type parsing can bypass nodes if it hits the cache,
|
|
221
|
-
// so bind it directly (could be optimized)
|
|
222
|
-
}).bindScope(this)
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
parse(def: unknown, ctx: ParseContext): RawSchema {
|
|
226
|
-
if (typeof def === "string") {
|
|
227
|
-
if (ctx.args && Object.keys(ctx.args).every(k => !def.includes(k))) {
|
|
228
|
-
// we can only rely on the cache if there are no contextual
|
|
229
|
-
// resolutions like "this" or generic args
|
|
230
|
-
return this.parseString(def, ctx)
|
|
231
|
-
}
|
|
232
|
-
if (!this.parseCache[def])
|
|
233
|
-
this.parseCache[def] = this.parseString(def, ctx)
|
|
234
|
-
|
|
235
|
-
return this.parseCache[def]
|
|
236
|
-
}
|
|
237
|
-
return hasDomain(def, "object") ?
|
|
238
|
-
parseObject(def, ctx)
|
|
239
|
-
: throwParseError(writeBadDefinitionTypeMessage(domainOf(def)))
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
parseString(def: string, ctx: ParseContext): RawSchema {
|
|
243
|
-
return (
|
|
244
|
-
this.maybeResolveSchema(def) ??
|
|
245
|
-
((def.endsWith("[]") &&
|
|
246
|
-
this.maybeResolveSchema(def.slice(0, -2))?.array()) ||
|
|
247
|
-
fullStringParse(new DynamicState(def, ctx)))
|
|
248
|
-
)
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
export const writeShallowCycleErrorMessage = (
|
|
253
|
-
name: string,
|
|
254
|
-
seen: string[]
|
|
255
|
-
): string =>
|
|
256
|
-
`Alias '${name}' has a shallow resolution cycle: ${[...seen, name].join(":")}`
|
|
257
|
-
|
|
258
|
-
export type ParsedScopeKey = {
|
|
259
|
-
name: string
|
|
260
|
-
params: string[]
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
export const parseScopeKey = (k: string): ParsedScopeKey => {
|
|
264
|
-
const firstParamIndex = k.indexOf("<")
|
|
265
|
-
if (firstParamIndex === -1) {
|
|
266
|
-
return {
|
|
267
|
-
name: k,
|
|
268
|
-
params: []
|
|
269
|
-
}
|
|
270
|
-
}
|
|
271
|
-
if (k.at(-1) !== ">") {
|
|
272
|
-
throwParseError(
|
|
273
|
-
`'>' must be the last character of a generic declaration in a scope`
|
|
274
|
-
)
|
|
275
|
-
}
|
|
276
|
-
return {
|
|
277
|
-
name: k.slice(0, firstParamIndex),
|
|
278
|
-
params: parseGenericParams(k.slice(firstParamIndex + 1, -1))
|
|
279
|
-
}
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
export type parseScopeKey<k> =
|
|
283
|
-
k extends GenericDeclaration<infer name, infer paramString> ?
|
|
284
|
-
{
|
|
285
|
-
name: name
|
|
286
|
-
params: parseGenericParams<paramString>
|
|
287
|
-
}
|
|
288
|
-
: {
|
|
289
|
-
name: k
|
|
290
|
-
params: []
|
|
291
|
-
}
|
package/type.ts
DELETED
|
@@ -1,266 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
ArkErrors,
|
|
3
|
-
type BaseMeta,
|
|
4
|
-
type BaseRoot,
|
|
5
|
-
type Disjoint,
|
|
6
|
-
type Morph,
|
|
7
|
-
type NodeDef,
|
|
8
|
-
type Out,
|
|
9
|
-
type Predicate,
|
|
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,
|
|
19
|
-
type includesMorphs,
|
|
20
|
-
type inferIntersection,
|
|
21
|
-
type inferMorphOut,
|
|
22
|
-
type inferNarrow,
|
|
23
|
-
type inferPipes,
|
|
24
|
-
type internalImplementationOf
|
|
25
|
-
} from "@arktype/schema"
|
|
26
|
-
import {
|
|
27
|
-
Callable,
|
|
28
|
-
type Constructor,
|
|
29
|
-
type array,
|
|
30
|
-
type conform
|
|
31
|
-
} from "@arktype/util"
|
|
32
|
-
import { Generic, type validateParameterString } from "./generic.js"
|
|
33
|
-
import type {
|
|
34
|
-
inferDefinition,
|
|
35
|
-
validateDeclared,
|
|
36
|
-
validateDefinition
|
|
37
|
-
} from "./parser/definition.js"
|
|
38
|
-
import { parseGenericParams } from "./parser/generic.js"
|
|
39
|
-
import type {
|
|
40
|
-
IndexOneOperator,
|
|
41
|
-
IndexZeroOperator,
|
|
42
|
-
TupleInfixOperator
|
|
43
|
-
} from "./parser/tuple.js"
|
|
44
|
-
import type { RawScope, Scope, bindThis } from "./scope.js"
|
|
45
|
-
|
|
46
|
-
export interface TypeParserAttachments {
|
|
47
|
-
errors: typeof ArkErrors
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
export interface TypeParser<$> extends TypeParserAttachments {
|
|
51
|
-
// Parse and check the definition, returning either the original input for a
|
|
52
|
-
// valid definition or a string representing an error message.
|
|
53
|
-
<const def>(def: validateTypeRoot<def, $>): Type<inferTypeRoot<def, $>, $>
|
|
54
|
-
|
|
55
|
-
// Spread version of a tuple expression
|
|
56
|
-
<const zero, const one, const rest extends array>(
|
|
57
|
-
_0: zero extends IndexZeroOperator ? zero : validateTypeRoot<zero, $>,
|
|
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
|
-
: []
|
|
70
|
-
): Type<inferTypeRoot<[zero, one, ...rest], $>, $>
|
|
71
|
-
|
|
72
|
-
<params extends string, const def>(
|
|
73
|
-
params: `<${validateParameterString<params>}>`,
|
|
74
|
-
def: validateDefinition<
|
|
75
|
-
def,
|
|
76
|
-
$,
|
|
77
|
-
{
|
|
78
|
-
[param in parseGenericParams<params>[number]]: unknown
|
|
79
|
-
}
|
|
80
|
-
>
|
|
81
|
-
): Generic<parseGenericParams<params>, def, $>
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
const typeParserAttachments = Object.freeze({
|
|
85
|
-
errors: ArkErrors
|
|
86
|
-
} satisfies TypeParserAttachments)
|
|
87
|
-
|
|
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
|
-
)
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
|
|
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
|
-
}
|
|
128
|
-
|
|
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<$>;
|
|
137
|
-
|
|
138
|
-
get in(): Type<this["tIn"], $>
|
|
139
|
-
get out(): Type<this["tOut"], $>
|
|
140
|
-
|
|
141
|
-
intersect<def>(
|
|
142
|
-
def: validateTypeRoot<def, $>
|
|
143
|
-
): Type<inferIntersection<t, inferTypeRoot<def, $>>> | Disjoint
|
|
144
|
-
|
|
145
|
-
and<def>(
|
|
146
|
-
def: validateTypeRoot<def, $>
|
|
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]>, $>
|
|
210
|
-
|
|
211
|
-
// TODO: based on below, should maybe narrow morph output if used after
|
|
212
|
-
narrow<def extends Predicate<distillConstrainableOut<t>>>(
|
|
213
|
-
def: def
|
|
214
|
-
): Type<
|
|
215
|
-
includesMorphs<t> extends true ?
|
|
216
|
-
(In: this["tIn"]) => Out<inferNarrow<this["infer"], def>>
|
|
217
|
-
: inferNarrow<this["infer"], def>,
|
|
218
|
-
$
|
|
219
|
-
>
|
|
220
|
-
|
|
221
|
-
equals<def>(
|
|
222
|
-
def: validateTypeRoot<def, $>
|
|
223
|
-
): this is Type<inferTypeRoot<def>, $>
|
|
224
|
-
|
|
225
|
-
// TODO: i/o
|
|
226
|
-
extract<def>(r: validateTypeRoot<def, $>): Type<t, $>
|
|
227
|
-
exclude<def>(r: validateTypeRoot<def, $>): Type<t, $>
|
|
228
|
-
extends<def>(
|
|
229
|
-
other: validateTypeRoot<def, $>
|
|
230
|
-
): this is Type<inferTypeRoot<def>, $>
|
|
231
|
-
|
|
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>, $>
|
|
239
|
-
}
|
|
240
|
-
|
|
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, $> {}
|
|
246
|
-
|
|
247
|
-
export type TypeConstructor<t = unknown, $ = any> = new (
|
|
248
|
-
def: unknown,
|
|
249
|
-
$: Scope<$>
|
|
250
|
-
) => Type<t, $>
|
|
251
|
-
|
|
252
|
-
export const Type: TypeConstructor = RawSchema as never
|
|
253
|
-
|
|
254
|
-
export type DefinitionParser<$> = <def>(def: validateTypeRoot<def, $>) => def
|
|
255
|
-
|
|
256
|
-
export type validateTypeRoot<def, $ = {}> = validateDefinition<
|
|
257
|
-
def,
|
|
258
|
-
$ & ambient,
|
|
259
|
-
bindThis<def>
|
|
260
|
-
>
|
|
261
|
-
|
|
262
|
-
export type inferTypeRoot<def, $ = {}> = inferDefinition<
|
|
263
|
-
def,
|
|
264
|
-
$ & ambient,
|
|
265
|
-
bindThis<def>
|
|
266
|
-
>
|