arktype 2.0.0-rc.3 → 2.0.0-rc.31
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/out/attributes.d.ts +94 -0
- package/out/attributes.js +3 -0
- package/out/config.d.ts +1 -1
- package/out/generic.d.ts +55 -58
- package/out/generic.js +6 -8
- package/out/index.d.ts +6 -4
- package/out/index.js +4 -3
- package/out/keywords/{constructors/Array.d.ts → Array.d.ts} +3 -3
- package/out/keywords/{constructors/Array.js → Array.js} +7 -7
- package/out/keywords/{constructors/FormData.d.ts → FormData.d.ts} +3 -3
- package/out/keywords/{constructors/FormData.js → FormData.js} +8 -8
- package/out/keywords/{constructors/TypedArray.d.ts → TypedArray.d.ts} +1 -1
- package/out/keywords/{constructors/TypedArray.js → TypedArray.js} +2 -2
- package/out/keywords/builtins.js +2 -2
- package/out/keywords/{constructors/constructors.d.ts → constructors.d.ts} +1 -2
- package/out/keywords/{constructors/constructors.js → constructors.js} +2 -2
- package/out/keywords/keywords.d.ts +61 -0
- package/out/keywords/keywords.js +29 -0
- package/out/keywords/number.d.ts +23 -0
- package/out/keywords/{number/epoch.js → number.js} +24 -2
- package/out/keywords/string.d.ts +327 -0
- package/out/keywords/string.js +398 -0
- package/out/keywords/ts.d.ts +42 -4
- package/out/keywords/ts.js +24 -7
- package/out/methods/array.d.ts +12 -0
- package/out/methods/base.d.ts +178 -0
- package/out/methods/date.d.ts +10 -0
- package/out/methods/instantiate.d.ts +17 -0
- package/out/methods/morph.d.ts +12 -0
- package/out/methods/number.d.ts +11 -0
- package/out/methods/object.d.ts +106 -0
- package/out/methods/string.d.ts +12 -0
- package/out/module.d.ts +2 -2
- package/out/parser/{semantic → ast}/bounds.d.ts +3 -3
- package/out/parser/ast/default.d.ts +8 -0
- package/out/parser/ast/generic.d.ts +17 -0
- package/out/parser/ast/infer.d.ts +25 -0
- package/out/parser/{semantic → ast}/utils.d.ts +1 -1
- package/out/parser/ast/validate.d.ts +30 -0
- package/out/parser/ast/validate.js +3 -0
- package/out/parser/definition.d.ts +21 -13
- package/out/parser/definition.js +20 -2
- package/out/parser/objectLiteral.d.ts +41 -67
- package/out/parser/objectLiteral.js +84 -71
- package/out/parser/property.d.ts +36 -0
- package/out/parser/property.js +16 -0
- package/out/parser/{string/reduce → reduce}/dynamic.d.ts +10 -13
- package/out/parser/{string/reduce → reduce}/dynamic.js +5 -6
- package/out/parser/{string/reduce → reduce}/shared.d.ts +1 -1
- package/out/parser/{string/reduce → reduce}/static.d.ts +4 -4
- package/out/parser/{string/shift → shift}/operand/date.d.ts +3 -1
- package/out/parser/{string/shift → shift}/operand/enclosed.d.ts +4 -4
- package/out/parser/{string/shift → shift}/operand/enclosed.js +7 -3
- package/out/parser/shift/operand/operand.d.ts +9 -0
- package/out/parser/{string/shift → shift}/operand/operand.js +2 -2
- package/out/parser/{string/shift → shift}/operand/unenclosed.d.ts +15 -7
- package/out/parser/{string/shift → shift}/operand/unenclosed.js +12 -7
- package/out/parser/{string/shift → shift}/operator/bounds.d.ts +7 -7
- package/out/parser/{string/shift → shift}/operator/bounds.js +5 -4
- package/out/parser/shift/operator/brand.d.ts +6 -0
- package/out/parser/shift/operator/brand.js +5 -0
- package/out/parser/{string/shift → shift}/operator/default.d.ts +4 -6
- package/out/parser/{string/shift → shift}/operator/default.js +4 -6
- package/out/parser/{string/shift → shift}/operator/divisor.d.ts +2 -2
- package/out/parser/shift/operator/operator.d.ts +14 -0
- package/out/parser/{string/shift → shift}/operator/operator.js +7 -5
- package/out/parser/shift/scanner.d.ts +47 -0
- package/out/parser/shift/scanner.js +43 -0
- package/out/parser/{string/string.d.ts → string.d.ts} +9 -9
- package/out/parser/{string/string.js → string.js} +19 -0
- package/out/parser/tupleExpressions.d.ts +47 -0
- package/out/parser/tupleExpressions.js +67 -0
- package/out/parser/tupleLiteral.d.ts +95 -0
- package/out/parser/tupleLiteral.js +122 -0
- package/out/scope.d.ts +41 -34
- package/out/scope.js +54 -62
- package/out/type.d.ts +46 -22
- package/out/type.js +15 -11
- package/package.json +3 -3
- package/out/intersect.d.ts +0 -28
- package/out/keys.d.ts +0 -16
- package/out/keywords/ark.d.ts +0 -39
- package/out/keywords/ark.js +0 -24
- package/out/keywords/ast.d.ts +0 -193
- package/out/keywords/ast.js +0 -2
- package/out/keywords/number/epoch.d.ts +0 -10
- package/out/keywords/number/integer.d.ts +0 -3
- package/out/keywords/number/integer.js +0 -5
- package/out/keywords/number/number.d.ts +0 -28
- package/out/keywords/number/number.js +0 -9
- package/out/keywords/string/alpha.d.ts +0 -7
- package/out/keywords/string/alpha.js +0 -2
- package/out/keywords/string/alphanumeric.d.ts +0 -7
- package/out/keywords/string/alphanumeric.js +0 -2
- package/out/keywords/string/capitalize.d.ts +0 -15
- package/out/keywords/string/capitalize.js +0 -12
- package/out/keywords/string/creditCard.d.ts +0 -8
- package/out/keywords/string/creditCard.js +0 -37
- package/out/keywords/string/date.d.ts +0 -59
- package/out/keywords/string/date.js +0 -98
- package/out/keywords/string/digits.d.ts +0 -7
- package/out/keywords/string/digits.js +0 -2
- package/out/keywords/string/email.d.ts +0 -7
- package/out/keywords/string/email.js +0 -4
- package/out/keywords/string/integer.d.ts +0 -16
- package/out/keywords/string/integer.js +0 -16
- package/out/keywords/string/ip.d.ts +0 -20
- package/out/keywords/string/ip.js +0 -23
- package/out/keywords/string/json.d.ts +0 -15
- package/out/keywords/string/json.js +0 -45
- package/out/keywords/string/lower.d.ts +0 -15
- package/out/keywords/string/lower.js +0 -12
- package/out/keywords/string/normalize.d.ts +0 -92
- package/out/keywords/string/normalize.js +0 -43
- package/out/keywords/string/numeric.d.ts +0 -15
- package/out/keywords/string/numeric.js +0 -13
- package/out/keywords/string/semver.d.ts +0 -7
- package/out/keywords/string/semver.js +0 -4
- package/out/keywords/string/string.d.ts +0 -112
- package/out/keywords/string/string.js +0 -41
- package/out/keywords/string/trim.d.ts +0 -15
- package/out/keywords/string/trim.js +0 -14
- package/out/keywords/string/upper.d.ts +0 -15
- package/out/keywords/string/upper.js +0 -12
- package/out/keywords/string/url.d.ts +0 -15
- package/out/keywords/string/url.js +0 -37
- package/out/keywords/string/utils.d.ts +0 -2
- package/out/keywords/string/utils.js +0 -10
- package/out/keywords/string/uuid.d.ts +0 -42
- package/out/keywords/string/uuid.js +0 -19
- package/out/keywords/utils.d.ts +0 -1
- package/out/keywords/utils.js +0 -2
- package/out/parser/semantic/default.d.ts +0 -9
- package/out/parser/semantic/default.js +0 -1
- package/out/parser/semantic/infer.d.ts +0 -39
- package/out/parser/semantic/validate.d.ts +0 -26
- package/out/parser/semantic/validate.js +0 -1
- package/out/parser/string/shift/operand/operand.d.ts +0 -9
- package/out/parser/string/shift/operator/operator.d.ts +0 -13
- package/out/parser/string/shift/scanner.d.ts +0 -69
- package/out/parser/string/shift/scanner.js +0 -100
- package/out/parser/tuple.d.ts +0 -132
- package/out/parser/tuple.js +0 -158
- package/out/subtypes/array.d.ts +0 -13
- package/out/subtypes/base.d.ts +0 -85
- package/out/subtypes/date.d.ts +0 -11
- package/out/subtypes/instantiate.d.ts +0 -16
- package/out/subtypes/morph.d.ts +0 -13
- package/out/subtypes/number.d.ts +0 -12
- package/out/subtypes/object.d.ts +0 -33
- package/out/subtypes/string.d.ts +0 -13
- package/out/subtypes/validator.d.ts +0 -11
- package/out/subtypes/validator.js +0 -1
- /package/out/{subtypes → methods}/array.js +0 -0
- /package/out/{subtypes → methods}/base.js +0 -0
- /package/out/{subtypes → methods}/date.js +0 -0
- /package/out/{subtypes → methods}/instantiate.js +0 -0
- /package/out/{subtypes → methods}/morph.js +0 -0
- /package/out/{subtypes → methods}/number.js +0 -0
- /package/out/{subtypes → methods}/object.js +0 -0
- /package/out/{subtypes → methods}/string.js +0 -0
- /package/out/parser/{semantic → ast}/bounds.js +0 -0
- /package/out/{intersect.js → parser/ast/default.js} +0 -0
- /package/out/parser/{semantic → ast}/divisor.d.ts +0 -0
- /package/out/parser/{semantic → ast}/divisor.js +0 -0
- /package/out/{keys.js → parser/ast/generic.js} +0 -0
- /package/out/parser/{semantic → ast}/infer.js +0 -0
- /package/out/parser/{semantic → ast}/keyof.d.ts +0 -0
- /package/out/parser/{semantic → ast}/keyof.js +0 -0
- /package/out/parser/{semantic → ast}/utils.js +0 -0
- /package/out/parser/{string/reduce → reduce}/shared.js +0 -0
- /package/out/parser/{string/reduce → reduce}/static.js +0 -0
- /package/out/parser/{string/shift → shift}/operand/date.js +0 -0
- /package/out/parser/{string/shift → shift}/operand/genericArgs.d.ts +0 -0
- /package/out/parser/{string/shift → shift}/operand/genericArgs.js +0 -0
- /package/out/parser/{string/shift → shift}/operator/divisor.js +0 -0
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { postfixAfterOptionalOrDefaultableMessage, type BaseParseContext, type BaseRoot, type Sequence } from "@ark/schema";
|
|
2
|
+
import { type array, type conform, type ErrorMessage, type satisfy } from "@ark/util";
|
|
3
|
+
import type { inferDefinition, validateInnerDefinition } from "./definition.ts";
|
|
4
|
+
import { type DefaultablePropertyTuple, type isDefaultable, type OptionalPropertyDefinition } from "./property.ts";
|
|
5
|
+
export declare const parseTupleLiteral: (def: array, ctx: BaseParseContext) => BaseRoot;
|
|
6
|
+
export type validateTupleLiteral<def extends array, $, args> = parseSequence<def, $, args> extends infer s extends SequenceParseState ? Readonly<s["validated"]> : never;
|
|
7
|
+
export type inferTupleLiteral<def extends array, $, args> = parseSequence<def, $, args> extends infer s extends SequenceParseState ? s["inferred"] : never;
|
|
8
|
+
type SequencePhase = satisfy<keyof Sequence.Inner, SequencePhase.prefix | SequencePhase.optionals | SequencePhase.defaultables | SequencePhase.postfix>;
|
|
9
|
+
declare namespace SequencePhase {
|
|
10
|
+
type prefix = "prefix";
|
|
11
|
+
type optionals = "optionals";
|
|
12
|
+
type defaultables = "defaultables";
|
|
13
|
+
type postfix = "postfix";
|
|
14
|
+
}
|
|
15
|
+
type SequenceParseState = {
|
|
16
|
+
unscanned: array;
|
|
17
|
+
inferred: array;
|
|
18
|
+
validated: array;
|
|
19
|
+
phase: SequencePhase;
|
|
20
|
+
};
|
|
21
|
+
type parseSequence<def extends array, $, args> = parseNextElement<{
|
|
22
|
+
unscanned: def;
|
|
23
|
+
inferred: [];
|
|
24
|
+
validated: [];
|
|
25
|
+
phase: SequencePhase.prefix;
|
|
26
|
+
}, $, args>;
|
|
27
|
+
type PreparsedElementKind = "required" | SequencePhase.optionals | SequencePhase.defaultables;
|
|
28
|
+
type PreparsedElement = {
|
|
29
|
+
head: unknown;
|
|
30
|
+
tail: array;
|
|
31
|
+
inferred: unknown;
|
|
32
|
+
validated: unknown;
|
|
33
|
+
kind: PreparsedElementKind;
|
|
34
|
+
spread: boolean;
|
|
35
|
+
};
|
|
36
|
+
declare namespace PreparsedElement {
|
|
37
|
+
type from<result extends PreparsedElement> = result;
|
|
38
|
+
type required = "required";
|
|
39
|
+
type optionals = "optionals";
|
|
40
|
+
type defaultables = "defaultables";
|
|
41
|
+
}
|
|
42
|
+
type preparseNextState<s extends SequenceParseState, $, args> = s["unscanned"] extends readonly ["...", infer head, ...infer tail] ? preparseNextElement<head, tail, true, $, args> : s["unscanned"] extends readonly [infer head, ...infer tail] ? preparseNextElement<head, tail, false, $, args> : null;
|
|
43
|
+
type preparseNextElement<head, tail extends array, spread extends boolean, $, args> = PreparsedElement.from<{
|
|
44
|
+
head: head;
|
|
45
|
+
tail: tail;
|
|
46
|
+
inferred: inferDefinition<head, $, args>;
|
|
47
|
+
validated: validateInnerDefinition<head, $, args>;
|
|
48
|
+
kind: head extends OptionalPropertyDefinition ? PreparsedElement.optionals : head extends DefaultablePropertyTuple ? PreparsedElement.defaultables : isDefaultable<head, $, args> extends true ? PreparsedElement.defaultables : PreparsedElement.required;
|
|
49
|
+
spread: spread;
|
|
50
|
+
}>;
|
|
51
|
+
type parseNextElement<s extends SequenceParseState, $, args> = preparseNextState<s, $, args> extends infer next extends PreparsedElement ? parseNextElement<{
|
|
52
|
+
unscanned: next["tail"];
|
|
53
|
+
inferred: nextInferred<s, next>;
|
|
54
|
+
validated: nextValidated<s, next>;
|
|
55
|
+
phase: next["kind"] extends (SequencePhase.optionals | SequencePhase.defaultables) ? next["kind"] : number extends nextInferred<s, next>["length"] ? s["phase"] : SequencePhase.prefix;
|
|
56
|
+
}, $, args> : s;
|
|
57
|
+
type nextInferred<s extends SequenceParseState, next extends PreparsedElement> = next["spread"] extends true ? [
|
|
58
|
+
...s["inferred"],
|
|
59
|
+
...conform<next["inferred"], array>
|
|
60
|
+
] : next["kind"] extends SequencePhase.optionals ? [
|
|
61
|
+
...s["inferred"],
|
|
62
|
+
next["inferred"]?
|
|
63
|
+
] : [...s["inferred"], next["inferred"]];
|
|
64
|
+
type nextValidated<s extends SequenceParseState, next extends PreparsedElement> = [
|
|
65
|
+
...s["validated"],
|
|
66
|
+
...nextValidatedSpreadOperatorIfPresent<s, next>,
|
|
67
|
+
nextValidatedElement<s, next>
|
|
68
|
+
];
|
|
69
|
+
type nextValidatedSpreadOperatorIfPresent<s extends SequenceParseState, next extends PreparsedElement> = next["spread"] extends true ? [
|
|
70
|
+
next["inferred"] extends infer spreadOperand extends array ? [
|
|
71
|
+
number,
|
|
72
|
+
number
|
|
73
|
+
] extends ([
|
|
74
|
+
s["inferred"]["length"],
|
|
75
|
+
spreadOperand["length"]
|
|
76
|
+
]) ? ErrorMessage<multipleVariadicMessage> : "..." : ErrorMessage<writeNonArraySpreadMessage<next["head"]>>
|
|
77
|
+
] : [];
|
|
78
|
+
type nextValidatedElement<s extends SequenceParseState, next extends PreparsedElement> = next["kind"] extends SequencePhase.optionals ? next["spread"] extends true ? ErrorMessage<spreadOptionalMessage> : s["phase"] extends SequencePhase.postfix ? ErrorMessage<optionalOrDefaultableAfterVariadicMessage> : next["validated"] : next["kind"] extends SequencePhase.defaultables ? next["spread"] extends true ? ErrorMessage<spreadDefaultableMessage> : s["phase"] extends SequencePhase.optionals ? ErrorMessage<defaultablePostOptionalMessage> : s["phase"] extends SequencePhase.postfix ? ErrorMessage<optionalOrDefaultableAfterVariadicMessage> : next["validated"] : [s["phase"], next["spread"]] extends ([
|
|
79
|
+
SequencePhase.optionals | SequencePhase.defaultables,
|
|
80
|
+
false
|
|
81
|
+
]) ? ErrorMessage<postfixAfterOptionalOrDefaultableMessage> : next["validated"];
|
|
82
|
+
export declare const writeNonArraySpreadMessage: <operand extends string>(operand: operand) => writeNonArraySpreadMessage<operand>;
|
|
83
|
+
type writeNonArraySpreadMessage<operand> = `Spread element must be an array${operand extends string ? ` (was ${operand})` : ""}`;
|
|
84
|
+
export declare const multipleVariadicMesage = "A tuple may have at most one variadic element";
|
|
85
|
+
type multipleVariadicMessage = typeof multipleVariadicMesage;
|
|
86
|
+
export declare const requiredPostOptionalMessage = "A required element may not follow an optional element";
|
|
87
|
+
export declare const optionalOrDefaultableAfterVariadicMessage = "An optional element may not follow a variadic element";
|
|
88
|
+
type optionalOrDefaultableAfterVariadicMessage = typeof optionalOrDefaultableAfterVariadicMessage;
|
|
89
|
+
export declare const spreadOptionalMessage = "A spread element cannot be optional";
|
|
90
|
+
type spreadOptionalMessage = typeof spreadOptionalMessage;
|
|
91
|
+
export declare const spreadDefaultableMessage = "A spread element cannot have a default";
|
|
92
|
+
type spreadDefaultableMessage = typeof spreadDefaultableMessage;
|
|
93
|
+
export declare const defaultablePostOptionalMessage = "A defaultable element may not follow an optional element without a default";
|
|
94
|
+
type defaultablePostOptionalMessage = typeof defaultablePostOptionalMessage;
|
|
95
|
+
export {};
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { $ark, makeRootAndArrayPropertiesMutable, postfixAfterOptionalOrDefaultableMessage } from "@ark/schema";
|
|
2
|
+
import { append, isArray, isEmptyObject, throwParseError } from "@ark/util";
|
|
3
|
+
import { parseProperty } from "./property.js";
|
|
4
|
+
export const parseTupleLiteral = (def, ctx) => {
|
|
5
|
+
let sequences = [{}];
|
|
6
|
+
let i = 0;
|
|
7
|
+
while (i < def.length) {
|
|
8
|
+
let spread = false;
|
|
9
|
+
if (def[i] === "..." && i < def.length - 1) {
|
|
10
|
+
spread = true;
|
|
11
|
+
i++;
|
|
12
|
+
}
|
|
13
|
+
const parsedProperty = parseProperty(def[i], ctx);
|
|
14
|
+
const [valueNode, operator, possibleDefaultValue] = !isArray(parsedProperty) ? [parsedProperty] : parsedProperty;
|
|
15
|
+
i++;
|
|
16
|
+
if (spread) {
|
|
17
|
+
if (!valueNode.extends($ark.intrinsic.Array))
|
|
18
|
+
return throwParseError(writeNonArraySpreadMessage(valueNode.expression));
|
|
19
|
+
// a spread must be distributed over branches e.g.:
|
|
20
|
+
// def: [string, ...(number[] | [true, false])]
|
|
21
|
+
// nodes: [string, ...number[]] | [string, true, false]
|
|
22
|
+
sequences = sequences.flatMap(base =>
|
|
23
|
+
// since appendElement mutates base, we have to shallow-ish clone it for each branch
|
|
24
|
+
valueNode.distribute(branch => appendSpreadBranch(makeRootAndArrayPropertiesMutable(base), branch)));
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
sequences = sequences.map(base => {
|
|
28
|
+
if (operator === "?")
|
|
29
|
+
return appendOptionalElement(base, valueNode);
|
|
30
|
+
if (operator === "=")
|
|
31
|
+
return appendDefaultableElement(base, valueNode, possibleDefaultValue);
|
|
32
|
+
return appendRequiredElement(base, valueNode);
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return ctx.$.parseSchema(sequences.map(sequence => isEmptyObject(sequence) ?
|
|
37
|
+
{
|
|
38
|
+
proto: Array,
|
|
39
|
+
exactLength: 0
|
|
40
|
+
}
|
|
41
|
+
: {
|
|
42
|
+
proto: Array,
|
|
43
|
+
sequence
|
|
44
|
+
}));
|
|
45
|
+
};
|
|
46
|
+
const appendRequiredElement = (base, element) => {
|
|
47
|
+
if (base.defaultables || base.optionals) {
|
|
48
|
+
return throwParseError(base.variadic ?
|
|
49
|
+
// e.g. [boolean = true, ...string[], number]
|
|
50
|
+
postfixAfterOptionalOrDefaultableMessage
|
|
51
|
+
// e.g. [string?, number]
|
|
52
|
+
: requiredPostOptionalMessage);
|
|
53
|
+
}
|
|
54
|
+
if (base.variadic) {
|
|
55
|
+
// e.g. [...string[], number]
|
|
56
|
+
base.postfix = append(base.postfix, element);
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
// e.g. [string, number]
|
|
60
|
+
base.prefix = append(base.prefix, element);
|
|
61
|
+
}
|
|
62
|
+
return base;
|
|
63
|
+
};
|
|
64
|
+
const appendOptionalElement = (base, element) => {
|
|
65
|
+
if (base.variadic)
|
|
66
|
+
// e.g. [...string[], number?]
|
|
67
|
+
return throwParseError(optionalOrDefaultableAfterVariadicMessage);
|
|
68
|
+
// e.g. [string, number?]
|
|
69
|
+
base.optionals = append(base.optionals, element);
|
|
70
|
+
return base;
|
|
71
|
+
};
|
|
72
|
+
const appendDefaultableElement = (base, element, value) => {
|
|
73
|
+
if (base.variadic)
|
|
74
|
+
// e.g. [...string[], number = 0]
|
|
75
|
+
return throwParseError(optionalOrDefaultableAfterVariadicMessage);
|
|
76
|
+
if (base.optionals)
|
|
77
|
+
// e.g. [string?, number = 0]
|
|
78
|
+
return throwParseError(defaultablePostOptionalMessage);
|
|
79
|
+
// value's assignability to element will be checked when the
|
|
80
|
+
// sequence is instantiated by @ark/schema
|
|
81
|
+
// e.g. [string, number = 0]
|
|
82
|
+
base.defaultables = append(base.defaultables, [[element, value]]);
|
|
83
|
+
return base;
|
|
84
|
+
};
|
|
85
|
+
const appendVariadicElement = (base, element) => {
|
|
86
|
+
// e.g. [...string[], number, ...string[]]
|
|
87
|
+
if (base.postfix)
|
|
88
|
+
throwParseError(multipleVariadicMesage);
|
|
89
|
+
if (base.variadic) {
|
|
90
|
+
if (!base.variadic.equals(element)) {
|
|
91
|
+
// e.g. [...string[], ...number[]]
|
|
92
|
+
throwParseError(multipleVariadicMesage);
|
|
93
|
+
}
|
|
94
|
+
// e.g. [...string[], ...string[]]
|
|
95
|
+
// do nothing, second spread doesn't change the type
|
|
96
|
+
}
|
|
97
|
+
else {
|
|
98
|
+
// e.g. [string, ...number[]]
|
|
99
|
+
base.variadic = element.internal;
|
|
100
|
+
}
|
|
101
|
+
return base;
|
|
102
|
+
};
|
|
103
|
+
const appendSpreadBranch = (base, branch) => {
|
|
104
|
+
const spread = branch.firstReferenceOfKind("sequence");
|
|
105
|
+
if (!spread) {
|
|
106
|
+
// the only array with no sequence reference is unknown[]
|
|
107
|
+
return appendVariadicElement(base, $ark.intrinsic.unknown);
|
|
108
|
+
}
|
|
109
|
+
spread.prefix?.forEach(node => appendRequiredElement(base, node));
|
|
110
|
+
spread.optionals?.forEach(node => appendOptionalElement(base, node));
|
|
111
|
+
if (spread.variadic)
|
|
112
|
+
appendVariadicElement(base, spread.variadic);
|
|
113
|
+
spread.postfix?.forEach(node => appendRequiredElement(base, node));
|
|
114
|
+
return base;
|
|
115
|
+
};
|
|
116
|
+
export const writeNonArraySpreadMessage = (operand) => `Spread element must be an array (was ${operand})`;
|
|
117
|
+
export const multipleVariadicMesage = "A tuple may have at most one variadic element";
|
|
118
|
+
export const requiredPostOptionalMessage = "A required element may not follow an optional element";
|
|
119
|
+
export const optionalOrDefaultableAfterVariadicMessage = "An optional element may not follow a variadic element";
|
|
120
|
+
export const spreadOptionalMessage = "A spread element cannot be optional";
|
|
121
|
+
export const spreadDefaultableMessage = "A spread element cannot have a default";
|
|
122
|
+
export const defaultablePostOptionalMessage = "A defaultable element may not follow an optional element without a default";
|
package/out/scope.d.ts
CHANGED
|
@@ -1,24 +1,28 @@
|
|
|
1
|
-
import { BaseScope, type AliasDefEntry, type ArkScopeConfig, type BaseNode, type
|
|
2
|
-
import { type ErrorType, type
|
|
1
|
+
import { BaseScope, type AliasDefEntry, type ArkScopeConfig, 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 RootKind, type RootSchema, type arkKind, type exportedNameOf, type nodeOfKind, type reducibleKindOf, type toInternalScope, type writeDuplicateAliasError } from "@ark/schema";
|
|
2
|
+
import { type Brand, type ErrorType, type JsonStructure, type anyOrNever, type array, type flattenListable, type noSuggest } from "@ark/util";
|
|
3
3
|
import type { ArkSchemaRegistry } from "./config.ts";
|
|
4
|
-
import {
|
|
5
|
-
import type { Ark } from "./keywords/
|
|
4
|
+
import { type GenericDeclaration, type GenericParser, type ParameterString, type baseGenericConstraints, type parseGenericParams, type parseValidGenericParams } from "./generic.ts";
|
|
5
|
+
import type { Ark, type } from "./keywords/keywords.ts";
|
|
6
6
|
import type { BoundModule, Module, Submodule, instantiateExport } from "./module.ts";
|
|
7
|
-
import {
|
|
8
|
-
import
|
|
9
|
-
import type {
|
|
10
|
-
import
|
|
11
|
-
|
|
12
|
-
|
|
7
|
+
import type { DefAst, InferredAst } from "./parser/ast/infer.ts";
|
|
8
|
+
import { type inferDefinition } from "./parser/definition.ts";
|
|
9
|
+
import type { ParsedOptionalProperty } from "./parser/property.ts";
|
|
10
|
+
import type { ParsedDefaultableProperty } from "./parser/shift/operator/default.ts";
|
|
11
|
+
import { InternalTypeParser, type DeclarationParser, type DefinitionParser, type EnumeratedTypeParser, type InstanceOfTypeParser, type SchemaParser, type TypeParser, type UnitTypeParser } from "./type.ts";
|
|
12
|
+
/** The convenience properties attached to `scope` */
|
|
13
|
+
export type ScopeParserAttachments = Omit<ScopeParser, never>;
|
|
14
|
+
export interface ScopeParser {
|
|
15
|
+
<const def>(def: scope.validate<def>, config?: ArkScopeConfig): Scope<scope.infer<def>>;
|
|
16
|
+
define: <const def>(def: scope.validate<def>) => def;
|
|
17
|
+
}
|
|
18
|
+
export type ModuleParser = <const def>(def: scope.validate<def>, config?: ArkScopeConfig) => scope.infer<def> extends infer $ ? Module<{
|
|
13
19
|
[k in exportedNameOf<$>]: $[k];
|
|
14
20
|
}> : never;
|
|
15
|
-
export type
|
|
16
|
-
|
|
21
|
+
export type bindThis<def> = {
|
|
22
|
+
this: Def<def>;
|
|
17
23
|
};
|
|
18
|
-
export type inferScope<def> = inferBootstrapped<bootstrapAliases<def>>;
|
|
19
|
-
export type bindThis<def> = {};
|
|
20
24
|
/** nominal type for an unparsed definition used during scope bootstrapping */
|
|
21
|
-
type Def<def = {}> =
|
|
25
|
+
type Def<def = {}> = Brand<def, "unparsed">;
|
|
22
26
|
/** sentinel indicating a scope that will be associated with a generic has not yet been parsed */
|
|
23
27
|
export type UnparsedScope = "$";
|
|
24
28
|
/** These are legal as values of a scope but not as definitions in other contexts */
|
|
@@ -44,7 +48,7 @@ export type resolutionToAst<alias extends string, resolution> = [
|
|
|
44
48
|
resolution
|
|
45
49
|
] extends [anyOrNever] ? InferredAst<resolution, alias> : resolution extends Def<infer def> ? DefAst<def, alias> : resolution extends {
|
|
46
50
|
[arkKind]: "module";
|
|
47
|
-
|
|
51
|
+
root: infer root;
|
|
48
52
|
} ? InferredAst<root, alias> : resolution extends GenericAst ? resolution : InferredAst<resolution, alias>;
|
|
49
53
|
export type moduleKeyOf<$> = {
|
|
50
54
|
[k in keyof $]: $[k] extends {
|
|
@@ -57,52 +61,55 @@ export interface ArkTypeRegistry extends ArkSchemaRegistry {
|
|
|
57
61
|
typeAttachments?: Ark.boundTypeAttachments<any>;
|
|
58
62
|
}
|
|
59
63
|
export declare const $arkTypeRegistry: ArkTypeRegistry;
|
|
60
|
-
export interface ParseContext extends TypeParseOptions {
|
|
61
|
-
$: InternalScope;
|
|
62
|
-
}
|
|
63
|
-
export interface TypeParseOptions {
|
|
64
|
-
args?: GenericArgResolutions;
|
|
65
|
-
}
|
|
66
64
|
export interface InternalScope {
|
|
67
65
|
constructor: typeof InternalScope;
|
|
68
66
|
}
|
|
69
67
|
export declare class InternalScope<$ extends {} = {}> extends BaseScope<$> {
|
|
70
|
-
private parseCache;
|
|
71
|
-
preparseAlias(k: string, v: unknown): AliasDefEntry;
|
|
72
68
|
protected cacheGetter<name extends keyof this>(name: name, value: this[name]): this[name];
|
|
73
|
-
preparseRoot(def: unknown): unknown;
|
|
74
69
|
get ambientAttachments(): Ark.boundTypeAttachments<$> | undefined;
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
70
|
+
protected preparseOwnAliasEntry(k: string, v: unknown): AliasDefEntry;
|
|
71
|
+
parseGenericParams(def: string, opts: BaseParseOptions): array<GenericParamDef>;
|
|
72
|
+
protected normalizeRootScopeValue(resolution: unknown): unknown;
|
|
73
|
+
protected preparseOwnDefinitionFormat(def: unknown, opts: BaseParseOptions): BaseRoot | BaseParseContextInput;
|
|
74
|
+
parseOwnDefinitionFormat(def: unknown, ctx: BaseParseContext): BaseRoot;
|
|
75
|
+
unit: UnitTypeParser<$>;
|
|
76
|
+
enumerated: EnumeratedTypeParser<$>;
|
|
77
|
+
instanceOf: InstanceOfTypeParser<$>;
|
|
78
78
|
type: InternalTypeParser;
|
|
79
79
|
declare: () => {
|
|
80
80
|
type: InternalTypeParser;
|
|
81
81
|
};
|
|
82
|
-
define
|
|
82
|
+
define<def>(def: def): def;
|
|
83
83
|
static scope: ScopeParser;
|
|
84
84
|
static module: ModuleParser;
|
|
85
85
|
}
|
|
86
86
|
export declare const scope: ScopeParser;
|
|
87
|
-
export declare
|
|
87
|
+
export declare namespace scope {
|
|
88
|
+
type validate<def> = {
|
|
89
|
+
[k in keyof def]: k extends noSuggest ? unknown : parseScopeKey<k, def>["params"] extends infer params ? params extends array<GenericParamAst> ? params["length"] extends 0 ? def[k] extends type.Any | PreparsedResolution ? def[k] : k extends (PrivateDeclaration<infer name extends keyof def & string>) ? ErrorType<writeDuplicateAliasError<name>> : type.validate<def[k], bootstrapAliases<def>, {}> : type.validate<def[k], bootstrapAliases<def>, baseGenericConstraints<params>> : params : never;
|
|
90
|
+
};
|
|
91
|
+
type infer<def> = inferBootstrapped<bootstrapAliases<def>>;
|
|
92
|
+
}
|
|
88
93
|
export interface Scope<$ = {}> {
|
|
89
94
|
t: $;
|
|
90
95
|
[arkKind]: "scope";
|
|
91
96
|
config: ArkScopeConfig;
|
|
92
97
|
references: readonly BaseNode[];
|
|
93
|
-
json:
|
|
98
|
+
json: JsonStructure;
|
|
94
99
|
exportedNames: array<exportedNameOf<$>>;
|
|
95
100
|
/** The set of names defined at the root-level of the scope mapped to their
|
|
96
101
|
* corresponding definitions.**/
|
|
97
102
|
aliases: Record<string, unknown>;
|
|
98
103
|
internal: toInternalScope<$>;
|
|
99
104
|
defineSchema<const def extends RootSchema>(schema: def): def;
|
|
100
|
-
|
|
101
|
-
|
|
105
|
+
node<kinds extends NodeKind | array<RootKind>>(kinds: kinds, schema: NodeSchema<flattenListable<kinds>>, opts?: BaseParseOptions): nodeOfKind<reducibleKindOf<flattenListable<kinds>>>;
|
|
106
|
+
unit: UnitTypeParser<$>;
|
|
107
|
+
enumerated: EnumeratedTypeParser<$>;
|
|
102
108
|
type: TypeParser<$>;
|
|
103
109
|
declare: DeclarationParser<$>;
|
|
104
110
|
define: DefinitionParser<$>;
|
|
105
111
|
generic: GenericParser<$>;
|
|
112
|
+
schema: SchemaParser<$>;
|
|
106
113
|
import(): Module<{
|
|
107
114
|
[k in exportedNameOf<$> as PrivateDeclaration<k>]: $[k];
|
|
108
115
|
}>;
|
|
@@ -123,7 +130,6 @@ export interface ScopeConstructor {
|
|
|
123
130
|
module: ModuleParser;
|
|
124
131
|
}
|
|
125
132
|
export declare const Scope: ScopeConstructor;
|
|
126
|
-
export declare const writeShallowCycleErrorMessage: (name: string, seen: string[]) => string;
|
|
127
133
|
export type parseScopeKey<k, def> = k extends `${infer name}<${infer params}>` ? parseGenericScopeKey<name, params, def> : {
|
|
128
134
|
name: k;
|
|
129
135
|
params: [];
|
|
@@ -132,4 +138,5 @@ type parseGenericScopeKey<name extends string, params extends string, def> = {
|
|
|
132
138
|
name: name;
|
|
133
139
|
params: parseGenericParams<params, bootstrapAliases<def>>;
|
|
134
140
|
};
|
|
141
|
+
export type InnerParseResult = BaseRoot | ParsedOptionalProperty | ParsedDefaultableProperty;
|
|
135
142
|
export {};
|
package/out/scope.js
CHANGED
|
@@ -1,16 +1,25 @@
|
|
|
1
1
|
import { $ark, BaseScope, hasArkKind, parseGeneric } from "@ark/schema";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import { Scanner } from "./parser/string/shift/scanner.js";
|
|
8
|
-
import { fullStringParse } from "./parser/string/string.js";
|
|
2
|
+
import { flatMorph, isArray, isThunk, throwParseError } from "@ark/util";
|
|
3
|
+
import { parseGenericParamName } from "./generic.js";
|
|
4
|
+
import { shallowDefaultableMessage, shallowOptionalMessage } from "./parser/ast/validate.js";
|
|
5
|
+
import { parseInnerDefinition } from "./parser/definition.js";
|
|
6
|
+
import { ArkTypeScanner } from "./parser/shift/scanner.js";
|
|
9
7
|
import { InternalTypeParser } from "./type.js";
|
|
10
8
|
export const $arkTypeRegistry = $ark;
|
|
11
9
|
export class InternalScope extends BaseScope {
|
|
12
|
-
|
|
13
|
-
|
|
10
|
+
cacheGetter(name, value) {
|
|
11
|
+
Object.defineProperty(this, name, { value });
|
|
12
|
+
return value;
|
|
13
|
+
}
|
|
14
|
+
get ambientAttachments() {
|
|
15
|
+
if (!$arkTypeRegistry.typeAttachments)
|
|
16
|
+
return;
|
|
17
|
+
return this.cacheGetter("ambientAttachments", flatMorph($arkTypeRegistry.typeAttachments, (k, v) => [
|
|
18
|
+
k,
|
|
19
|
+
this.bindReference(v)
|
|
20
|
+
]));
|
|
21
|
+
}
|
|
22
|
+
preparseOwnAliasEntry(k, v) {
|
|
14
23
|
const firstParamIndex = k.indexOf("<");
|
|
15
24
|
if (firstParamIndex === -1)
|
|
16
25
|
return [k, v];
|
|
@@ -24,74 +33,57 @@ export class InternalScope extends BaseScope {
|
|
|
24
33
|
// use a thunk definition for the generic so that we can parse
|
|
25
34
|
// constraints within the current scope
|
|
26
35
|
() => {
|
|
27
|
-
const params = parseGenericParams(paramString, {
|
|
28
|
-
$: this,
|
|
29
|
-
args: {}
|
|
30
|
-
});
|
|
36
|
+
const params = this.parseGenericParams(paramString, { alias: name });
|
|
31
37
|
const generic = parseGeneric(params, v, this);
|
|
32
38
|
return generic;
|
|
33
39
|
}
|
|
34
40
|
];
|
|
35
41
|
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
return value;
|
|
42
|
+
parseGenericParams(def, opts) {
|
|
43
|
+
return parseGenericParamName(new ArkTypeScanner(def), [], this.createParseContext({ ...opts, def, prefix: "generic" }));
|
|
39
44
|
}
|
|
40
|
-
|
|
41
|
-
if (isThunk(
|
|
42
|
-
return
|
|
43
|
-
return
|
|
45
|
+
normalizeRootScopeValue(resolution) {
|
|
46
|
+
if (isThunk(resolution) && !hasArkKind(resolution, "generic"))
|
|
47
|
+
return resolution();
|
|
48
|
+
return resolution;
|
|
44
49
|
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
]));
|
|
50
|
+
preparseOwnDefinitionFormat(def, opts) {
|
|
51
|
+
return {
|
|
52
|
+
...opts,
|
|
53
|
+
def,
|
|
54
|
+
prefix: opts.alias ?? "type"
|
|
55
|
+
};
|
|
52
56
|
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
|
|
57
|
+
parseOwnDefinitionFormat(def, ctx) {
|
|
58
|
+
const isScopeAlias = ctx.alias && ctx.alias in this.aliases;
|
|
59
|
+
// if the definition being parsed is not a scope alias and is not a
|
|
60
|
+
// generic instantiation (i.e. opts don't include args), add `this` as a resolution.
|
|
61
|
+
// if we're parsing a nested string, ctx.args will have already been set
|
|
62
|
+
if (!isScopeAlias && !ctx.args)
|
|
63
|
+
ctx.args = { this: ctx.id };
|
|
64
|
+
const result = parseInnerDefinition(def, ctx);
|
|
65
|
+
if (isArray(result)) {
|
|
66
|
+
if (result[1] === "=")
|
|
67
|
+
return throwParseError(shallowDefaultableMessage);
|
|
68
|
+
if (result[1] === "?")
|
|
69
|
+
return throwParseError(shallowOptionalMessage);
|
|
62
70
|
}
|
|
63
|
-
return
|
|
64
|
-
parseObject(def, ctx)
|
|
65
|
-
: throwParseError(writeBadDefinitionTypeMessage(domainOf(def)));
|
|
71
|
+
return result;
|
|
66
72
|
}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
return aliasResolution;
|
|
71
|
-
const aliasArrayResolution = def.endsWith("[]") ?
|
|
72
|
-
this.maybeResolveRoot(def.slice(0, -2))?.array()
|
|
73
|
-
: undefined;
|
|
74
|
-
if (aliasArrayResolution)
|
|
75
|
-
return aliasArrayResolution;
|
|
76
|
-
const s = new DynamicState(new Scanner(def), ctx, defaultable);
|
|
77
|
-
const node = fullStringParse(s);
|
|
78
|
-
if (s.finalizer === ">")
|
|
79
|
-
throwParseError(writeUnexpectedCharacterMessage(">"));
|
|
80
|
-
return node;
|
|
81
|
-
}
|
|
82
|
-
parseRoot = (def, opts = {}) => {
|
|
83
|
-
const node = this.parse(def, Object.assign(this.finalizeRootArgs(opts, () => node), { $: this })).bindScope(this);
|
|
84
|
-
return node;
|
|
85
|
-
};
|
|
73
|
+
unit = value => this.units([value]);
|
|
74
|
+
enumerated = (...values) => this.units(values);
|
|
75
|
+
instanceOf = ctor => this.node("proto", { proto: ctor }, { prereduced: true });
|
|
86
76
|
type = new InternalTypeParser(this);
|
|
87
77
|
declare = () => ({
|
|
88
78
|
type: this.type
|
|
89
79
|
});
|
|
90
|
-
define
|
|
80
|
+
define(def) {
|
|
81
|
+
return def;
|
|
82
|
+
}
|
|
91
83
|
static scope = ((def, config = {}) => new InternalScope(def, config));
|
|
92
84
|
static module = ((def, config = {}) => this.scope(def, config).export());
|
|
93
85
|
}
|
|
94
|
-
export const scope = InternalScope.scope
|
|
95
|
-
|
|
86
|
+
export const scope = Object.assign(InternalScope.scope, {
|
|
87
|
+
define: def => def
|
|
88
|
+
});
|
|
96
89
|
export const Scope = InternalScope;
|
|
97
|
-
export const writeShallowCycleErrorMessage = (name, seen) => `Alias '${name}' has a shallow resolution cycle: ${[...seen, name].join(":")}`;
|
package/out/type.d.ts
CHANGED
|
@@ -1,25 +1,52 @@
|
|
|
1
|
-
import { ArkErrors, BaseRoot, type MetaSchema, type Morph, type Predicate } from "@ark/schema";
|
|
2
|
-
import { Callable, type Constructor, type array, type conform } from "@ark/util";
|
|
3
|
-
import
|
|
4
|
-
import type {
|
|
5
|
-
import type {
|
|
6
|
-
import type {
|
|
7
|
-
import type {
|
|
1
|
+
import { ArkErrors, BaseRoot, type BaseParseOptions, type MetaSchema, type Morph, type Predicate, type RootSchema } from "@ark/schema";
|
|
2
|
+
import { Callable, Hkt, type Constructor, type array, type conform } from "@ark/util";
|
|
3
|
+
import type { distill } from "./attributes.ts";
|
|
4
|
+
import type { Generic, GenericParser, ParameterString, baseGenericConstraints, parseValidGenericParams, validateParameterString } from "./generic.ts";
|
|
5
|
+
import type { Ark, keywords, type } from "./keywords/keywords.ts";
|
|
6
|
+
import type { BaseType } from "./methods/base.ts";
|
|
7
|
+
import type { instantiateType } from "./methods/instantiate.ts";
|
|
8
|
+
import type { validateDeclared } from "./parser/definition.ts";
|
|
9
|
+
import type { IndexOneOperator, IndexZeroOperator, TupleInfixOperator } from "./parser/tupleExpressions.ts";
|
|
8
10
|
import type { InternalScope, ModuleParser, Scope, ScopeParser, bindThis } from "./scope.ts";
|
|
9
|
-
import type { BaseType } from "./subtypes/base.ts";
|
|
10
|
-
import type { instantiateType } from "./subtypes/instantiate.ts";
|
|
11
11
|
/** The convenience properties attached to `type` */
|
|
12
12
|
export type TypeParserAttachments = Omit<TypeParser, never>;
|
|
13
13
|
export interface TypeParser<$ = {}> extends Ark.boundTypeAttachments<$> {
|
|
14
|
-
<const def, r = Type<
|
|
15
|
-
<params extends ParameterString, const def>(params: validateParameterString<params, $>, def:
|
|
16
|
-
<const zero, const one, const rest extends array, r = Type<
|
|
17
|
-
|
|
14
|
+
<const def, r = Type<type.infer<def, $>, $>>(def: type.validate<def, $>): r;
|
|
15
|
+
<const params extends ParameterString, const def>(params: validateParameterString<params, $>, def: type.validate<def, $, baseGenericConstraints<parseValidGenericParams<params, $>>>): Generic<parseValidGenericParams<params, $>, def, $>;
|
|
16
|
+
<const zero, const one, const rest extends array, r = Type<type.infer<[zero, one, ...rest], $>, $>>(_0: zero extends IndexZeroOperator ? zero : type.validate<zero, $>, _1: zero extends "keyof" ? type.validate<one, $> : zero extends "instanceof" ? conform<one, Constructor> : zero extends "===" ? conform<one, unknown> : conform<one, IndexOneOperator>, ..._2: zero extends "===" ? rest : zero extends "instanceof" ? conform<rest, readonly Constructor[]> : one extends TupleInfixOperator ? one extends ":" ? [Predicate<distill.In<type.infer<zero, $>>>] : one extends "=>" ? [Morph<distill.Out<type.infer<zero, $>>, unknown>] : one extends "@" ? [MetaSchema] : [type.validate<rest[0], $>] : []): r;
|
|
17
|
+
/**
|
|
18
|
+
* Error class for validation errors
|
|
19
|
+
* Calling type instance returns an instance of this class on failure
|
|
20
|
+
* @example if ( T(data) instanceof type.errors ) { ... }
|
|
21
|
+
*/
|
|
18
22
|
errors: typeof ArkErrors;
|
|
23
|
+
hkt: typeof Hkt;
|
|
24
|
+
keywords: typeof keywords;
|
|
25
|
+
$: Scope<$>;
|
|
26
|
+
raw(def: unknown): BaseType<any, $>;
|
|
19
27
|
module: ModuleParser;
|
|
20
28
|
scope: ScopeParser;
|
|
29
|
+
define: DefinitionParser<$>;
|
|
21
30
|
generic: GenericParser<$>;
|
|
22
|
-
|
|
31
|
+
schema: SchemaParser<$>;
|
|
32
|
+
/**
|
|
33
|
+
* Create a `Type` that is satisfied only by a value strictly equal (`===`) to the argument passed to this function.
|
|
34
|
+
* @example const foo = type.unit('foo') // Type<'foo'>
|
|
35
|
+
* @example const sym: unique symbol = Symbol(); type.unit(sym) // Type<typeof sym>
|
|
36
|
+
*/
|
|
37
|
+
unit: UnitTypeParser<$>;
|
|
38
|
+
/**
|
|
39
|
+
* Create a `Type` that is satisfied only by a value strictly equal (`===`) to one of the arguments passed to this function.
|
|
40
|
+
* @example const enum = type.enumerated('foo', 'bar', obj) // obj is a by-reference object
|
|
41
|
+
* @example const tupleForm = type(['===', 'foo', 'bar', obj])
|
|
42
|
+
* @example const argsForm = type('===', 'foo', 'bar', obj)
|
|
43
|
+
*/
|
|
44
|
+
enumerated: EnumeratedTypeParser<$>;
|
|
45
|
+
/**
|
|
46
|
+
* Create a `Type` that is satisfied only by a value of a specific class.
|
|
47
|
+
* @example const array = type.instanceOf(Array)
|
|
48
|
+
*/
|
|
49
|
+
instanceOf: InstanceOfTypeParser<$>;
|
|
23
50
|
}
|
|
24
51
|
export declare class InternalTypeParser extends Callable<(...args: unknown[]) => BaseRoot | Generic, TypeParserAttachments> {
|
|
25
52
|
constructor($: InternalScope);
|
|
@@ -27,14 +54,11 @@ export declare class InternalTypeParser extends Callable<(...args: unknown[]) =>
|
|
|
27
54
|
export type DeclarationParser<$> = <preinferred>() => {
|
|
28
55
|
type: <const def>(def: validateDeclared<preinferred, def, $, bindThis<def>>) => Type<preinferred, $>;
|
|
29
56
|
};
|
|
30
|
-
export type
|
|
31
|
-
export type
|
|
32
|
-
export type
|
|
33
|
-
export type
|
|
34
|
-
export type
|
|
57
|
+
export type UnitTypeParser<$> = <const t>(value: t) => Type<t, $>;
|
|
58
|
+
export type InstanceOfTypeParser<$> = <const t extends object>(ctor: Constructor<t>) => Type<t, $>;
|
|
59
|
+
export type EnumeratedTypeParser<$> = <const values extends readonly unknown[]>(...values: values) => Type<values[number], $>;
|
|
60
|
+
export type DefinitionParser<$> = <const def>(def: type.validate<def, $>) => def;
|
|
61
|
+
export type SchemaParser<$> = (schema: RootSchema, opts?: BaseParseOptions) => Type<unknown, $>;
|
|
35
62
|
export type Type<t = unknown, $ = {}> = instantiateType<t, $>;
|
|
36
|
-
export declare namespace Type {
|
|
37
|
-
type Any<t = any> = BaseType<t, any>;
|
|
38
|
-
}
|
|
39
63
|
export type TypeConstructor<t = unknown, $ = {}> = new (def: unknown, $: Scope<$>) => Type<t, $>;
|
|
40
64
|
export declare const Type: TypeConstructor;
|