arktype 2.0.0-rc.6 → 2.0.0-rc.8
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/generic.d.ts +8 -9
- package/out/generic.js +3 -5
- package/out/index.d.ts +1 -1
- package/out/keys.d.ts +5 -3
- package/out/keywords/ark.d.ts +6 -1
- package/out/keywords/ark.js +1 -0
- package/out/keywords/ast.d.ts +27 -17
- package/out/keywords/constructors/Array.js +3 -3
- package/out/keywords/constructors/FormData.js +5 -5
- package/out/keywords/constructors/constructors.d.ts +0 -1
- package/out/keywords/number/epoch.js +2 -2
- package/out/keywords/number/integer.js +2 -2
- package/out/keywords/number/number.js +2 -2
- package/out/keywords/string/capitalize.js +2 -2
- package/out/keywords/string/creditCard.js +2 -2
- package/out/keywords/string/date.js +11 -11
- package/out/keywords/string/integer.js +2 -2
- package/out/keywords/string/ip.js +2 -2
- package/out/keywords/string/json.d.ts +1 -0
- package/out/keywords/string/json.js +21 -13
- package/out/keywords/string/lower.js +2 -2
- package/out/keywords/string/normalize.js +3 -3
- package/out/keywords/string/numeric.js +2 -2
- package/out/keywords/string/string.d.ts +1 -1
- package/out/keywords/string/trim.js +2 -2
- package/out/keywords/string/upper.js +2 -2
- package/out/keywords/string/url.js +4 -4
- package/out/methods/base.d.ts +10 -5
- package/out/methods/instantiate.d.ts +2 -1
- package/out/methods/object.d.ts +31 -6
- package/out/parser/definition.d.ts +4 -5
- package/out/parser/objectLiteral.d.ts +4 -5
- package/out/parser/objectLiteral.js +4 -4
- package/out/parser/semantic/infer.d.ts +4 -3
- package/out/parser/semantic/validate.d.ts +1 -1
- package/out/parser/string/reduce/dynamic.d.ts +3 -4
- package/out/parser/string/reduce/dynamic.js +3 -2
- package/out/parser/string/shift/operand/unenclosed.js +6 -2
- package/out/parser/string/shift/scanner.d.ts +3 -1
- package/out/parser/string/shift/scanner.js +5 -3
- package/out/parser/string/string.d.ts +1 -1
- package/out/parser/string/string.js +2 -0
- package/out/parser/tuple.d.ts +47 -36
- package/out/parser/tuple.js +24 -25
- package/out/scope.d.ts +22 -25
- package/out/scope.js +33 -29
- package/out/type.d.ts +10 -9
- package/out/type.js +9 -9
- package/package.json +3 -3
package/out/parser/tuple.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { $ark, makeRootAndArrayPropertiesMutable } from "@ark/schema";
|
|
1
|
+
import { $ark, Disjoint, intersectNodesRoot, makeRootAndArrayPropertiesMutable } from "@ark/schema";
|
|
2
2
|
import { append, isEmptyObject, objectKindOrDomainOf, throwParseError } from "@ark/util";
|
|
3
3
|
import { writeMissingRightOperandMessage } from "./string/shift/operand/unenclosed.js";
|
|
4
4
|
export const parseTuple = (def, ctx) => maybeParseTupleExpression(def, ctx) ?? parseTupleLiteral(def, ctx);
|
|
@@ -7,19 +7,12 @@ export const parseTupleLiteral = (def, ctx) => {
|
|
|
7
7
|
let i = 0;
|
|
8
8
|
while (i < def.length) {
|
|
9
9
|
let spread = false;
|
|
10
|
-
let optional = false;
|
|
11
10
|
if (def[i] === "..." && i < def.length - 1) {
|
|
12
11
|
spread = true;
|
|
13
12
|
i++;
|
|
14
13
|
}
|
|
15
|
-
const element = ctx.$.
|
|
14
|
+
const element = ctx.$.parseOwnDefinitionFormat(def[i], ctx);
|
|
16
15
|
i++;
|
|
17
|
-
if (def[i] === "?") {
|
|
18
|
-
if (spread)
|
|
19
|
-
return throwParseError(spreadOptionalMessage);
|
|
20
|
-
optional = true;
|
|
21
|
-
i++;
|
|
22
|
-
}
|
|
23
16
|
if (spread) {
|
|
24
17
|
if (!element.extends($ark.intrinsic.Array))
|
|
25
18
|
return throwParseError(writeNonArraySpreadMessage(element.expression));
|
|
@@ -31,10 +24,10 @@ export const parseTupleLiteral = (def, ctx) => {
|
|
|
31
24
|
element.distribute(branch => appendSpreadBranch(makeRootAndArrayPropertiesMutable(base), branch)));
|
|
32
25
|
}
|
|
33
26
|
else {
|
|
34
|
-
sequences = sequences.map(base => appendElement(base, optional ? "optional" : "required", element));
|
|
27
|
+
sequences = sequences.map(base => appendElement(base, element.meta.optional ? "optional" : "required", element));
|
|
35
28
|
}
|
|
36
29
|
}
|
|
37
|
-
return ctx.$.
|
|
30
|
+
return ctx.$.parseSchema(sequences.map(sequence => isEmptyObject(sequence) ?
|
|
38
31
|
{
|
|
39
32
|
proto: Array,
|
|
40
33
|
exactLength: 0
|
|
@@ -91,11 +84,11 @@ const appendSpreadBranch = (base, branch) => {
|
|
|
91
84
|
// the only array with no sequence reference is unknown[]
|
|
92
85
|
return appendElement(base, "variadic", $ark.intrinsic.unknown);
|
|
93
86
|
}
|
|
94
|
-
spread.prefix
|
|
95
|
-
spread.optionals
|
|
87
|
+
spread.prefix?.forEach(node => appendElement(base, "required", node));
|
|
88
|
+
spread.optionals?.forEach(node => appendElement(base, "optional", node));
|
|
96
89
|
if (spread.variadic)
|
|
97
90
|
appendElement(base, "variadic", spread.variadic);
|
|
98
|
-
spread.postfix
|
|
91
|
+
spread.postfix?.forEach(node => appendElement(base, "required", node));
|
|
99
92
|
return base;
|
|
100
93
|
};
|
|
101
94
|
const maybeParseTupleExpression = (def, ctx) => {
|
|
@@ -109,36 +102,42 @@ export const multipleVariadicMesage = "A tuple may have at most one variadic ele
|
|
|
109
102
|
export const requiredPostOptionalMessage = "A required element may not follow an optional element";
|
|
110
103
|
export const optionalPostVariadicMessage = "An optional element may not follow a variadic element";
|
|
111
104
|
export const spreadOptionalMessage = "A spread element cannot be optional";
|
|
112
|
-
export const parseKeyOfTuple = (def, ctx) => ctx.$.
|
|
105
|
+
export const parseKeyOfTuple = (def, ctx) => ctx.$.parseOwnDefinitionFormat(def[1], ctx).keyof();
|
|
113
106
|
const parseBranchTuple = (def, ctx) => {
|
|
114
107
|
if (def[2] === undefined)
|
|
115
108
|
return throwParseError(writeMissingRightOperandMessage(def[1], ""));
|
|
116
|
-
const l = ctx.$.
|
|
117
|
-
const r = ctx.$.
|
|
118
|
-
|
|
109
|
+
const l = ctx.$.parseOwnDefinitionFormat(def[0], ctx);
|
|
110
|
+
const r = ctx.$.parseOwnDefinitionFormat(def[2], ctx);
|
|
111
|
+
if (def[1] === "|")
|
|
112
|
+
return ctx.$.node("union", { branches: [l, r] });
|
|
113
|
+
const result = intersectNodesRoot(l, r, ctx.$);
|
|
114
|
+
if (result instanceof Disjoint)
|
|
115
|
+
return result.throw();
|
|
116
|
+
return result;
|
|
119
117
|
};
|
|
120
|
-
const parseArrayTuple = (def, ctx) => ctx.$.
|
|
118
|
+
const parseArrayTuple = (def, ctx) => ctx.$.parseOwnDefinitionFormat(def[0], ctx).array();
|
|
121
119
|
const isIndexOneExpression = (def) => indexOneParsers[def[1]] !== undefined;
|
|
122
120
|
export const parseMorphTuple = (def, ctx) => {
|
|
123
121
|
if (typeof def[2] !== "function") {
|
|
124
122
|
return throwParseError(writeMalformedFunctionalExpressionMessage("=>", def[2]));
|
|
125
123
|
}
|
|
126
|
-
|
|
127
|
-
return ctx.$.parse(def[0], ctx).pipe(def[2]);
|
|
124
|
+
return ctx.$.parseOwnDefinitionFormat(def[0], ctx).pipe(def[2]);
|
|
128
125
|
};
|
|
129
126
|
export const writeMalformedFunctionalExpressionMessage = (operator, value) => `${operator === ":" ? "Narrow" : "Morph"} expression requires a function following '${operator}' (was ${typeof value})`;
|
|
130
127
|
export const parseNarrowTuple = (def, ctx) => {
|
|
131
128
|
if (typeof def[2] !== "function") {
|
|
132
129
|
return throwParseError(writeMalformedFunctionalExpressionMessage(":", def[2]));
|
|
133
130
|
}
|
|
134
|
-
return ctx.$.
|
|
131
|
+
return ctx.$.parseOwnDefinitionFormat(def[0], ctx).constrain("predicate", def[2]);
|
|
135
132
|
};
|
|
136
|
-
const parseAttributeTuple = (def, ctx) => ctx.$.
|
|
137
|
-
const parseDefaultTuple = (def, ctx) => ctx.$.
|
|
133
|
+
const parseAttributeTuple = (def, ctx) => ctx.$.parseOwnDefinitionFormat(def[0], ctx).configureShallowDescendants(def[2]);
|
|
134
|
+
const parseDefaultTuple = (def, ctx) => ctx.$.parseOwnDefinitionFormat(def[0], ctx).default(def[2]);
|
|
135
|
+
const parseOptionalTuple = (def, ctx) => ctx.$.parseOwnDefinitionFormat(def[0], ctx).optional();
|
|
138
136
|
const indexOneParsers = {
|
|
137
|
+
"[]": parseArrayTuple,
|
|
138
|
+
"?": parseOptionalTuple,
|
|
139
139
|
"|": parseBranchTuple,
|
|
140
140
|
"&": parseBranchTuple,
|
|
141
|
-
"[]": parseArrayTuple,
|
|
142
141
|
":": parseNarrowTuple,
|
|
143
142
|
"=>": parseMorphTuple,
|
|
144
143
|
"@": parseAttributeTuple,
|
package/out/scope.d.ts
CHANGED
|
@@ -1,21 +1,19 @@
|
|
|
1
|
-
import { BaseScope, type AliasDefEntry, type ArkScopeConfig, type BaseNode, 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
2
|
import { type ErrorType, type Json, type anyOrNever, type array, type flattenListable, type noSuggest, type nominal } from "@ark/util";
|
|
3
3
|
import type { ArkSchemaRegistry } from "./config.ts";
|
|
4
|
-
import {
|
|
5
|
-
import type { Ark } from "./keywords/ark.ts";
|
|
4
|
+
import { type GenericDeclaration, type GenericParser, type ParameterString, type baseGenericConstraints, type parseGenericParams, type parseValidGenericParams } from "./generic.ts";
|
|
5
|
+
import type { Ark, type } from "./keywords/ark.ts";
|
|
6
6
|
import type { BoundModule, Module, Submodule, instantiateExport } from "./module.ts";
|
|
7
7
|
import { type inferDefinition, type validateDefinition } from "./parser/definition.ts";
|
|
8
8
|
import type { DefAst, InferredAst } from "./parser/semantic/infer.ts";
|
|
9
|
-
import { InternalTypeParser, type DeclarationParser, type DefinitionParser, type EnumeratedTypeParser, type
|
|
10
|
-
export type ScopeParser = <const def>(def:
|
|
11
|
-
export type ModuleParser = <const def>(def:
|
|
9
|
+
import { InternalTypeParser, type DeclarationParser, type DefinitionParser, type EnumeratedTypeParser, type SchemaParser, type TypeParser, type UnitTypeParser } from "./type.ts";
|
|
10
|
+
export type ScopeParser = <const def>(def: scope.validate<def>, config?: ArkScopeConfig) => Scope<scope.infer<def>>;
|
|
11
|
+
export type ModuleParser = <const def>(def: scope.validate<def>, config?: ArkScopeConfig) => scope.infer<def> extends infer $ ? Module<{
|
|
12
12
|
[k in exportedNameOf<$>]: $[k];
|
|
13
13
|
}> : never;
|
|
14
|
-
export type
|
|
15
|
-
|
|
14
|
+
export type bindThis<def> = {
|
|
15
|
+
this: Def<def>;
|
|
16
16
|
};
|
|
17
|
-
export type inferScope<def> = inferBootstrapped<bootstrapAliases<def>>;
|
|
18
|
-
export type bindThis<def> = {};
|
|
19
17
|
/** nominal type for an unparsed definition used during scope bootstrapping */
|
|
20
18
|
type Def<def = {}> = nominal<def, "unparsed">;
|
|
21
19
|
/** sentinel indicating a scope that will be associated with a generic has not yet been parsed */
|
|
@@ -56,25 +54,19 @@ export interface ArkTypeRegistry extends ArkSchemaRegistry {
|
|
|
56
54
|
typeAttachments?: Ark.boundTypeAttachments<any>;
|
|
57
55
|
}
|
|
58
56
|
export declare const $arkTypeRegistry: ArkTypeRegistry;
|
|
59
|
-
export interface ParseContext extends TypeParseOptions {
|
|
60
|
-
$: InternalScope;
|
|
61
|
-
}
|
|
62
|
-
export interface TypeParseOptions {
|
|
63
|
-
args?: GenericArgResolutions;
|
|
64
|
-
}
|
|
65
57
|
export interface InternalScope {
|
|
66
58
|
constructor: typeof InternalScope;
|
|
67
59
|
}
|
|
68
60
|
export declare class InternalScope<$ extends {} = {}> extends BaseScope<$> {
|
|
69
61
|
private parseCache;
|
|
70
|
-
preparseAlias(k: string, v: unknown): AliasDefEntry;
|
|
71
62
|
protected cacheGetter<name extends keyof this>(name: name, value: this[name]): this[name];
|
|
72
|
-
preparseRoot(def: unknown): unknown;
|
|
73
63
|
get ambientAttachments(): Ark.boundTypeAttachments<$> | undefined;
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
64
|
+
protected preparseOwnAliasEntry(k: string, v: unknown): AliasDefEntry;
|
|
65
|
+
parseGenericParams(def: string, opts: BaseParseOptions): array<GenericParamDef>;
|
|
66
|
+
protected normalizeRootScopeValue(resolution: unknown): unknown;
|
|
67
|
+
protected preparseOwnDefinitionFormat(def: unknown, opts: BaseParseOptions): BaseRoot | BaseParseContextInput;
|
|
68
|
+
parseOwnDefinitionFormat(def: unknown, ctx: BaseParseContext): BaseRoot;
|
|
69
|
+
parseString(def: string, ctx: BaseParseContext): BaseRoot;
|
|
78
70
|
unit: UnitTypeParser<$>;
|
|
79
71
|
enumerated: EnumeratedTypeParser<$>;
|
|
80
72
|
type: InternalTypeParser;
|
|
@@ -86,6 +78,12 @@ export declare class InternalScope<$ extends {} = {}> extends BaseScope<$> {
|
|
|
86
78
|
static module: ModuleParser;
|
|
87
79
|
}
|
|
88
80
|
export declare const scope: ScopeParser;
|
|
81
|
+
export declare namespace scope {
|
|
82
|
+
type validate<def> = {
|
|
83
|
+
[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>> : validateDefinition<def[k], bootstrapAliases<def>, {}> : validateDefinition<def[k], bootstrapAliases<def>, baseGenericConstraints<params>> : params : never;
|
|
84
|
+
};
|
|
85
|
+
type infer<def> = inferBootstrapped<bootstrapAliases<def>>;
|
|
86
|
+
}
|
|
89
87
|
export declare const module: ModuleParser;
|
|
90
88
|
export interface Scope<$ = {}> {
|
|
91
89
|
t: $;
|
|
@@ -99,14 +97,14 @@ export interface Scope<$ = {}> {
|
|
|
99
97
|
aliases: Record<string, unknown>;
|
|
100
98
|
internal: toInternalScope<$>;
|
|
101
99
|
defineSchema<const def extends RootSchema>(schema: def): def;
|
|
102
|
-
|
|
103
|
-
node<kinds extends NodeKind | array<RootKind>>(kinds: kinds, schema: NodeSchema<flattenListable<kinds>>, opts?: NodeParseOptions): nodeOfKind<reducibleKindOf<flattenListable<kinds>>>;
|
|
100
|
+
node<kinds extends NodeKind | array<RootKind>>(kinds: kinds, schema: NodeSchema<flattenListable<kinds>>, opts?: BaseParseOptions): nodeOfKind<reducibleKindOf<flattenListable<kinds>>>;
|
|
104
101
|
unit: UnitTypeParser<$>;
|
|
105
102
|
enumerated: EnumeratedTypeParser<$>;
|
|
106
103
|
type: TypeParser<$>;
|
|
107
104
|
declare: DeclarationParser<$>;
|
|
108
105
|
define: DefinitionParser<$>;
|
|
109
106
|
generic: GenericParser<$>;
|
|
107
|
+
schema: SchemaParser<$>;
|
|
110
108
|
import(): Module<{
|
|
111
109
|
[k in exportedNameOf<$> as PrivateDeclaration<k>]: $[k];
|
|
112
110
|
}>;
|
|
@@ -127,7 +125,6 @@ export interface ScopeConstructor {
|
|
|
127
125
|
module: ModuleParser;
|
|
128
126
|
}
|
|
129
127
|
export declare const Scope: ScopeConstructor;
|
|
130
|
-
export declare const writeShallowCycleErrorMessage: (name: string, seen: string[]) => string;
|
|
131
128
|
export type parseScopeKey<k, def> = k extends `${infer name}<${infer params}>` ? parseGenericScopeKey<name, params, def> : {
|
|
132
129
|
name: k;
|
|
133
130
|
params: [];
|
package/out/scope.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { $ark, BaseScope, hasArkKind, parseGeneric } from "@ark/schema";
|
|
2
2
|
import { domainOf, flatMorph, hasDomain, isThunk, throwParseError } from "@ark/util";
|
|
3
|
-
import {
|
|
3
|
+
import { parseGenericParamName } from "./generic.js";
|
|
4
4
|
import { parseObject, writeBadDefinitionTypeMessage } from "./parser/definition.js";
|
|
5
5
|
import { DynamicState } from "./parser/string/reduce/dynamic.js";
|
|
6
6
|
import { writeUnexpectedCharacterMessage } from "./parser/string/shift/operator/operator.js";
|
|
@@ -10,7 +10,19 @@ import { InternalTypeParser } from "./type.js";
|
|
|
10
10
|
export const $arkTypeRegistry = $ark;
|
|
11
11
|
export class InternalScope extends BaseScope {
|
|
12
12
|
parseCache = {};
|
|
13
|
-
|
|
13
|
+
cacheGetter(name, value) {
|
|
14
|
+
Object.defineProperty(this, name, { value });
|
|
15
|
+
return value;
|
|
16
|
+
}
|
|
17
|
+
get ambientAttachments() {
|
|
18
|
+
if (!$arkTypeRegistry.typeAttachments)
|
|
19
|
+
return;
|
|
20
|
+
return this.cacheGetter("ambientAttachments", flatMorph($arkTypeRegistry.typeAttachments, (k, v) => [
|
|
21
|
+
k,
|
|
22
|
+
this.bindReference(v)
|
|
23
|
+
]));
|
|
24
|
+
}
|
|
25
|
+
preparseOwnAliasEntry(k, v) {
|
|
14
26
|
const firstParamIndex = k.indexOf("<");
|
|
15
27
|
if (firstParamIndex === -1)
|
|
16
28
|
return [k, v];
|
|
@@ -24,36 +36,33 @@ export class InternalScope extends BaseScope {
|
|
|
24
36
|
// use a thunk definition for the generic so that we can parse
|
|
25
37
|
// constraints within the current scope
|
|
26
38
|
() => {
|
|
27
|
-
const params = parseGenericParams(paramString, {
|
|
28
|
-
$: this,
|
|
29
|
-
args: {}
|
|
30
|
-
});
|
|
39
|
+
const params = this.parseGenericParams(paramString, { alias: name });
|
|
31
40
|
const generic = parseGeneric(params, v, this);
|
|
32
41
|
return generic;
|
|
33
42
|
}
|
|
34
43
|
];
|
|
35
44
|
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
return value;
|
|
45
|
+
parseGenericParams(def, opts) {
|
|
46
|
+
return parseGenericParamName(new Scanner(def), [], this.createParseContext({ ...opts, def, prefix: "generic" }));
|
|
39
47
|
}
|
|
40
|
-
|
|
41
|
-
if (isThunk(
|
|
42
|
-
return
|
|
43
|
-
return
|
|
44
|
-
}
|
|
45
|
-
get ambientAttachments() {
|
|
46
|
-
if (!$arkTypeRegistry.typeAttachments)
|
|
47
|
-
return;
|
|
48
|
-
return this.cacheGetter("ambientAttachments", flatMorph($arkTypeRegistry.typeAttachments, (k, v) => [
|
|
49
|
-
k,
|
|
50
|
-
this.bindReference(v)
|
|
51
|
-
]));
|
|
48
|
+
normalizeRootScopeValue(resolution) {
|
|
49
|
+
if (isThunk(resolution) && !hasArkKind(resolution, "generic"))
|
|
50
|
+
return resolution();
|
|
51
|
+
return resolution;
|
|
52
52
|
}
|
|
53
|
-
|
|
54
|
-
return
|
|
53
|
+
preparseOwnDefinitionFormat(def, opts) {
|
|
54
|
+
return {
|
|
55
|
+
...opts,
|
|
56
|
+
def,
|
|
57
|
+
prefix: opts.alias ?? "type"
|
|
58
|
+
};
|
|
55
59
|
}
|
|
56
|
-
|
|
60
|
+
parseOwnDefinitionFormat(def, ctx) {
|
|
61
|
+
const isScopeAlias = ctx.alias && ctx.alias in this.aliases;
|
|
62
|
+
// if the definition being parsed is not a scope alias and is not a
|
|
63
|
+
// generic instantiation (i.e. opts don't include args), add this as a resolution.
|
|
64
|
+
if (!isScopeAlias && !ctx.args)
|
|
65
|
+
ctx.args = { this: ctx.id };
|
|
57
66
|
if (typeof def === "string") {
|
|
58
67
|
if (ctx.args && Object.keys(ctx.args).some(k => def.includes(k))) {
|
|
59
68
|
// we can only rely on the cache if there are no contextual
|
|
@@ -81,10 +90,6 @@ export class InternalScope extends BaseScope {
|
|
|
81
90
|
throwParseError(writeUnexpectedCharacterMessage(">"));
|
|
82
91
|
return node;
|
|
83
92
|
}
|
|
84
|
-
parseRoot = (def, opts = {}) => {
|
|
85
|
-
const node = this.parse(def, Object.assign(this.finalizeRootArgs(opts, () => node), { $: this }));
|
|
86
|
-
return node;
|
|
87
|
-
};
|
|
88
93
|
unit = value => this.units([value]);
|
|
89
94
|
enumerated = (...values) => this.units(values);
|
|
90
95
|
type = new InternalTypeParser(this);
|
|
@@ -98,4 +103,3 @@ export class InternalScope extends BaseScope {
|
|
|
98
103
|
export const scope = InternalScope.scope;
|
|
99
104
|
export const module = InternalScope.module;
|
|
100
105
|
export const Scope = InternalScope;
|
|
101
|
-
export const writeShallowCycleErrorMessage = (name, seen) => `Alias '${name}' has a shallow resolution cycle: ${[...seen, name].join(":")}`;
|
package/out/type.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
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 {
|
|
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 { Generic, GenericParser, ParameterString, baseGenericConstraints, parseValidGenericParams, validateParameterString } from "./generic.ts";
|
|
4
4
|
import type { Ark, ark, type } from "./keywords/ark.ts";
|
|
5
5
|
import type { distill } from "./keywords/ast.ts";
|
|
6
6
|
import type { BaseType } from "./methods/base.ts";
|
|
@@ -12,15 +12,18 @@ import type { InternalScope, ModuleParser, Scope, ScopeParser, bindThis } from "
|
|
|
12
12
|
export type TypeParserAttachments = Omit<TypeParser, never>;
|
|
13
13
|
export interface TypeParser<$ = {}> extends Ark.boundTypeAttachments<$> {
|
|
14
14
|
<const def, r = Type<type.infer<def, $>, $>>(def: type.validate<def, $>): r;
|
|
15
|
-
<params extends ParameterString, const def>(params: validateParameterString<params, $>, def: validateDefinition<def, $, baseGenericConstraints<parseValidGenericParams<params, $>>>): Generic<parseValidGenericParams<params, $>, def, $>;
|
|
15
|
+
<const params extends ParameterString, const def>(params: validateParameterString<params, $>, def: validateDefinition<def, $, baseGenericConstraints<parseValidGenericParams<params, $>>>): Generic<parseValidGenericParams<params, $>, def, $>;
|
|
16
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
17
|
$: Scope<$>;
|
|
18
18
|
raw(def: unknown): BaseType<any, $>;
|
|
19
19
|
errors: typeof ArkErrors;
|
|
20
|
+
hkt: typeof Hkt;
|
|
21
|
+
ark: typeof ark;
|
|
20
22
|
module: ModuleParser;
|
|
21
23
|
scope: ScopeParser;
|
|
24
|
+
define: DefinitionParser<$>;
|
|
22
25
|
generic: GenericParser<$>;
|
|
23
|
-
|
|
26
|
+
schema: SchemaParser<$>;
|
|
24
27
|
unit: UnitTypeParser<$>;
|
|
25
28
|
enumerated: EnumeratedTypeParser<$>;
|
|
26
29
|
}
|
|
@@ -32,10 +35,8 @@ export type DeclarationParser<$> = <preinferred>() => {
|
|
|
32
35
|
};
|
|
33
36
|
export type UnitTypeParser<$> = <const t>(value: t) => Type<t, $>;
|
|
34
37
|
export type EnumeratedTypeParser<$> = <const values extends readonly unknown[]>(...values: values) => Type<values[number], $>;
|
|
35
|
-
export type DefinitionParser<$> = <def>(def: type.validate<def, $>) => def;
|
|
38
|
+
export type DefinitionParser<$> = <const def>(def: type.validate<def, $>) => def;
|
|
39
|
+
export type SchemaParser<$> = (schema: RootSchema, opts?: BaseParseOptions) => Type<unknown, $>;
|
|
36
40
|
export type Type<t = unknown, $ = {}> = instantiateType<t, $>;
|
|
37
|
-
export declare namespace Type {
|
|
38
|
-
type Any<t = any> = BaseType<t, any>;
|
|
39
|
-
}
|
|
40
41
|
export type TypeConstructor<t = unknown, $ = {}> = new (def: unknown, $: Scope<$>) => Type<t, $>;
|
|
41
42
|
export declare const Type: TypeConstructor;
|
package/out/type.js
CHANGED
|
@@ -1,15 +1,17 @@
|
|
|
1
1
|
import { ArkErrors, BaseRoot, GenericRoot } from "@ark/schema";
|
|
2
|
-
import { Callable } from "@ark/util";
|
|
3
|
-
import { parseGenericParams } from "./generic.js";
|
|
2
|
+
import { Callable, Hkt } from "@ark/util";
|
|
4
3
|
export class InternalTypeParser extends Callable {
|
|
5
4
|
constructor($) {
|
|
6
5
|
const attach = Object.assign({
|
|
7
6
|
errors: ArkErrors,
|
|
7
|
+
hkt: Hkt,
|
|
8
8
|
$: $,
|
|
9
|
-
raw: $.
|
|
9
|
+
raw: $.parse,
|
|
10
10
|
module: $.constructor.module,
|
|
11
11
|
scope: $.constructor.scope,
|
|
12
|
+
define: $.define,
|
|
12
13
|
generic: $.generic,
|
|
14
|
+
schema: $.schema,
|
|
13
15
|
// this won't be defined during bootstrapping, but externally always will be
|
|
14
16
|
ark: $.ambient,
|
|
15
17
|
unit: $.unit,
|
|
@@ -20,7 +22,7 @@ export class InternalTypeParser extends Callable {
|
|
|
20
22
|
super((...args) => {
|
|
21
23
|
if (args.length === 1) {
|
|
22
24
|
// treat as a simple definition
|
|
23
|
-
return $.
|
|
25
|
+
return $.parse(args[0]);
|
|
24
26
|
}
|
|
25
27
|
if (args.length === 2 &&
|
|
26
28
|
typeof args[0] === "string" &&
|
|
@@ -28,16 +30,14 @@ export class InternalTypeParser extends Callable {
|
|
|
28
30
|
args[0].at(-1) === ">") {
|
|
29
31
|
// if there are exactly two args, the first of which looks like <${string}>,
|
|
30
32
|
// treat as a generic
|
|
31
|
-
const
|
|
32
|
-
|
|
33
|
-
args: {}
|
|
34
|
-
});
|
|
33
|
+
const paramString = args[0].slice(1, -1);
|
|
34
|
+
const params = $.parseGenericParams(paramString, {});
|
|
35
35
|
return new GenericRoot(params, args[1], $, $);
|
|
36
36
|
}
|
|
37
37
|
// otherwise, treat as a tuple expression. technically, this also allows
|
|
38
38
|
// non-expression tuple definitions to be parsed, but it's not a supported
|
|
39
39
|
// part of the API as specified by the associated types
|
|
40
|
-
return $.
|
|
40
|
+
return $.parse(args);
|
|
41
41
|
}, {
|
|
42
42
|
bind: $,
|
|
43
43
|
attach
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "arktype",
|
|
3
3
|
"description": "TypeScript's 1:1 validator, optimized from editor to runtime",
|
|
4
|
-
"version": "2.0.0-rc.
|
|
4
|
+
"version": "2.0.0-rc.8",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": {
|
|
7
7
|
"name": "David Blass",
|
|
@@ -34,8 +34,8 @@
|
|
|
34
34
|
"out"
|
|
35
35
|
],
|
|
36
36
|
"dependencies": {
|
|
37
|
-
"@ark/
|
|
38
|
-
"@ark/
|
|
37
|
+
"@ark/schema": "0.10.0",
|
|
38
|
+
"@ark/util": "0.10.0"
|
|
39
39
|
},
|
|
40
40
|
"publishConfig": {
|
|
41
41
|
"access": "public"
|