arktype 2.0.4 → 2.1.1
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/README.md +123 -93
- package/out/attributes.d.ts +2 -2
- package/out/config.d.ts +17 -5
- package/out/config.js +2 -1
- package/out/index.d.ts +2 -2
- package/out/index.js +1 -1
- package/out/keywords/Array.js +2 -0
- package/out/keywords/FormData.js +2 -0
- package/out/keywords/TypedArray.js +2 -0
- package/out/keywords/keywords.d.ts +9 -4
- package/out/keywords/keywords.js +27 -11
- package/out/keywords/number.js +2 -0
- package/out/keywords/string.d.ts +20 -17
- package/out/keywords/string.js +43 -1
- package/out/keywords/ts.js +8 -2
- package/out/match.d.ts +101 -54
- package/out/match.js +70 -50
- package/out/methods/base.d.ts +75 -54
- package/out/methods/instantiate.d.ts +3 -8
- package/out/methods/object.d.ts +5 -5
- package/out/parser/definition.js +1 -1
- package/out/parser/property.d.ts +1 -3
- package/out/parser/shift/scanner.d.ts +1 -1
- package/out/parser/tupleExpressions.d.ts +50 -14
- package/out/parser/tupleExpressions.js +20 -9
- package/out/scope.d.ts +14 -6
- package/out/scope.js +18 -9
- package/out/type.d.ts +7 -4
- package/out/type.js +1 -0
- package/package.json +8 -5
- package/out/methods/morph.d.ts +0 -12
- package/out/methods/morph.js +0 -1
package/out/methods/base.d.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { BaseRoot, Disjoint, JsonSchema, Morph, Predicate, StandardSchemaV1, UndeclaredKeyBehavior } from "@ark/schema";
|
|
2
2
|
import type { anyOrNever, array, Callable, ErrorMessage, inferred, JsonStructure, unset } from "@ark/util";
|
|
3
|
-
import type { defaultFor, distill, inferIntersection, inferMorphOut, inferPipes, InferredMorph, Out, To } from "../attributes.ts";
|
|
4
|
-
import type { ArkAmbient } from "../config.ts";
|
|
3
|
+
import type { defaultFor, distill, inferIntersection, inferMorphOut, inferPipe, inferPipes, InferredMorph, Out, To } from "../attributes.ts";
|
|
4
|
+
import type { ArkAmbient, TypeMetaInput } from "../config.ts";
|
|
5
5
|
import type { type } from "../keywords/keywords.ts";
|
|
6
6
|
import type { Scope } from "../scope.ts";
|
|
7
7
|
import type { ArrayType } from "./array.ts";
|
|
8
8
|
import type { instantiateType } from "./instantiate.ts";
|
|
9
9
|
/** @ts-ignore cast variance */
|
|
10
|
-
interface
|
|
10
|
+
export interface Inferred<out t = unknown, $ = {}> {
|
|
11
11
|
internal: BaseRoot;
|
|
12
12
|
[inferred]: t;
|
|
13
13
|
/**
|
|
@@ -174,7 +174,7 @@ interface Type<out t = unknown, $ = {}> extends Callable<(data: unknown) => dist
|
|
|
174
174
|
* // error message at root is affected, leading to a misleading description
|
|
175
175
|
* const nonObject = notOddBox(null) // must be not odd (was null)
|
|
176
176
|
*/
|
|
177
|
-
configure(meta:
|
|
177
|
+
configure<meta extends TypeMetaInput>(meta: meta): this;
|
|
178
178
|
/**
|
|
179
179
|
* #### add description to shallow references
|
|
180
180
|
*
|
|
@@ -252,16 +252,6 @@ interface Type<out t = unknown, $ = {}> extends Callable<(data: unknown) => dist
|
|
|
252
252
|
* const baseOut = base.out
|
|
253
253
|
*/
|
|
254
254
|
get out(): instantiateType<this["inferIntrospectableOut"], $>;
|
|
255
|
-
/**
|
|
256
|
-
* #### cast the way this is inferred
|
|
257
|
-
*
|
|
258
|
-
* 🥸 inference-only function that does nothing runtime
|
|
259
|
-
*
|
|
260
|
-
* @example
|
|
261
|
-
* // Type<`LEEEEEEEE${string}ROY`>
|
|
262
|
-
* const leeroy = type(/^LE{8,}ROY$/).as<`LEEEEEEEE${string}ROY`>()
|
|
263
|
-
*/
|
|
264
|
-
as<castTo = unset>(...args: validateChainedAsArgs<castTo>): instantiateType<castTo, $>;
|
|
265
255
|
/**
|
|
266
256
|
* #### add a compile-time brand to output
|
|
267
257
|
*
|
|
@@ -274,27 +264,7 @@ interface Type<out t = unknown, $ = {}> extends Callable<(data: unknown) => dist
|
|
|
274
264
|
* // Brand<string, "palindrome">
|
|
275
265
|
* const out = palindrome.assert("racecar")
|
|
276
266
|
*/
|
|
277
|
-
brand<const name extends string, r = type.brand<t, name
|
|
278
|
-
/**
|
|
279
|
-
* #### intersect the parsed Type, throwing if the result is unsatisfiable
|
|
280
|
-
*
|
|
281
|
-
* @example
|
|
282
|
-
* // Type<{ foo: number; bar: string }>
|
|
283
|
-
* const t = type({ foo: "number" }).and({ bar: "string" })
|
|
284
|
-
* // ParseError: Intersection at foo of number and string results in an unsatisfiable type
|
|
285
|
-
* const bad = type({ foo: "number" }).and({ foo: "string" })
|
|
286
|
-
*/
|
|
287
|
-
and<const def, r = type.infer<def, $>>(def: type.validate<def, $>): instantiateType<inferIntersection<t, r>, $>;
|
|
288
|
-
/**
|
|
289
|
-
* #### union with the parsed Type
|
|
290
|
-
*
|
|
291
|
-
* ⚠️ a union that could apply different morphs to the same data is a ParseError ([docs](https://arktype.io/docs/expressions/union-morphs))
|
|
292
|
-
*
|
|
293
|
-
* @example
|
|
294
|
-
* // Type<string | { box: string }>
|
|
295
|
-
* const t = type("string").or({ box: "string" })
|
|
296
|
-
*/
|
|
297
|
-
or<const def, r = type.infer<def, $>>(def: type.validate<def, $>): instantiateType<t | r, $>;
|
|
267
|
+
brand<const name extends string, r = instantiateType<type.brand<t, name>, $>>(name: name): r extends infer _ ? _ : never;
|
|
298
268
|
/**
|
|
299
269
|
* #### an array of this
|
|
300
270
|
*
|
|
@@ -345,11 +315,13 @@ interface Type<out t = unknown, $ = {}> extends Callable<(data: unknown) => dist
|
|
|
345
315
|
* s.endsWith("Z")
|
|
346
316
|
* )
|
|
347
317
|
*/
|
|
348
|
-
filter<narrowed extends this["inferIn"] = never, r =
|
|
318
|
+
filter<narrowed extends this["inferIn"] = never, r = instantiateType<[
|
|
319
|
+
narrowed
|
|
320
|
+
] extends [never] ? t : t extends InferredMorph<any, infer o> ? (In: narrowed) => o : narrowed, $>>(predicate: Predicate.Castable<this["inferIn"], narrowed>): r extends infer _ ? _ : never;
|
|
349
321
|
/**
|
|
350
322
|
* #### apply a predicate function to output
|
|
351
323
|
*
|
|
352
|
-
* ✅ go-to fallback for validation not composable via
|
|
324
|
+
* ✅ go-to fallback for validation not composable via builtin types and operators
|
|
353
325
|
* ✅ runs after all other validators and morphs, if present
|
|
354
326
|
* 🥸 {@link https://www.typescriptlang.org/docs/handbook/2/narrowing.html#using-type-predicates | Type predicates} can be used as casts
|
|
355
327
|
*
|
|
@@ -362,13 +334,62 @@ interface Type<out t = unknown, $ = {}> extends Callable<(data: unknown) => dist
|
|
|
362
334
|
* // Type<`${string}.tsx`>
|
|
363
335
|
* const withPredicate = type("string").narrow((s): s is `${string}.tsx` => /\.tsx?$/.test(s))
|
|
364
336
|
*/
|
|
365
|
-
narrow<narrowed extends this["infer"] = never, r =
|
|
337
|
+
narrow<narrowed extends this["infer"] = never, r = instantiateType<[
|
|
338
|
+
narrowed
|
|
339
|
+
] extends [never] ? t : t extends InferredMorph<infer i, infer o> ? o extends To ? (In: i) => To<narrowed> : (In: i) => Out<narrowed> : narrowed, $>>(predicate: Predicate.Castable<this["infer"], narrowed>): r extends infer _ ? _ : never;
|
|
366
340
|
/**
|
|
367
|
-
*
|
|
368
|
-
*
|
|
369
|
-
* @example
|
|
341
|
+
* #### pipe output through arbitrary transformations or other Types
|
|
342
|
+
*
|
|
343
|
+
* @example
|
|
344
|
+
* const user = type({ name: "string" })
|
|
345
|
+
*
|
|
346
|
+
* // parse a string and validate that the result as a user
|
|
347
|
+
* const parseUser = type("string").pipe(s => JSON.parse(s), user)
|
|
370
348
|
*/
|
|
371
349
|
pipe: ChainedPipe<t, $>;
|
|
350
|
+
/**
|
|
351
|
+
* #### parse a definition as an output validator
|
|
352
|
+
*
|
|
353
|
+
* 🔗 `to({ name: "string" })` is equivalent to `.pipe(type({ name: "string" }))`
|
|
354
|
+
*
|
|
355
|
+
* @example
|
|
356
|
+
* // parse a string and validate that the result as a user
|
|
357
|
+
* const parseUser = type("string").pipe(s => JSON.parse(s)).to({ name: "string" })
|
|
358
|
+
*/
|
|
359
|
+
to<const def, r = instantiateType<inferPipe<t, type.infer<def, $>>, $>>(def: type.validate<def, $>): r extends infer _ ? _ : never;
|
|
360
|
+
}
|
|
361
|
+
/** @ts-ignore cast variance */
|
|
362
|
+
interface Type<out t = unknown, $ = {}> extends Callable<(data: unknown) => distill.Out<t> | ArkEnv.onFail>, Inferred<t, $> {
|
|
363
|
+
/**
|
|
364
|
+
* #### cast the way this is inferred
|
|
365
|
+
*
|
|
366
|
+
* 🥸 inference-only function that does nothing runtime
|
|
367
|
+
*
|
|
368
|
+
* @example
|
|
369
|
+
* // Type<`LEEEEEEEE${string}ROY`>
|
|
370
|
+
* const leeroy = type(/^LE{8,}ROY$/).as<`LEEEEEEEE${string}ROY`>()
|
|
371
|
+
*/
|
|
372
|
+
as<castTo = unset>(...args: validateChainedAsArgs<castTo>): instantiateType<castTo, $>;
|
|
373
|
+
/**
|
|
374
|
+
* #### intersect the parsed Type, throwing if the result is unsatisfiable
|
|
375
|
+
*
|
|
376
|
+
* @example
|
|
377
|
+
* // Type<{ foo: number; bar: string }>
|
|
378
|
+
* const t = type({ foo: "number" }).and({ bar: "string" })
|
|
379
|
+
* // ParseError: Intersection at foo of number and string results in an unsatisfiable type
|
|
380
|
+
* const bad = type({ foo: "number" }).and({ foo: "string" })
|
|
381
|
+
*/
|
|
382
|
+
and<const def, r = instantiateType<inferIntersection<t, type.infer<def, $>>, $>>(def: type.validate<def, $>): r extends infer _ ? _ : never;
|
|
383
|
+
/**
|
|
384
|
+
* #### union with the parsed Type
|
|
385
|
+
*
|
|
386
|
+
* ⚠️ a union that could apply different morphs to the same data is a ParseError ([docs](https://arktype.io/docs/expressions/union-morphs))
|
|
387
|
+
*
|
|
388
|
+
* @example
|
|
389
|
+
* // Type<string | { box: string }>
|
|
390
|
+
* const t = type("string").or({ box: "string" })
|
|
391
|
+
*/
|
|
392
|
+
or<const def, r = instantiateType<t | type.infer<def, $>, $>>(def: type.validate<def, $>): r extends infer _ ? _ : never;
|
|
372
393
|
/**
|
|
373
394
|
* #### intersect the parsed Type, returning an introspectable {@link Disjoint} if the result is unsatisfiable
|
|
374
395
|
*
|
|
@@ -379,7 +400,7 @@ interface Type<out t = unknown, $ = {}> extends Callable<(data: unknown) => dist
|
|
|
379
400
|
* // logs "Intersection of > 10 and < 5 results in an unsatisfiable type"
|
|
380
401
|
* if (bad instanceof Disjoint) console.log(`${bad.summary}`)
|
|
381
402
|
*/
|
|
382
|
-
intersect<const def, r = type.infer<def, $>>(def: type.validate<def, $>):
|
|
403
|
+
intersect<const def, r = instantiateType<inferIntersection<t, type.infer<def, $>>, $>>(def: type.validate<def, $>): r extends infer _ ? _ | Disjoint : never;
|
|
383
404
|
/**
|
|
384
405
|
* #### check if the parsed Type's constraints are identical
|
|
385
406
|
*
|
|
@@ -407,7 +428,7 @@ interface Type<out t = unknown, $ = {}> extends Callable<(data: unknown) => dist
|
|
|
407
428
|
* // Type<0.5> | undefined
|
|
408
429
|
* const ez = n.ifEquals("0.5")
|
|
409
430
|
*/
|
|
410
|
-
ifEquals<const def, r = type.
|
|
431
|
+
ifEquals<const def, r = type.instantiate<def, $>>(def: type.validate<def, $>): r extends infer _ ? _ | undefined : never;
|
|
411
432
|
/**
|
|
412
433
|
* #### check if this is a subtype of the parsed Type
|
|
413
434
|
*
|
|
@@ -429,7 +450,7 @@ interface Type<out t = unknown, $ = {}> extends Callable<(data: unknown) => dist
|
|
|
429
450
|
* const n = type(Math.random() > 0.5 ? "true" : "0") // Type<0 | true>
|
|
430
451
|
* const ez = n.ifExtends("boolean") // Type<true> | undefined
|
|
431
452
|
*/
|
|
432
|
-
ifExtends<const def, r = type.
|
|
453
|
+
ifExtends<const def, r = type.instantiate<def, $>>(other: type.validate<def, $>): r extends infer _ ? _ | undefined : never;
|
|
433
454
|
/**
|
|
434
455
|
* #### check if a value could satisfy this and the parsed Type
|
|
435
456
|
*
|
|
@@ -451,7 +472,7 @@ interface Type<out t = unknown, $ = {}> extends Callable<(data: unknown) => dist
|
|
|
451
472
|
* // Type<true | 0 | 2>
|
|
452
473
|
* const t = type("boolean | 0 | 'one' | 2 | bigint").extract("number | 0n | true")
|
|
453
474
|
*/
|
|
454
|
-
extract<const def, r = type.infer<def, $>>(r: type.validate<def, $>):
|
|
475
|
+
extract<const def, r = instantiateType<t extends type.infer<def, $> ? t : never, $>>(r: type.validate<def, $>): r extends infer _ extends r ? _ : never;
|
|
455
476
|
/**
|
|
456
477
|
* #### exclude branches {@link extend}ing the parsed Type
|
|
457
478
|
*
|
|
@@ -460,7 +481,7 @@ interface Type<out t = unknown, $ = {}> extends Callable<(data: unknown) => dist
|
|
|
460
481
|
* // Type<false | 'one' | bigint>
|
|
461
482
|
* const t = type("boolean | 0 | 'one' | 2 | bigint").exclude("number | 0n | true")
|
|
462
483
|
*/
|
|
463
|
-
exclude<const def, r = type.infer<def, $>>(r: type.validate<def, $>):
|
|
484
|
+
exclude<const def, r = instantiateType<t extends type.infer<def, $> ? never : t, $>>(r: type.validate<def, $>): r extends infer _ ? _ : never;
|
|
464
485
|
/**
|
|
465
486
|
* @experimental
|
|
466
487
|
* Map and optionally reduce branches of a union. Types that are not unions
|
|
@@ -497,13 +518,13 @@ interface Type<out t = unknown, $ = {}> extends Callable<(data: unknown) => dist
|
|
|
497
518
|
Symbol: never;
|
|
498
519
|
}
|
|
499
520
|
interface ChainedPipeSignature<t, $> {
|
|
500
|
-
<a extends Morph<distill.Out<t>>, r = instantiateType<inferPipes<t, [a]>, $>>(a: a):
|
|
501
|
-
<a extends Morph<distill.Out<t>>, b extends Morph<inferMorphOut<a>>, r = instantiateType<inferPipes<t, [a, b]>, $>>(a: a, b: b):
|
|
502
|
-
<a extends Morph<distill.Out<t>>, b extends Morph<inferMorphOut<a>>, c extends Morph<inferMorphOut<b>>, r = instantiateType<inferPipes<t, [a, b, c]>, $>>(a: a, b: b, c: c):
|
|
503
|
-
<a extends Morph<distill.Out<t>>, b extends Morph<inferMorphOut<a>>, c extends Morph<inferMorphOut<b>>, d extends Morph<inferMorphOut<c>>, r = instantiateType<inferPipes<t, [a, b, c, d]>, $>>(a: a, b: b, c: c, d: d):
|
|
504
|
-
<a extends Morph<distill.Out<t>>, b extends Morph<inferMorphOut<a>>, c extends Morph<inferMorphOut<b>>, d extends Morph<inferMorphOut<c>>, e extends Morph<inferMorphOut<d>>, r = instantiateType<inferPipes<t, [a, b, c, d, e]>, $>>(a: a, b: b, c: c, d: d, e: e):
|
|
505
|
-
<a extends Morph<distill.Out<t>>, b extends Morph<inferMorphOut<a>>, c extends Morph<inferMorphOut<b>>, d extends Morph<inferMorphOut<c>>, e extends Morph<inferMorphOut<d>>, f extends Morph<inferMorphOut<e>>, r = instantiateType<inferPipes<t, [a, b, c, d, e, f]>, $>>(a: a, b: b, c: c, d: d, e: e, f: f):
|
|
506
|
-
<a extends Morph<distill.Out<t>>, b extends Morph<inferMorphOut<a>>, c extends Morph<inferMorphOut<b>>, d extends Morph<inferMorphOut<c>>, e extends Morph<inferMorphOut<d>>, f extends Morph<inferMorphOut<e>>, g extends Morph<inferMorphOut<f>>, r = instantiateType<inferPipes<t, [a, b, c, d, e, f, g]>, $>>(a: a, b: b, c: c, d: d, e: e, f: f, g: g):
|
|
521
|
+
<a extends Morph<distill.Out<t>>, r = instantiateType<inferPipes<t, [a]>, $>>(a: a): r extends infer _ ? _ : never;
|
|
522
|
+
<a extends Morph<distill.Out<t>>, b extends Morph<inferMorphOut<a>>, r = instantiateType<inferPipes<t, [a, b]>, $>>(a: a, b: b): r extends infer _ ? _ : never;
|
|
523
|
+
<a extends Morph<distill.Out<t>>, b extends Morph<inferMorphOut<a>>, c extends Morph<inferMorphOut<b>>, r = instantiateType<inferPipes<t, [a, b, c]>, $>>(a: a, b: b, c: c): r extends infer _ ? _ : never;
|
|
524
|
+
<a extends Morph<distill.Out<t>>, b extends Morph<inferMorphOut<a>>, c extends Morph<inferMorphOut<b>>, d extends Morph<inferMorphOut<c>>, r = instantiateType<inferPipes<t, [a, b, c, d]>, $>>(a: a, b: b, c: c, d: d): r extends infer _ ? _ : never;
|
|
525
|
+
<a extends Morph<distill.Out<t>>, b extends Morph<inferMorphOut<a>>, c extends Morph<inferMorphOut<b>>, d extends Morph<inferMorphOut<c>>, e extends Morph<inferMorphOut<d>>, r = instantiateType<inferPipes<t, [a, b, c, d, e]>, $>>(a: a, b: b, c: c, d: d, e: e): r extends infer _ ? _ : never;
|
|
526
|
+
<a extends Morph<distill.Out<t>>, b extends Morph<inferMorphOut<a>>, c extends Morph<inferMorphOut<b>>, d extends Morph<inferMorphOut<c>>, e extends Morph<inferMorphOut<d>>, f extends Morph<inferMorphOut<e>>, r = instantiateType<inferPipes<t, [a, b, c, d, e, f]>, $>>(a: a, b: b, c: c, d: d, e: e, f: f): r extends infer _ ? _ : never;
|
|
527
|
+
<a extends Morph<distill.Out<t>>, b extends Morph<inferMorphOut<a>>, c extends Morph<inferMorphOut<b>>, d extends Morph<inferMorphOut<c>>, e extends Morph<inferMorphOut<d>>, f extends Morph<inferMorphOut<e>>, g extends Morph<inferMorphOut<f>>, r = instantiateType<inferPipes<t, [a, b, c, d, e, f, g]>, $>>(a: a, b: b, c: c, d: d, e: e, f: f, g: g): r extends infer _ ? _ : never;
|
|
507
528
|
}
|
|
508
529
|
export interface ChainedPipe<t, $> extends ChainedPipeSignature<t, $> {
|
|
509
530
|
try: ChainedPipeSignature<t, $>;
|
|
@@ -1,17 +1,12 @@
|
|
|
1
1
|
import type { anyOrNever, array } from "@ark/util";
|
|
2
|
-
import type { InferredMorph } from "../attributes.ts";
|
|
3
2
|
import type { ArrayType } from "./array.ts";
|
|
4
3
|
import type { BaseType } from "./base.ts";
|
|
5
4
|
import type { DateType } from "./date.ts";
|
|
6
|
-
import type { MorphType } from "./morph.ts";
|
|
7
5
|
import type { NumberType } from "./number.ts";
|
|
8
6
|
import type { ObjectType } from "./object.ts";
|
|
9
7
|
import type { StringType } from "./string.ts";
|
|
10
|
-
export type instantiateType<t, $> =
|
|
11
|
-
export type _instantiateType<t, $> = [
|
|
12
|
-
Extract<t, InferredMorph>
|
|
13
|
-
] extends [anyOrNever] ? [
|
|
8
|
+
export type instantiateType<t, $> = [
|
|
14
9
|
t
|
|
15
|
-
] extends [anyOrNever] ? BaseType<t, $> : [t] extends [
|
|
10
|
+
] extends [anyOrNever] ? BaseType<t, $> : [t] extends [object] ? [
|
|
16
11
|
t
|
|
17
|
-
] extends [array] ? ArrayType<t, $> : [t] extends [Date] ? DateType<t, $> : ObjectType<t, $> :
|
|
12
|
+
] extends [array] ? ArrayType<t, $> : [t] extends [Date] ? DateType<t, $> : ObjectType<t, $> : [t] extends [string] ? StringType<t, $> : [t] extends [number] ? NumberType<t, $> : BaseType<t, $>;
|
package/out/methods/object.d.ts
CHANGED
|
@@ -17,9 +17,9 @@ interface Type<out t extends object = object, $ = {}> extends BaseType<t, $> {
|
|
|
17
17
|
* Get the `Type` of a property of this `Type<object>`.
|
|
18
18
|
* @example type({ foo: "string" }).get("foo") // Type<string>
|
|
19
19
|
*/
|
|
20
|
-
get<const k1 extends arkIndexableOf<t>, r = arkGet<t, k1
|
|
21
|
-
get<const k1 extends arkIndexableOf<t>, const k2 extends arkIndexableOf<arkGet<t, k1
|
|
22
|
-
get<const k1 extends arkIndexableOf<t>, const k2 extends arkIndexableOf<arkGet<t, k1>>, const k3 extends arkIndexableOf<arkGet<arkGet<t, k1>, k2>>, r = arkGet<arkGet<arkGet<t, k1>, k2>, k3
|
|
20
|
+
get<const k1 extends arkIndexableOf<t>, r = instantiateType<arkGet<t, k1>, $>>(k1: k1 | type.cast<k1>): r extends infer _ ? _ : never;
|
|
21
|
+
get<const k1 extends arkIndexableOf<t>, const k2 extends arkIndexableOf<arkGet<t, k1>>, r = instantiateType<arkGet<arkGet<t, k1>, k2>, $>>(k1: k1 | type.cast<k1>, k2: k2 | type.cast<k2>): r extends infer _ ? _ : never;
|
|
22
|
+
get<const k1 extends arkIndexableOf<t>, const k2 extends arkIndexableOf<arkGet<t, k1>>, const k3 extends arkIndexableOf<arkGet<arkGet<t, k1>, k2>>, r = instantiateType<arkGet<arkGet<arkGet<t, k1>, k2>, k3>, $>>(k1: k1 | type.cast<k1>, k2: k2 | type.cast<k2>, k3: k3 | type.cast<k3>): r extends infer _ ? _ : never;
|
|
23
23
|
/**
|
|
24
24
|
* Create a copy of this `Type` with only the specified properties.
|
|
25
25
|
* @example type({ foo: "string", bar: "number" }).pick("foo") // Type<{ foo: string }>
|
|
@@ -38,7 +38,7 @@ interface Type<out t extends object = object, $ = {}> extends BaseType<t, $> {
|
|
|
38
38
|
* Merge another `Type` definition, overriding properties of this `Type` with the duplicate keys.
|
|
39
39
|
* @example type({ a: "1", b: "2" }).merge({ b: "3", c: "4" }) // Type<{ a: 1, b: 3, c: 4 }>
|
|
40
40
|
*/
|
|
41
|
-
merge<const def,
|
|
41
|
+
merge<const def, inferredDef = type.infer<def, $>, r = Type<merge<t, inferredDef>, $>>(def: type.validate<def, $> & (inferredDef extends object ? unknown : ErrorType<"Merged type must be an object", [actual: inferredDef]>)): r extends infer _ ? _ : never;
|
|
42
42
|
/**
|
|
43
43
|
* Create a copy of this `Type` with all properties required.
|
|
44
44
|
* @example const T = type({ "foo?"": "string" }).required() // Type<{ foo: string }>
|
|
@@ -53,7 +53,7 @@ interface Type<out t extends object = object, $ = {}> extends BaseType<t, $> {
|
|
|
53
53
|
partial(): Type<{
|
|
54
54
|
[k in keyof t]?: t[k];
|
|
55
55
|
}, $>;
|
|
56
|
-
map<transformed extends listable<MappedTypeProp
|
|
56
|
+
map<transformed extends listable<MappedTypeProp>, r = Type<constructMapped<t, transformed>, $>>(flatMapEntry: (entry: typePropOf<t, $>) => transformed): r extends infer _ ? _ : never;
|
|
57
57
|
/**
|
|
58
58
|
* List of property info of this `Type<object>`.
|
|
59
59
|
* @example type({ foo: "string = "" }).props // [{ kind: "required", key: "foo", value: Type<string>, default: "" }]
|
package/out/parser/definition.js
CHANGED
|
@@ -12,7 +12,7 @@ export const parseInnerDefinition = (def, ctx) => {
|
|
|
12
12
|
// resolutions like "this" or generic args
|
|
13
13
|
return parseString(def, ctx);
|
|
14
14
|
}
|
|
15
|
-
const scopeCache = (parseCache[ctx.$.
|
|
15
|
+
const scopeCache = (parseCache[ctx.$.name] ??= {});
|
|
16
16
|
return (scopeCache[def] ??= parseString(def, ctx));
|
|
17
17
|
}
|
|
18
18
|
return hasDomain(def, "object") ?
|
package/out/parser/property.d.ts
CHANGED
|
@@ -21,9 +21,8 @@ export type validateProperty<def, keyKind extends ParsedKeyKind, $, args> = [
|
|
|
21
21
|
* })
|
|
22
22
|
* ```
|
|
23
23
|
*/
|
|
24
|
-
def : keyKind extends "spread" ?
|
|
24
|
+
def : keyKind extends "spread" ? def extends validateInnerDefinition<def, $, args> ? inferDefinition<def, $, args> extends object ? def : ErrorType<writeInvalidSpreadTypeMessage<typeToString<inferDefinition<def, $, args>>>> : validateInnerDefinition<def, $, args> : keyKind extends "undeclared" ? UndeclaredKeyBehavior : keyKind extends "required" ? validateInnerDefinition<def, $, args> : def extends OptionalPropertyDefinition ? ErrorMessage<invalidOptionalKeyKindMessage> : isDefaultable<def, $, args> extends true ? ErrorMessage<invalidDefaultableKeyKindMessage> : validateInnerDefinition<def, $, args>;
|
|
25
25
|
export type isDefaultable<def, $, args> = def extends DefaultablePropertyTuple ? true : def extends PossibleDefaultableStringDefinition ? parseString<def, $, args> extends DefaultablePropertyTuple ? true : false : false;
|
|
26
|
-
type validateSpread<def, inferredProperty, $, args> = inferredProperty extends object ? validateInnerDefinition<def, $, args> : ErrorType<writeInvalidSpreadTypeMessage<typeToString<inferredProperty>>>;
|
|
27
26
|
export type OptionalPropertyDefinition<baseDef = unknown> = OptionalPropertyTuple<baseDef> | OptionalPropertyString<baseDef & string>;
|
|
28
27
|
export type OptionalPropertyString<baseDef extends string = string> = `${baseDef}?`;
|
|
29
28
|
export type OptionalPropertyTuple<baseDef = unknown> = readonly [baseDef, "?"];
|
|
@@ -33,4 +32,3 @@ export declare const invalidOptionalKeyKindMessage = "Only required keys may mak
|
|
|
33
32
|
export type invalidOptionalKeyKindMessage = typeof invalidOptionalKeyKindMessage;
|
|
34
33
|
export declare const invalidDefaultableKeyKindMessage = "Only required keys may specify default values, e.g. { value: 'number = 0' }";
|
|
35
34
|
export type invalidDefaultableKeyKindMessage = typeof invalidDefaultableKeyKindMessage;
|
|
36
|
-
export {};
|
|
@@ -32,7 +32,7 @@ export declare namespace ArkTypeScanner {
|
|
|
32
32
|
type lookaheadIsFinalizing<lookahead extends string, unscanned extends string> = lookahead extends ">" ? unscanned extends `=${infer nextUnscanned}` ? nextUnscanned extends `=${string}` ? true : false : ArkTypeScanner.skipWhitespace<unscanned> extends ("" | `${TerminatingChar}${string}`) ? true : false : lookahead extends "=" ? unscanned extends `=${string}` ? false : true : lookahead extends "," | "?" ? true : false;
|
|
33
33
|
type TerminatingChar = keyof typeof ArkTypeScanner.terminatingChars;
|
|
34
34
|
type FinalizingLookahead = keyof typeof ArkTypeScanner.finalizingLookaheads;
|
|
35
|
-
type InfixToken = Comparator | "|" | "&" | "%" | ":" | "=>" | "#" | "@" | "=";
|
|
35
|
+
type InfixToken = Comparator | "|" | "&" | "%" | ":" | "=>" | "|>" | "#" | "@" | "=";
|
|
36
36
|
type PostfixToken = "[]" | "?";
|
|
37
37
|
type OperatorToken = InfixToken | PostfixToken;
|
|
38
38
|
type shift<lookahead extends string, unscanned extends string> = `${lookahead}${unscanned}`;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { type BaseParseContext, type BaseRoot, type
|
|
1
|
+
import { type BaseParseContext, type BaseRoot, type Morph, type Predicate, type unwrapDefault } from "@ark/schema";
|
|
2
2
|
import { type array, type BuiltinObjectKind, type Constructor, type Domain, type show } from "@ark/util";
|
|
3
|
-
import type { defaultFor, distill, inferIntersection, inferMorphOut, inferPredicate, Out, withDefault } from "../attributes.ts";
|
|
3
|
+
import type { defaultFor, distill, inferIntersection, inferMorphOut, inferPipe, inferPredicate, Out, withDefault } from "../attributes.ts";
|
|
4
|
+
import type { TypeMetaInput } from "../config.ts";
|
|
4
5
|
import type { type } from "../keywords/keywords.ts";
|
|
5
6
|
import type { inferDefinition, validateDefinition } from "./definition.ts";
|
|
6
7
|
import { writeMissingRightOperandMessage } from "./shift/operand/unenclosed.ts";
|
|
@@ -11,38 +12,73 @@ export type maybeValidateTupleExpression<def extends array, $, args> = def exten
|
|
|
11
12
|
def[0] extends "" ? BaseCompletions<$, args, IndexZeroOperator | "..."> : def[0],
|
|
12
13
|
def[1] extends "" ? BaseCompletions<$, args, IndexOneOperator | "..."> : def[1]
|
|
13
14
|
] : null;
|
|
14
|
-
export type inferTupleExpression<def extends TupleExpression, $, args> = def[1] extends "[]" ? inferDefinition<def[0], $, args>[] : def[1] extends "?" ? inferDefinition<def[0], $, args> : def[1] extends "&" ? inferIntersection<inferDefinition<def[0], $, args>, inferDefinition<def[2], $, args>> : def[1] extends "|" ? inferDefinition<def[0], $, args> | inferDefinition<def[2], $, args> : def[1] extends ":" ? inferPredicate<inferDefinition<def[0], $, args>, def[2]> : def[1] extends "=>" ? parseMorph<def[0], def[2], $, args> : def[1] extends "=" ? withDefault<inferDefinition<def[0], $, args>, unwrapDefault<def[2]>> : def[1] extends "@" ? inferDefinition<def[0], $, args> : def extends readonly ["===", ...infer values] ? values[number] : def extends (readonly ["instanceof", ...infer constructors extends Constructor[]]) ? InstanceType<constructors[number]> : def[0] extends "keyof" ? inferKeyOfExpression<def[1], $, args> : never;
|
|
15
|
+
export type inferTupleExpression<def extends TupleExpression, $, args> = def[1] extends "[]" ? inferDefinition<def[0], $, args>[] : def[1] extends "?" ? inferDefinition<def[0], $, args> : def[1] extends "&" ? inferIntersection<inferDefinition<def[0], $, args>, inferDefinition<def[2], $, args>> : def[1] extends "|" ? inferDefinition<def[0], $, args> | inferDefinition<def[2], $, args> : def[1] extends ":" ? inferPredicate<inferDefinition<def[0], $, args>, def[2]> : def[1] extends "=>" ? parseMorph<def[0], def[2], $, args> : def[1] extends "|>" ? parseTo<def[0], def[2], $, args> : def[1] extends "=" ? withDefault<inferDefinition<def[0], $, args>, unwrapDefault<def[2]>> : def[1] extends "@" ? inferDefinition<def[0], $, args> : def extends readonly ["===", ...infer values] ? values[number] : def extends (readonly ["instanceof", ...infer constructors extends Constructor[]]) ? InstanceType<constructors[number]> : def[0] extends "keyof" ? inferKeyOfExpression<def[1], $, args> : never;
|
|
15
16
|
export type validatePrefixExpression<def extends IndexZeroExpression, $, args> = def["length"] extends 1 ? readonly [writeMissingRightOperandMessage<def[0]>] : def[0] extends "keyof" ? readonly [def[0], validateDefinition<def[1], $, args>] : def[0] extends "===" ? readonly [def[0], ...unknown[]] : def[0] extends "instanceof" ? readonly [def[0], ...Constructor[]] : never;
|
|
16
17
|
export type validateIndexOneExpression<def extends IndexOneExpression, $, args> = def[1] extends TuplePostfixOperator ? readonly [validateDefinition<def[0], $, args>, def[1]] : readonly [
|
|
17
18
|
validateDefinition<def[0], $, args>,
|
|
18
19
|
def["length"] extends 2 ? writeMissingRightOperandMessage<def[1]> : def[1],
|
|
19
|
-
def[1] extends "|" ? validateDefinition<def[2], $, args> : def[1] extends "&" ? validateDefinition<def[2], $, args> : def[1] extends ":" ? Predicate<type.infer.Out<def[0], $, args>> : def[1] extends "=>" ? Morph<type.infer.Out<def[0], $, args>> : def[1] extends "=" ? defaultFor<type.infer.In<def[0], $, args>> : def[1] extends "@" ?
|
|
20
|
+
def[1] extends "|" ? validateDefinition<def[2], $, args> : def[1] extends "&" ? validateDefinition<def[2], $, args> : def[1] extends ":" ? Predicate<type.infer.Out<def[0], $, args>> : def[1] extends "=>" ? Morph<type.infer.Out<def[0], $, args>> : def[1] extends "|>" ? validateDefinition<def[2], $, args> : def[1] extends "=" ? defaultFor<type.infer.In<def[0], $, args>> : def[1] extends "@" ? TypeMetaInput : validateDefinition<def[2], $, args>
|
|
20
21
|
];
|
|
21
22
|
export type UnparsedTupleExpressionInput = {
|
|
22
23
|
instanceof: Constructor;
|
|
23
24
|
"===": unknown;
|
|
24
25
|
};
|
|
25
26
|
export type UnparsedTupleOperator = show<keyof UnparsedTupleExpressionInput>;
|
|
26
|
-
export declare const parseKeyOfTuple:
|
|
27
|
+
export declare const parseKeyOfTuple: IndexZeroParser<"keyof">;
|
|
27
28
|
export type inferKeyOfExpression<operandDef, $, args> = show<keyof inferDefinition<operandDef, $, args>>;
|
|
28
|
-
export type IndexOneParser<token extends IndexOneOperator> = (def: IndexOneExpression<token>, ctx: BaseParseContext) => BaseRoot;
|
|
29
|
-
export type PrefixParser<token extends IndexZeroOperator> = (def: IndexZeroExpression<token>, ctx: BaseParseContext) => BaseRoot;
|
|
30
29
|
export type TupleExpression = IndexZeroExpression | IndexOneExpression;
|
|
31
30
|
export type TupleExpressionOperator = IndexZeroOperator | IndexOneOperator;
|
|
32
|
-
export type IndexOneOperator = TuplePostfixOperator | TupleInfixOperator;
|
|
33
31
|
export type ArgTwoOperator = Exclude<IndexOneOperator, "?" | "=">;
|
|
34
|
-
export type TuplePostfixOperator = "[]" | "?";
|
|
35
|
-
export type TupleInfixOperator = "&" | "|" | "=>" | "=" | ":" | "@";
|
|
36
|
-
export type IndexOneExpression<token extends IndexOneOperator = IndexOneOperator> = readonly [unknown, token, ...unknown[]];
|
|
37
32
|
export declare const parseMorphTuple: IndexOneParser<"=>">;
|
|
38
|
-
export declare const
|
|
33
|
+
export declare const parseToTuple: IndexOneParser<"|>">;
|
|
34
|
+
export declare const writeMalformedFunctionalExpressionMessage: (operator: ":" | "=>" | "|>", value: unknown) => string;
|
|
35
|
+
export type parseTo<inDef, outDef, $, args> = inferPipe<inferDefinition<inDef, $, args>, inferDefinition<outDef, $, args>>;
|
|
39
36
|
export type parseMorph<inDef, morph, $, args> = morph extends Morph ? inferMorphOut<morph> extends infer out ? (In: distill.In<inferDefinition<inDef, $, args>>) => Out<out> : never : never;
|
|
40
37
|
export declare const parseNarrowTuple: IndexOneParser<":">;
|
|
41
|
-
export type
|
|
42
|
-
|
|
38
|
+
export type IndexOneExpression<token extends string = IndexOneOperator> = readonly [unknown, token, ...unknown[]];
|
|
39
|
+
type IndexOneParser<token extends string> = (def: IndexOneExpression<token>, ctx: BaseParseContext) => BaseRoot;
|
|
40
|
+
declare const postfixParsers: {
|
|
41
|
+
"?": IndexOneParser<"?">;
|
|
42
|
+
"[]": IndexOneParser<"[]">;
|
|
43
|
+
};
|
|
44
|
+
export type TuplePostfixOperator = keyof typeof postfixParsers;
|
|
45
|
+
declare const infixParsers: {
|
|
46
|
+
"=": IndexOneParser<"=">;
|
|
47
|
+
"|": IndexOneParser<"|">;
|
|
48
|
+
"&": IndexOneParser<"&">;
|
|
49
|
+
":": IndexOneParser<":">;
|
|
50
|
+
"=>": IndexOneParser<"=>">;
|
|
51
|
+
"|>": IndexOneParser<"|>">;
|
|
52
|
+
"@": IndexOneParser<"@">;
|
|
53
|
+
};
|
|
54
|
+
export type TupleInfixOperator = keyof typeof infixParsers;
|
|
55
|
+
declare const indexOneParsers: {
|
|
56
|
+
"=": IndexOneParser<"=">;
|
|
57
|
+
"|": IndexOneParser<"|">;
|
|
58
|
+
"&": IndexOneParser<"&">;
|
|
59
|
+
":": IndexOneParser<":">;
|
|
60
|
+
"=>": IndexOneParser<"=>">;
|
|
61
|
+
"|>": IndexOneParser<"|>">;
|
|
62
|
+
"@": IndexOneParser<"@">;
|
|
63
|
+
"?": IndexOneParser<"?">;
|
|
64
|
+
"[]": IndexOneParser<"[]">;
|
|
65
|
+
};
|
|
66
|
+
export type IndexOneOperator = keyof typeof indexOneParsers;
|
|
43
67
|
export type InfixExpression = readonly [
|
|
44
68
|
unknown,
|
|
45
69
|
ArkTypeScanner.InfixToken,
|
|
46
70
|
...unknown[]
|
|
47
71
|
];
|
|
72
|
+
type IndexZeroParser<token extends string> = (def: IndexZeroExpression<token>, ctx: BaseParseContext) => BaseRoot;
|
|
73
|
+
type IndexZeroExpression<token extends string = IndexZeroOperator> = readonly [
|
|
74
|
+
token,
|
|
75
|
+
...unknown[]
|
|
76
|
+
];
|
|
77
|
+
declare const indexZeroParsers: {
|
|
78
|
+
keyof: IndexZeroParser<"keyof">;
|
|
79
|
+
instanceof: IndexZeroParser<"instanceof">;
|
|
80
|
+
"===": IndexZeroParser<"===">;
|
|
81
|
+
};
|
|
82
|
+
export type IndexZeroOperator = keyof typeof indexZeroParsers;
|
|
48
83
|
export declare const writeInvalidConstructorMessage: <actual extends Domain | BuiltinObjectKind>(actual: actual) => string;
|
|
84
|
+
export {};
|
|
@@ -2,7 +2,7 @@ import { Disjoint, intersectNodesRoot } from "@ark/schema";
|
|
|
2
2
|
import { objectKindOrDomainOf, throwParseError } from "@ark/util";
|
|
3
3
|
import { shallowDefaultableMessage, shallowOptionalMessage } from "./ast/validate.js";
|
|
4
4
|
import { writeMissingRightOperandMessage } from "./shift/operand/unenclosed.js";
|
|
5
|
-
export const maybeParseTupleExpression = (def, ctx) => isIndexZeroExpression(def) ?
|
|
5
|
+
export const maybeParseTupleExpression = (def, ctx) => isIndexZeroExpression(def) ? indexZeroParsers[def[0]](def, ctx)
|
|
6
6
|
: isIndexOneExpression(def) ? indexOneParsers[def[1]](def, ctx)
|
|
7
7
|
: null;
|
|
8
8
|
export const parseKeyOfTuple = (def, ctx) => ctx.$.parseOwnDefinitionFormat(def[1], ctx).keyof();
|
|
@@ -19,13 +19,17 @@ const parseBranchTuple = (def, ctx) => {
|
|
|
19
19
|
return result;
|
|
20
20
|
};
|
|
21
21
|
const parseArrayTuple = (def, ctx) => ctx.$.parseOwnDefinitionFormat(def[0], ctx).array();
|
|
22
|
-
const isIndexOneExpression = (def) => indexOneParsers[def[1]] !== undefined;
|
|
23
22
|
export const parseMorphTuple = (def, ctx) => {
|
|
24
23
|
if (typeof def[2] !== "function") {
|
|
25
24
|
return throwParseError(writeMalformedFunctionalExpressionMessage("=>", def[2]));
|
|
26
25
|
}
|
|
27
26
|
return ctx.$.parseOwnDefinitionFormat(def[0], ctx).pipe(def[2]);
|
|
28
27
|
};
|
|
28
|
+
export const parseToTuple = (def, ctx) => {
|
|
29
|
+
const node = ctx.$.parseOwnDefinitionFormat(def[0], ctx);
|
|
30
|
+
const outputValidator = ctx.$.parseOwnDefinitionFormat(def[2], ctx);
|
|
31
|
+
return node.to(outputValidator);
|
|
32
|
+
};
|
|
29
33
|
export const writeMalformedFunctionalExpressionMessage = (operator, value) => `${operator === ":" ? "Narrow" : "Morph"} expression requires a function following '${operator}' (was ${typeof value})`;
|
|
30
34
|
export const parseNarrowTuple = (def, ctx) => {
|
|
31
35
|
if (typeof def[2] !== "function") {
|
|
@@ -34,19 +38,26 @@ export const parseNarrowTuple = (def, ctx) => {
|
|
|
34
38
|
return ctx.$.parseOwnDefinitionFormat(def[0], ctx).constrain("predicate", def[2]);
|
|
35
39
|
};
|
|
36
40
|
const parseAttributeTuple = (def, ctx) => ctx.$.parseOwnDefinitionFormat(def[0], ctx).configureShallowDescendants(def[2]);
|
|
37
|
-
const
|
|
41
|
+
const defineIndexOneParsers = (parsers) => parsers;
|
|
42
|
+
const postfixParsers = defineIndexOneParsers({
|
|
38
43
|
"[]": parseArrayTuple,
|
|
44
|
+
"?": () => throwParseError(shallowOptionalMessage)
|
|
45
|
+
});
|
|
46
|
+
const infixParsers = defineIndexOneParsers({
|
|
39
47
|
"|": parseBranchTuple,
|
|
40
48
|
"&": parseBranchTuple,
|
|
41
49
|
":": parseNarrowTuple,
|
|
42
50
|
"=>": parseMorphTuple,
|
|
51
|
+
"|>": parseToTuple,
|
|
43
52
|
"@": parseAttributeTuple,
|
|
44
53
|
// since object and tuple literals parse there via `parseProperty`,
|
|
45
54
|
// they must be shallow if parsed directly as a tuple expression
|
|
46
|
-
"=": () => throwParseError(shallowDefaultableMessage)
|
|
47
|
-
|
|
48
|
-
};
|
|
49
|
-
const
|
|
55
|
+
"=": () => throwParseError(shallowDefaultableMessage)
|
|
56
|
+
});
|
|
57
|
+
const indexOneParsers = { ...postfixParsers, ...infixParsers };
|
|
58
|
+
const isIndexOneExpression = (def) => indexOneParsers[def[1]] !== undefined;
|
|
59
|
+
const defineIndexZeroParsers = (parsers) => parsers;
|
|
60
|
+
const indexZeroParsers = defineIndexZeroParsers({
|
|
50
61
|
keyof: parseKeyOfTuple,
|
|
51
62
|
instanceof: (def, ctx) => {
|
|
52
63
|
if (typeof def[1] !== "function") {
|
|
@@ -62,6 +73,6 @@ const prefixParsers = {
|
|
|
62
73
|
: ctx.$.node("union", { branches });
|
|
63
74
|
},
|
|
64
75
|
"===": (def, ctx) => ctx.$.units(def.slice(1))
|
|
65
|
-
};
|
|
66
|
-
const isIndexZeroExpression = (def) =>
|
|
76
|
+
});
|
|
77
|
+
const isIndexZeroExpression = (def) => indexZeroParsers[def[0]] !== undefined;
|
|
67
78
|
export const writeInvalidConstructorMessage = (actual) => `Expected a constructor following 'instanceof' operator (was ${actual})`;
|
package/out/scope.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { BaseScope, type AliasDefEntry, type
|
|
1
|
+
import { BaseScope, type AliasDefEntry, type ArkSchemaRegistry, type ArkSchemaScopeConfig, type BaseNode, type BaseParseContext, type BaseParseContextInput, type BaseParseOptions, type BaseRoot, type GenericAst, type GenericParamAst, type GenericParamDef, type NodeKind, type NodeSchema, type PreparsedNodeResolution, type PrivateDeclaration, type ResolvedScopeConfig, type RootKind, type RootSchema, type arkKind, type exportedNameOf, type nodeOfKind, type reducibleKindOf, type toInternalScope, type writeDuplicateAliasError } from "@ark/schema";
|
|
2
2
|
import { type Brand, type ErrorType, type JsonStructure, type anyOrNever, type array, type flattenListable, type noSuggest } from "@ark/util";
|
|
3
|
-
import type {
|
|
3
|
+
import type { TypeMetaInput } from "./config.ts";
|
|
4
4
|
import { type GenericDeclaration, type GenericParser, type ParameterString, type baseGenericConstraints, type parseGenericParams, type parseValidGenericParams } from "./generic.ts";
|
|
5
5
|
import type { Ark, type } from "./keywords/keywords.ts";
|
|
6
|
-
import { InternalMatchParser } from "./match.ts";
|
|
7
|
-
import type { BoundModule, Module, Submodule, instantiateExport } from "./module.ts";
|
|
6
|
+
import { InternalMatchParser, type MatchParser } from "./match.ts";
|
|
7
|
+
import type { BoundModule, Module, Submodule, exportScope, instantiateExport } from "./module.ts";
|
|
8
8
|
import type { DefAst, InferredAst } from "./parser/ast/infer.ts";
|
|
9
9
|
import { type inferDefinition } from "./parser/definition.ts";
|
|
10
10
|
import type { ParsedOptionalProperty } from "./parser/property.ts";
|
|
@@ -12,6 +12,8 @@ import type { ParsedDefaultableProperty } from "./parser/shift/operator/default.
|
|
|
12
12
|
import { InternalTypeParser, type DeclarationParser, type DefinitionParser, type EnumeratedTypeParser, type InstanceOfTypeParser, type SchemaParser, type TypeParser, type UnitTypeParser } from "./type.ts";
|
|
13
13
|
/** The convenience properties attached to `scope` */
|
|
14
14
|
export type ScopeParserAttachments = Omit<ScopeParser, never>;
|
|
15
|
+
export interface ArkScopeConfig extends ArkSchemaScopeConfig {
|
|
16
|
+
}
|
|
15
17
|
export interface ScopeParser {
|
|
16
18
|
<const def>(def: scope.validate<def>, config?: ArkScopeConfig): Scope<scope.infer<def>>;
|
|
17
19
|
define: <const def>(def: scope.validate<def>) => def;
|
|
@@ -60,14 +62,19 @@ export type moduleKeyOf<$> = {
|
|
|
60
62
|
}[keyof $];
|
|
61
63
|
export interface ArkTypeRegistry extends ArkSchemaRegistry {
|
|
62
64
|
typeAttachments?: Ark.boundTypeAttachments<any>;
|
|
65
|
+
ambient: exportScope<Ark>;
|
|
63
66
|
}
|
|
64
67
|
export declare const $arkTypeRegistry: ArkTypeRegistry;
|
|
65
68
|
export interface InternalScope {
|
|
66
69
|
constructor: typeof InternalScope;
|
|
67
70
|
}
|
|
71
|
+
interface ResolvedTypeScopeConfig extends ResolvedScopeConfig {
|
|
72
|
+
keywords?: Record<string, TypeMetaInput>;
|
|
73
|
+
}
|
|
68
74
|
export declare class InternalScope<$ extends {} = {}> extends BaseScope<$> {
|
|
75
|
+
resolvedConfig: ResolvedTypeScopeConfig;
|
|
69
76
|
get ambientAttachments(): Ark.boundTypeAttachments<$> | undefined;
|
|
70
|
-
protected preparseOwnAliasEntry(
|
|
77
|
+
protected preparseOwnAliasEntry(alias: string, def: unknown): AliasDefEntry;
|
|
71
78
|
parseGenericParams(def: string, opts: BaseParseOptions): array<GenericParamDef>;
|
|
72
79
|
protected normalizeRootScopeValue(resolution: unknown): unknown;
|
|
73
80
|
protected preparseOwnDefinitionFormat(def: unknown, opts: BaseParseOptions): BaseRoot | BaseParseContextInput;
|
|
@@ -75,12 +82,12 @@ export declare class InternalScope<$ extends {} = {}> extends BaseScope<$> {
|
|
|
75
82
|
unit: UnitTypeParser<$>;
|
|
76
83
|
enumerated: EnumeratedTypeParser<$>;
|
|
77
84
|
instanceOf: InstanceOfTypeParser<$>;
|
|
78
|
-
type: InternalTypeParser;
|
|
79
85
|
match: InternalMatchParser;
|
|
80
86
|
declare: () => {
|
|
81
87
|
type: InternalTypeParser;
|
|
82
88
|
};
|
|
83
89
|
define<def>(def: def): def;
|
|
90
|
+
type: InternalTypeParser;
|
|
84
91
|
static scope: ScopeParser;
|
|
85
92
|
static module: ModuleParser;
|
|
86
93
|
}
|
|
@@ -107,6 +114,7 @@ export interface Scope<$ = {}> {
|
|
|
107
114
|
unit: UnitTypeParser<$>;
|
|
108
115
|
enumerated: EnumeratedTypeParser<$>;
|
|
109
116
|
type: TypeParser<$>;
|
|
117
|
+
match: MatchParser<$>;
|
|
110
118
|
declare: DeclarationParser<$>;
|
|
111
119
|
define: DefinitionParser<$>;
|
|
112
120
|
generic: GenericParser<$>;
|
package/out/scope.js
CHANGED
|
@@ -16,22 +16,31 @@ export class InternalScope extends BaseScope {
|
|
|
16
16
|
this.bindReference(v)
|
|
17
17
|
]));
|
|
18
18
|
}
|
|
19
|
-
preparseOwnAliasEntry(
|
|
20
|
-
const firstParamIndex =
|
|
21
|
-
if (firstParamIndex === -1)
|
|
22
|
-
|
|
23
|
-
|
|
19
|
+
preparseOwnAliasEntry(alias, def) {
|
|
20
|
+
const firstParamIndex = alias.indexOf("<");
|
|
21
|
+
if (firstParamIndex === -1) {
|
|
22
|
+
if (hasArkKind(def, "module") || hasArkKind(def, "generic"))
|
|
23
|
+
return [alias, def];
|
|
24
|
+
const qualifiedName = this.name === "ark" ? alias
|
|
25
|
+
: alias === "root" ? this.name
|
|
26
|
+
: `${this.name}.${alias}`;
|
|
27
|
+
const config = this.resolvedConfig.keywords?.[qualifiedName];
|
|
28
|
+
if (config)
|
|
29
|
+
def = [def, "@", config];
|
|
30
|
+
return [alias, def];
|
|
31
|
+
}
|
|
32
|
+
if (alias.at(-1) !== ">") {
|
|
24
33
|
throwParseError(`'>' must be the last character of a generic declaration in a scope`);
|
|
25
34
|
}
|
|
26
|
-
const name =
|
|
27
|
-
const paramString =
|
|
35
|
+
const name = alias.slice(0, firstParamIndex);
|
|
36
|
+
const paramString = alias.slice(firstParamIndex + 1, -1);
|
|
28
37
|
return [
|
|
29
38
|
name,
|
|
30
39
|
// use a thunk definition for the generic so that we can parse
|
|
31
40
|
// constraints within the current scope
|
|
32
41
|
() => {
|
|
33
42
|
const params = this.parseGenericParams(paramString, { alias: name });
|
|
34
|
-
const generic = parseGeneric(params,
|
|
43
|
+
const generic = parseGeneric(params, def, this);
|
|
35
44
|
return generic;
|
|
36
45
|
}
|
|
37
46
|
];
|
|
@@ -74,7 +83,6 @@ export class InternalScope extends BaseScope {
|
|
|
74
83
|
unit = value => this.units([value]);
|
|
75
84
|
enumerated = (...values) => this.units(values);
|
|
76
85
|
instanceOf = ctor => this.node("proto", { proto: ctor }, { prereduced: true });
|
|
77
|
-
type = new InternalTypeParser(this);
|
|
78
86
|
match = new InternalMatchParser(this);
|
|
79
87
|
declare = () => ({
|
|
80
88
|
type: this.type
|
|
@@ -82,6 +90,7 @@ export class InternalScope extends BaseScope {
|
|
|
82
90
|
define(def) {
|
|
83
91
|
return def;
|
|
84
92
|
}
|
|
93
|
+
type = new InternalTypeParser(this);
|
|
85
94
|
static scope = ((def, config = {}) => new InternalScope(def, config));
|
|
86
95
|
static module = ((def, config = {}) => this.scope(def, config).export());
|
|
87
96
|
}
|