arktype 2.0.0-rc.0 → 2.0.0-rc.10

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.
Files changed (147) hide show
  1. package/out/config.d.ts +1 -1
  2. package/out/generic.d.ts +55 -53
  3. package/out/generic.js +9 -11
  4. package/out/index.d.ts +5 -5
  5. package/out/index.js +3 -2
  6. package/out/keywords/{builtin.d.ts → builtins.d.ts} +9 -5
  7. package/out/keywords/{builtin.js → builtins.js} +2 -2
  8. package/out/keywords/constructors/Array.d.ts +12 -8
  9. package/out/keywords/constructors/Array.js +7 -7
  10. package/out/keywords/constructors/Date.d.ts +32 -0
  11. package/out/keywords/constructors/FormData.d.ts +12 -6
  12. package/out/keywords/constructors/FormData.js +19 -6
  13. package/out/keywords/constructors/TypedArray.d.ts +18 -14
  14. package/out/keywords/constructors/TypedArray.js +2 -2
  15. package/out/keywords/constructors/constructors.d.ts +21 -32
  16. package/out/keywords/constructors/constructors.js +13 -28
  17. package/out/keywords/inference.d.ts +227 -0
  18. package/out/keywords/keywords.d.ts +61 -0
  19. package/out/keywords/keywords.js +26 -0
  20. package/out/keywords/number/epoch.d.ts +1 -2
  21. package/out/keywords/number/epoch.js +2 -2
  22. package/out/keywords/number/integer.js +2 -2
  23. package/out/keywords/number/number.d.ts +37 -9
  24. package/out/keywords/number/number.js +17 -5
  25. package/out/keywords/string/alpha.d.ts +1 -1
  26. package/out/keywords/string/alphanumeric.d.ts +1 -1
  27. package/out/keywords/string/capitalize.d.ts +11 -10
  28. package/out/keywords/string/capitalize.js +4 -4
  29. package/out/keywords/string/creditCard.d.ts +1 -1
  30. package/out/keywords/string/creditCard.js +2 -2
  31. package/out/keywords/string/date.d.ts +27 -26
  32. package/out/keywords/string/date.js +16 -16
  33. package/out/keywords/string/digits.d.ts +1 -1
  34. package/out/keywords/string/email.d.ts +1 -1
  35. package/out/keywords/string/integer.d.ts +11 -10
  36. package/out/keywords/string/integer.js +7 -7
  37. package/out/keywords/string/ip.d.ts +12 -12
  38. package/out/keywords/string/ip.js +5 -5
  39. package/out/keywords/string/json.d.ts +12 -10
  40. package/out/keywords/string/json.js +24 -16
  41. package/out/keywords/string/lower.d.ts +11 -10
  42. package/out/keywords/string/lower.js +4 -4
  43. package/out/keywords/string/normalize.d.ts +63 -47
  44. package/out/keywords/string/normalize.js +14 -14
  45. package/out/keywords/string/numeric.d.ts +11 -10
  46. package/out/keywords/string/numeric.js +7 -7
  47. package/out/keywords/string/semver.d.ts +1 -1
  48. package/out/keywords/string/string.d.ts +78 -26
  49. package/out/keywords/string/string.js +5 -5
  50. package/out/keywords/string/trim.d.ts +11 -10
  51. package/out/keywords/string/trim.js +4 -4
  52. package/out/keywords/string/upper.d.ts +11 -10
  53. package/out/keywords/string/upper.js +4 -4
  54. package/out/keywords/string/url.d.ts +11 -10
  55. package/out/keywords/string/url.js +8 -8
  56. package/out/keywords/string/uuid.d.ts +27 -23
  57. package/out/keywords/string/uuid.js +3 -3
  58. package/out/keywords/ts.d.ts +44 -22
  59. package/out/keywords/ts.js +20 -14
  60. package/out/keywords/utils.d.ts +1 -1
  61. package/out/keywords/utils.js +1 -1
  62. package/out/{subtypes → methods}/array.d.ts +6 -6
  63. package/out/methods/base.d.ts +96 -0
  64. package/out/{subtypes → methods}/date.d.ts +5 -5
  65. package/out/{subtypes → methods}/instantiate.d.ts +5 -4
  66. package/out/methods/morph.d.ts +12 -0
  67. package/out/{subtypes → methods}/number.d.ts +6 -6
  68. package/out/methods/object.d.ts +81 -0
  69. package/out/{subtypes → methods}/string.d.ts +7 -7
  70. package/out/{subtypes → methods}/validator.d.ts +2 -2
  71. package/out/module.d.ts +2 -2
  72. package/out/parser/{semantic → ast}/bounds.d.ts +6 -4
  73. package/out/parser/ast/default.d.ts +8 -0
  74. package/out/parser/ast/infer.d.ts +39 -0
  75. package/out/parser/ast/utils.d.ts +4 -0
  76. package/out/parser/ast/validate.d.ts +26 -0
  77. package/out/parser/definition.d.ts +9 -12
  78. package/out/parser/objectLiteral.d.ts +11 -25
  79. package/out/parser/objectLiteral.js +6 -24
  80. package/out/parser/{string/reduce → reduce}/dynamic.d.ts +5 -7
  81. package/out/parser/{string/reduce → reduce}/dynamic.js +5 -6
  82. package/out/parser/{string/reduce → reduce}/shared.d.ts +1 -1
  83. package/out/parser/{string/reduce → reduce}/static.d.ts +1 -1
  84. package/out/parser/{string/shift → shift}/operand/date.d.ts +3 -1
  85. package/out/parser/{string/shift → shift}/operand/enclosed.d.ts +4 -1
  86. package/out/parser/{string/shift → shift}/operand/enclosed.js +7 -3
  87. package/out/parser/{string/shift → shift}/operand/unenclosed.d.ts +7 -8
  88. package/out/parser/{string/shift → shift}/operand/unenclosed.js +7 -3
  89. package/out/parser/{string/shift → shift}/operator/bounds.d.ts +7 -6
  90. package/out/parser/{string/shift → shift}/operator/default.d.ts +3 -6
  91. package/out/parser/{string/shift → shift}/operator/default.js +1 -6
  92. package/out/parser/{string/shift → shift}/operator/operator.js +2 -2
  93. package/out/parser/{string/shift → shift}/scanner.d.ts +6 -2
  94. package/out/parser/{string/shift → shift}/scanner.js +7 -5
  95. package/out/parser/{string/string.d.ts → string.d.ts} +8 -8
  96. package/out/parser/{string/string.js → string.js} +2 -0
  97. package/out/parser/tuple.d.ts +54 -43
  98. package/out/parser/tuple.js +36 -30
  99. package/out/scope.d.ts +41 -34
  100. package/out/scope.js +48 -27
  101. package/out/type.d.ts +25 -20
  102. package/out/type.js +23 -14
  103. package/package.json +3 -3
  104. package/out/intersect.d.ts +0 -28
  105. package/out/keys.d.ts +0 -16
  106. package/out/keywords/ark.d.ts +0 -33
  107. package/out/keywords/ark.js +0 -18
  108. package/out/keywords/ast.d.ts +0 -193
  109. package/out/parser/semantic/default.d.ts +0 -8
  110. package/out/parser/semantic/default.js +0 -1
  111. package/out/parser/semantic/infer.d.ts +0 -33
  112. package/out/parser/semantic/utils.d.ts +0 -4
  113. package/out/parser/semantic/validate.d.ts +0 -29
  114. package/out/subtypes/base.d.ts +0 -84
  115. package/out/subtypes/morph.d.ts +0 -13
  116. package/out/subtypes/object.d.ts +0 -33
  117. /package/out/{intersect.js → keywords/constructors/Date.js} +0 -0
  118. /package/out/keywords/{ast.js → inference.js} +0 -0
  119. /package/out/{subtypes → methods}/array.js +0 -0
  120. /package/out/{subtypes → methods}/base.js +0 -0
  121. /package/out/{subtypes → methods}/date.js +0 -0
  122. /package/out/{subtypes → methods}/instantiate.js +0 -0
  123. /package/out/{subtypes → methods}/morph.js +0 -0
  124. /package/out/{subtypes → methods}/number.js +0 -0
  125. /package/out/{subtypes → methods}/object.js +0 -0
  126. /package/out/{subtypes → methods}/string.js +0 -0
  127. /package/out/{subtypes → methods}/validator.js +0 -0
  128. /package/out/parser/{semantic → ast}/bounds.js +0 -0
  129. /package/out/{keys.js → parser/ast/default.js} +0 -0
  130. /package/out/parser/{semantic → ast}/divisor.d.ts +0 -0
  131. /package/out/parser/{semantic → ast}/divisor.js +0 -0
  132. /package/out/parser/{semantic → ast}/infer.js +0 -0
  133. /package/out/parser/{semantic → ast}/keyof.d.ts +0 -0
  134. /package/out/parser/{semantic → ast}/keyof.js +0 -0
  135. /package/out/parser/{semantic → ast}/utils.js +0 -0
  136. /package/out/parser/{semantic → ast}/validate.js +0 -0
  137. /package/out/parser/{string/reduce → reduce}/shared.js +0 -0
  138. /package/out/parser/{string/reduce → reduce}/static.js +0 -0
  139. /package/out/parser/{string/shift → shift}/operand/date.js +0 -0
  140. /package/out/parser/{string/shift → shift}/operand/genericArgs.d.ts +0 -0
  141. /package/out/parser/{string/shift → shift}/operand/genericArgs.js +0 -0
  142. /package/out/parser/{string/shift → shift}/operand/operand.d.ts +0 -0
  143. /package/out/parser/{string/shift → shift}/operand/operand.js +0 -0
  144. /package/out/parser/{string/shift → shift}/operator/bounds.js +0 -0
  145. /package/out/parser/{string/shift → shift}/operator/divisor.d.ts +0 -0
  146. /package/out/parser/{string/shift → shift}/operator/divisor.js +0 -0
  147. /package/out/parser/{string/shift → shift}/operator/operator.d.ts +0 -0
package/out/scope.d.ts CHANGED
@@ -1,21 +1,19 @@
1
- import { BaseScope, type AliasDefEntry, type ArkScopeConfig, type BaseNode, type BaseRoot, type GenericArgResolutions, type GenericAst, type GenericParamAst, type NodeKind, type NodeParseOptions, 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 ErrorType, type Json, type anyOrNever, type array, type flattenListable, type inferred, type noSuggest, type nominal } from "@ark/util";
3
- import type { ArkAmbient } from "./config.ts";
4
- import { parseGenericParams, type GenericDeclaration, type GenericParser, type ParameterString, type baseGenericConstraints, type parseValidGenericParams } from "./generic.ts";
5
- import type { type } from "./keywords/ark.ts";
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 ErrorType, type Json, type anyOrNever, type array, type flattenListable, type noSuggest, type nominal } from "@ark/util";
3
+ import type { ArkSchemaRegistry } from "./config.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/keywords.ts";
6
6
  import type { BoundModule, Module, Submodule, instantiateExport } from "./module.ts";
7
+ import type { DefAst, InferredAst } from "./parser/ast/infer.ts";
7
8
  import { type inferDefinition, type validateDefinition } from "./parser/definition.ts";
8
- import type { ParsedDefault } from "./parser/string/shift/operator/default.ts";
9
- import { InternalTypeParser, type DeclarationParser, type DefinitionParser, type Type, type TypeParser } from "./type.ts";
10
- export type ScopeParser = <const def>(def: validateScope<def>, config?: ArkScopeConfig) => Scope<inferScope<def>>;
11
- export type ModuleParser = <const def>(def: validateScope<def>, config?: ArkScopeConfig) => inferScope<def> extends infer $ ? Module<{
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 validateScope<def> = {
15
- [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;
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 */
@@ -25,9 +23,9 @@ type PreparsedResolution = PreparsedNodeResolution;
25
23
  type bootstrapAliases<def> = {
26
24
  [k in Exclude<keyof def, GenericDeclaration>]: def[k] extends (PreparsedResolution) ? def[k] extends {
27
25
  t: infer g extends GenericAst;
28
- } ? g : def[k] extends Module<infer $> ? Submodule<$> : def[k] : def[k] extends (() => infer thunkReturn extends PreparsedResolution) ? thunkReturn extends {
26
+ } ? g : def[k] extends Module<infer $> | BoundModule<infer $, any> ? Submodule<$> : def[k] : def[k] extends (() => infer thunkReturn extends PreparsedResolution) ? thunkReturn extends {
29
27
  t: infer g extends GenericAst;
30
- } ? g : thunkReturn extends Module<infer $> ? Submodule<$> : thunkReturn : Def<def[k]>;
28
+ } ? g : thunkReturn extends Module<infer $> | BoundModule<infer $, any> ? Submodule<$> : thunkReturn : Def<def[k]>;
31
29
  } & {
32
30
  [k in keyof def & GenericDeclaration as extractGenericName<k>]: GenericAst<parseValidGenericParams<extractGenericParameters<k>, bootstrapAliases<def>>, def[k], UnparsedScope>;
33
31
  };
@@ -39,11 +37,12 @@ type inferBootstrapped<$> = {
39
37
  export type bindGenericToScope<g extends GenericAst, $> = GenericAst<g["paramsAst"], g["bodyDef"], g["$"] extends UnparsedScope ? $ : g["$"], $>;
40
38
  type extractGenericName<k> = k extends GenericDeclaration<infer name> ? name : never;
41
39
  type extractGenericParameters<k> = k extends `${string}<${infer params}>` ? ParameterString<params> : never;
42
- export type resolve<reference extends keyof $ | keyof args, $, args> = (reference extends keyof args ? args[reference] : $[reference & keyof $]) extends infer resolution ? [
40
+ export type resolutionToAst<alias extends string, resolution> = [
43
41
  resolution
44
- ] extends [anyOrNever] ? resolution : resolution extends {
45
- [inferred]: infer t;
46
- } ? t : resolution extends Def<infer def> ? inferDefinition<def, $, args> : resolution : never;
42
+ ] extends [anyOrNever] ? InferredAst<resolution, alias> : resolution extends Def<infer def> ? DefAst<def, alias> : resolution extends {
43
+ [arkKind]: "module";
44
+ root: infer root;
45
+ } ? InferredAst<root, alias> : resolution extends GenericAst ? resolution : InferredAst<resolution, alias>;
47
46
  export type moduleKeyOf<$> = {
48
47
  [k in keyof $]: $[k] extends {
49
48
  [arkKind]: "module";
@@ -51,24 +50,25 @@ export type moduleKeyOf<$> = {
51
50
  $[k]
52
51
  ] extends [anyOrNever] ? never : k & string : never;
53
52
  }[keyof $];
54
- type unwrapPreinferred<t> = t extends type.cast<infer inferred> ? inferred : t;
55
- export type tryInferSubmoduleReference<$, token> = token extends `${infer submodule extends moduleKeyOf<$>}.${infer subalias}` ? subalias extends keyof $[submodule] ? unwrapPreinferred<$[submodule][subalias]> : tryInferSubmoduleReference<$[submodule], subalias> : token extends (`${infer submodule extends moduleKeyOf<ArkAmbient.$>}.${infer subalias}`) ? subalias extends keyof ArkAmbient.$[submodule] ? unwrapPreinferred<ArkAmbient.$[submodule][subalias]> : tryInferSubmoduleReference<ArkAmbient.$[submodule], subalias> : never;
56
- export interface ParseContext extends TypeParseOptions {
57
- $: InternalScope;
58
- }
59
- export interface TypeParseOptions {
60
- args?: GenericArgResolutions;
53
+ export interface ArkTypeRegistry extends ArkSchemaRegistry {
54
+ typeAttachments?: Ark.boundTypeAttachments<any>;
61
55
  }
56
+ export declare const $arkTypeRegistry: ArkTypeRegistry;
62
57
  export interface InternalScope {
63
58
  constructor: typeof InternalScope;
64
59
  }
65
60
  export declare class InternalScope<$ extends {} = {}> extends BaseScope<$> {
66
61
  private parseCache;
67
- preparseAlias(k: string, v: unknown): AliasDefEntry;
68
- preparseRoot(def: unknown): unknown;
69
- parse<defaultable extends boolean = false>(def: unknown, ctx: ParseContext, defaultable?: defaultable): BaseRoot | (defaultable extends false ? never : ParsedDefault);
70
- parseString<defaultable extends boolean>(def: string, ctx: ParseContext, defaultable: defaultable): BaseRoot | (defaultable extends false ? never : ParsedDefault);
71
- parseRoot: (def: unknown, opts?: TypeParseOptions) => BaseRoot;
62
+ protected cacheGetter<name extends keyof this>(name: name, value: this[name]): this[name];
63
+ get ambientAttachments(): Ark.boundTypeAttachments<$> | undefined;
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;
70
+ unit: UnitTypeParser<$>;
71
+ enumerated: EnumeratedTypeParser<$>;
72
72
  type: InternalTypeParser;
73
73
  declare: () => {
74
74
  type: InternalTypeParser;
@@ -78,6 +78,12 @@ export declare class InternalScope<$ extends {} = {}> extends BaseScope<$> {
78
78
  static module: ModuleParser;
79
79
  }
80
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
+ }
81
87
  export declare const module: ModuleParser;
82
88
  export interface Scope<$ = {}> {
83
89
  t: $;
@@ -91,12 +97,14 @@ export interface Scope<$ = {}> {
91
97
  aliases: Record<string, unknown>;
92
98
  internal: toInternalScope<$>;
93
99
  defineSchema<const def extends RootSchema>(schema: def): def;
94
- schema(schema: RootSchema, opts?: NodeParseOptions): BaseRoot;
95
- 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>>>;
101
+ unit: UnitTypeParser<$>;
102
+ enumerated: EnumeratedTypeParser<$>;
96
103
  type: TypeParser<$>;
97
104
  declare: DeclarationParser<$>;
98
105
  define: DefinitionParser<$>;
99
106
  generic: GenericParser<$>;
107
+ schema: SchemaParser<$>;
100
108
  import(): Module<{
101
109
  [k in exportedNameOf<$> as PrivateDeclaration<k>]: $[k];
102
110
  }>;
@@ -117,7 +125,6 @@ export interface ScopeConstructor {
117
125
  module: ModuleParser;
118
126
  }
119
127
  export declare const Scope: ScopeConstructor;
120
- export declare const writeShallowCycleErrorMessage: (name: string, seen: string[]) => string;
121
128
  export type parseScopeKey<k, def> = k extends `${infer name}<${infer params}>` ? parseGenericScopeKey<name, params, def> : {
122
129
  name: k;
123
130
  params: [];
package/out/scope.js CHANGED
@@ -1,15 +1,28 @@
1
- import { BaseScope, hasArkKind, parseGeneric } from "@ark/schema";
2
- import { domainOf, hasDomain, isThunk, throwParseError } from "@ark/util";
3
- import { parseGenericParams } from "./generic.js";
1
+ import { $ark, BaseScope, hasArkKind, parseGeneric } from "@ark/schema";
2
+ import { domainOf, flatMorph, hasDomain, isThunk, throwParseError } from "@ark/util";
3
+ import { parseGenericParamName } from "./generic.js";
4
4
  import { parseObject, writeBadDefinitionTypeMessage } from "./parser/definition.js";
5
- import { DynamicState } from "./parser/string/reduce/dynamic.js";
6
- import { writeUnexpectedCharacterMessage } from "./parser/string/shift/operator/operator.js";
7
- import { Scanner } from "./parser/string/shift/scanner.js";
8
- import { fullStringParse } from "./parser/string/string.js";
5
+ import { DynamicState } from "./parser/reduce/dynamic.js";
6
+ import { writeUnexpectedCharacterMessage } from "./parser/shift/operator/operator.js";
7
+ import { Scanner } from "./parser/shift/scanner.js";
8
+ import { fullStringParse } from "./parser/string.js";
9
9
  import { InternalTypeParser } from "./type.js";
10
+ export const $arkTypeRegistry = $ark;
10
11
  export class InternalScope extends BaseScope {
11
12
  parseCache = {};
12
- preparseAlias(k, v) {
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) {
13
26
  const firstParamIndex = k.indexOf("<");
14
27
  if (firstParamIndex === -1)
15
28
  return [k, v];
@@ -23,35 +36,46 @@ export class InternalScope extends BaseScope {
23
36
  // use a thunk definition for the generic so that we can parse
24
37
  // constraints within the current scope
25
38
  () => {
26
- const params = parseGenericParams(paramString, {
27
- $: this,
28
- args: {}
29
- });
39
+ const params = this.parseGenericParams(paramString, { alias: name });
30
40
  const generic = parseGeneric(params, v, this);
31
41
  return generic;
32
42
  }
33
43
  ];
34
44
  }
35
- preparseRoot(def) {
36
- if (isThunk(def) && !hasArkKind(def, "generic"))
37
- return def();
38
- return def;
45
+ parseGenericParams(def, opts) {
46
+ return parseGenericParamName(new Scanner(def), [], this.createParseContext({ ...opts, def, prefix: "generic" }));
47
+ }
48
+ normalizeRootScopeValue(resolution) {
49
+ if (isThunk(resolution) && !hasArkKind(resolution, "generic"))
50
+ return resolution();
51
+ return resolution;
39
52
  }
40
- parse(def, ctx, defaultable = false) {
53
+ preparseOwnDefinitionFormat(def, opts) {
54
+ return {
55
+ ...opts,
56
+ def,
57
+ prefix: opts.alias ?? "type"
58
+ };
59
+ }
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 };
41
66
  if (typeof def === "string") {
42
67
  if (ctx.args && Object.keys(ctx.args).some(k => def.includes(k))) {
43
68
  // we can only rely on the cache if there are no contextual
44
69
  // resolutions like "this" or generic args
45
- return this.parseString(def, ctx, defaultable);
70
+ return this.parseString(def, ctx);
46
71
  }
47
- const contextKey = `${def}${defaultable}`;
48
- return (this.parseCache[contextKey] ??= this.parseString(def, ctx, defaultable));
72
+ return (this.parseCache[def] ??= this.parseString(def, ctx));
49
73
  }
50
74
  return hasDomain(def, "object") ?
51
75
  parseObject(def, ctx)
52
76
  : throwParseError(writeBadDefinitionTypeMessage(domainOf(def)));
53
77
  }
54
- parseString(def, ctx, defaultable) {
78
+ parseString(def, ctx) {
55
79
  const aliasResolution = this.maybeResolveRoot(def);
56
80
  if (aliasResolution)
57
81
  return aliasResolution;
@@ -60,16 +84,14 @@ export class InternalScope extends BaseScope {
60
84
  : undefined;
61
85
  if (aliasArrayResolution)
62
86
  return aliasArrayResolution;
63
- const s = new DynamicState(new Scanner(def), ctx, defaultable);
87
+ const s = new DynamicState(new Scanner(def), ctx);
64
88
  const node = fullStringParse(s);
65
89
  if (s.finalizer === ">")
66
90
  throwParseError(writeUnexpectedCharacterMessage(">"));
67
91
  return node;
68
92
  }
69
- parseRoot = (def, opts = {}) => {
70
- const node = this.parse(def, Object.assign(this.finalizeRootArgs(opts, () => node), { $: this })).bindScope(this);
71
- return node;
72
- };
93
+ unit = value => this.units([value]);
94
+ enumerated = (...values) => this.units(values);
73
95
  type = new InternalTypeParser(this);
74
96
  declare = () => ({
75
97
  type: this.type
@@ -81,4 +103,3 @@ export class InternalScope extends BaseScope {
81
103
  export const scope = InternalScope.scope;
82
104
  export const module = InternalScope.module;
83
105
  export const Scope = InternalScope;
84
- export const writeShallowCycleErrorMessage = (name, seen) => `Alias '${name}' has a shallow resolution cycle: ${[...seen, name].join(":")}`;
package/out/type.d.ts CHANGED
@@ -1,22 +1,31 @@
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 { type Generic, type ParameterString, type baseGenericConstraints, type parseValidGenericParams, type validateParameterString } from "./generic.ts";
4
- import type { distillIn, distillOut } from "./keywords/ast.ts";
5
- import type { inferDefinition, validateDeclared, validateDefinition } from "./parser/definition.ts";
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
+ import type { distill } from "./keywords/inference.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, validateDefinition } from "./parser/definition.ts";
6
9
  import type { IndexOneOperator, IndexZeroOperator, TupleInfixOperator } from "./parser/tuple.ts";
7
10
  import type { InternalScope, ModuleParser, Scope, ScopeParser, bindThis } from "./scope.ts";
8
- import type { BaseType } from "./subtypes/base.ts";
9
- import type { instantiateType } from "./subtypes/instantiate.ts";
10
11
  /** The convenience properties attached to `type` */
11
12
  export type TypeParserAttachments = Omit<TypeParser, never>;
12
- export interface TypeParser<$ = {}> {
13
- <const def, r = Type<inferTypeRoot<def, $>, $>>(def: validateTypeRoot<def, $>): r;
14
- <params extends ParameterString, const def>(params: validateParameterString<params, $>, def: validateDefinition<def, $, baseGenericConstraints<parseValidGenericParams<params, $>>>): Generic<parseValidGenericParams<params, $>, def, $>;
15
- <const zero, const one, const rest extends array, r = Type<inferTypeRoot<[zero, one, ...rest], $>, $>>(_0: zero extends IndexZeroOperator ? zero : validateTypeRoot<zero, $>, _1: zero extends "keyof" ? validateTypeRoot<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<distillIn<inferTypeRoot<zero, $>>>] : one extends "=>" ? [Morph<distillOut<inferTypeRoot<zero, $>>, unknown>] : one extends "@" ? [MetaSchema] : [validateTypeRoot<rest[0], $>] : []): r;
16
- raw(def: unknown): BaseType<any, $>;
13
+ export interface TypeParser<$ = {}> extends Ark.boundTypeAttachments<$> {
14
+ <const def, r = Type<type.infer<def, $>, $>>(def: type.validate<def, $>): r;
15
+ <const params extends ParameterString, const def>(params: validateParameterString<params, $>, def: validateDefinition<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] : one extends "=" ? [distill.In<type.infer<NoInfer<zero>, $>>] : [type.validate<rest[0], $>] : []): r;
17
17
  errors: typeof ArkErrors;
18
+ hkt: typeof Hkt;
19
+ keywords: typeof keywords;
20
+ $: Scope<$>;
21
+ raw(def: unknown): BaseType<any, $>;
18
22
  module: ModuleParser;
19
23
  scope: ScopeParser;
24
+ define: DefinitionParser<$>;
25
+ generic: GenericParser<$>;
26
+ schema: SchemaParser<$>;
27
+ unit: UnitTypeParser<$>;
28
+ enumerated: EnumeratedTypeParser<$>;
20
29
  }
21
30
  export declare class InternalTypeParser extends Callable<(...args: unknown[]) => BaseRoot | Generic, TypeParserAttachments> {
22
31
  constructor($: InternalScope);
@@ -24,14 +33,10 @@ export declare class InternalTypeParser extends Callable<(...args: unknown[]) =>
24
33
  export type DeclarationParser<$> = <preinferred>() => {
25
34
  type: <const def>(def: validateDeclared<preinferred, def, $, bindThis<def>>) => Type<preinferred, $>;
26
35
  };
27
- export type DefinitionParser<$> = <def>(def: validateTypeRoot<def, $>) => def;
28
- export type validateTypeRoot<def, $ = {}> = validateDefinition<def, $, bindThis<def>>;
29
- export type inferTypeRoot<def, $> = inferDefinition<def, $, bindThis<def>>;
30
- export type validateAmbient<def> = validateTypeRoot<def, {}>;
31
- export type inferAmbient<def> = inferTypeRoot<def, {}>;
36
+ export type UnitTypeParser<$> = <const t>(value: t) => Type<t, $>;
37
+ export type EnumeratedTypeParser<$> = <const values extends readonly unknown[]>(...values: values) => Type<values[number], $>;
38
+ export type DefinitionParser<$> = <const def>(def: type.validate<def, $>) => def;
39
+ export type SchemaParser<$> = (schema: RootSchema, opts?: BaseParseOptions) => Type<unknown, $>;
32
40
  export type Type<t = unknown, $ = {}> = instantiateType<t, $>;
33
- export declare namespace Type {
34
- type Any<t = any> = BaseType<t, any>;
35
- }
36
41
  export type TypeConstructor<t = unknown, $ = {}> = new (def: unknown, $: Scope<$>) => Type<t, $>;
37
42
  export declare const Type: TypeConstructor;
package/out/type.js CHANGED
@@ -1,12 +1,28 @@
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($) {
5
+ const attach = Object.assign({
6
+ errors: ArkErrors,
7
+ hkt: Hkt,
8
+ $: $,
9
+ raw: $.parse,
10
+ module: $.constructor.module,
11
+ scope: $.constructor.scope,
12
+ define: $.define,
13
+ generic: $.generic,
14
+ schema: $.schema,
15
+ // this won't be defined during bootstrapping, but externally always will be
16
+ keywords: $.ambient,
17
+ unit: $.unit,
18
+ enumerated: $.enumerated
19
+ },
20
+ // also won't be defined during bootstrapping
21
+ $.ambientAttachments);
6
22
  super((...args) => {
7
23
  if (args.length === 1) {
8
24
  // treat as a simple definition
9
- return $.parseRoot(args[0]);
25
+ return $.parse(args[0]);
10
26
  }
11
27
  if (args.length === 2 &&
12
28
  typeof args[0] === "string" &&
@@ -14,24 +30,17 @@ export class InternalTypeParser extends Callable {
14
30
  args[0].at(-1) === ">") {
15
31
  // if there are exactly two args, the first of which looks like <${string}>,
16
32
  // treat as a generic
17
- const params = parseGenericParams(args[0].slice(1, -1), {
18
- $,
19
- args: {}
20
- });
33
+ const paramString = args[0].slice(1, -1);
34
+ const params = $.parseGenericParams(paramString, {});
21
35
  return new GenericRoot(params, args[1], $, $);
22
36
  }
23
37
  // otherwise, treat as a tuple expression. technically, this also allows
24
38
  // non-expression tuple definitions to be parsed, but it's not a supported
25
39
  // part of the API as specified by the associated types
26
- return $.parseRoot(args);
40
+ return $.parse(args);
27
41
  }, {
28
42
  bind: $,
29
- attach: {
30
- errors: ArkErrors,
31
- raw: $.parseRoot,
32
- module: $.constructor.module,
33
- scope: $.constructor.scope
34
- }
43
+ attach
35
44
  });
36
45
  }
37
46
  }
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.0",
4
+ "version": "2.0.0-rc.10",
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/util": "0.3.0",
38
- "@ark/schema": "0.4.0"
37
+ "@ark/util": "0.12.0",
38
+ "@ark/schema": "0.12.0"
39
39
  },
40
40
  "publishConfig": {
41
41
  "access": "public"
@@ -1,28 +0,0 @@
1
- import type { anyOrNever, array, Hkt, intersectArrays, show } from "@ark/util";
2
- import type { constrain, Constraints, MorphAst, parseConstraints, To } from "./keywords/ast.ts";
3
- export type inferIntersection<l, r> = _inferIntersection<l, r, false>;
4
- export type inferPipe<l, r> = _inferIntersection<l, r, true>;
5
- type _inferIntersection<l, r, piped extends boolean> = [
6
- l & r
7
- ] extends [infer t extends anyOrNever] ? t : l extends MorphAst<infer lIn, infer lOut> ? r extends MorphAst<any, infer rOut> ? piped extends true ? (In: lIn) => rOut : never : piped extends true ? (In: lIn) => To<r> : (In: _inferIntersection<lIn, r, false>) => lOut : r extends MorphAst<infer rIn, infer rOut> ? (In: _inferIntersection<rIn, l, false>) => rOut : parseConstraints<l> extends ([
8
- infer lBase,
9
- infer lConstraints extends Constraints
10
- ]) ? parseConstraints<r> extends ([
11
- infer rBase,
12
- infer rConstraints extends Constraints
13
- ]) ? constrain<_inferIntersection<lBase, rBase, piped>, lConstraints & rConstraints> : constrain<_inferIntersection<lBase, r, piped>, lConstraints> : parseConstraints<r> extends ([
14
- infer rBase,
15
- infer rConstraints extends Constraints
16
- ]) ? constrain<_inferIntersection<l, rBase, piped>, rConstraints> : [l, r] extends [object, object] ? intersectObjects<l, r, piped> extends infer result ? result : never : l & r;
17
- interface MorphableIntersection<piped extends boolean> extends Hkt<[unknown, unknown]> {
18
- body: _inferIntersection<this[0], this[1], piped>;
19
- }
20
- type intersectObjects<l, r, piped extends boolean> = [
21
- l,
22
- r
23
- ] extends [infer lList extends array, infer rList extends array] ? intersectArrays<lList, rList, MorphableIntersection<piped>> : show<{
24
- [k in keyof l]: k extends keyof r ? _inferIntersection<l[k], r[k], piped> : l[k];
25
- } & {
26
- [k in keyof r]: k extends keyof l ? _inferIntersection<l[k], r[k], piped> : r[k];
27
- }>;
28
- export {};
package/out/keys.d.ts DELETED
@@ -1,16 +0,0 @@
1
- import type { array, join, Key, NonNegativeIntegerLiteral, typeToString } from "@ark/util";
2
- import type { type } from "./keywords/ark.ts";
3
- import type { termOrType } from "./keywords/ast.ts";
4
- export type toArkKey<o, k extends keyof o> = k extends number ? [
5
- o,
6
- number
7
- ] extends [array, k] ? NonNegativeIntegerLiteral : `${k}` : k;
8
- export type arkKeyOf<o> = o extends array ? (number extends o["length"] ? NonNegativeIntegerLiteral : never) | {
9
- [k in keyof o]-?: k extends `${infer index extends number}` ? index | k : never;
10
- }[keyof o & `${number}`] : {
11
- [k in keyof o]: k extends number ? k | `${k}` : k;
12
- }[keyof o];
13
- export type getArkKey<o, k extends arkKeyOf<o>> = o[Extract<k extends NonNegativeIntegerLiteral ? number : k, keyof o>];
14
- export type TypeKey = termOrType<Key>;
15
- export type typeKeyToString<k extends TypeKey> = typeToString<k extends type.cast<infer t> ? t : k>;
16
- export type writeInvalidKeysMessage<o extends string, keys extends array<string>> = `Key${keys["length"] extends 1 ? "" : "s"} ${join<keys, ", ">} ${keys["length"] extends 1 ? "does" : "do"} not exist on ${o}`;
@@ -1,33 +0,0 @@
1
- import type { ArkErrors } from "@ark/schema";
2
- import type { inferred } from "@ark/util";
3
- import type { GenericParser } from "../generic.ts";
4
- import type { Module } from "../module.ts";
5
- import { type Scope } from "../scope.ts";
6
- import type { DeclarationParser, DefinitionParser, TypeParser } from "../type.ts";
7
- import { arkBuiltin } from "./builtin.ts";
8
- import { arkPrototypes } from "./constructors/constructors.ts";
9
- import { number } from "./number/number.ts";
10
- import { string } from "./string/string.ts";
11
- import { arkTs } from "./ts.ts";
12
- export interface Ark extends Omit<Ark.keywords, keyof Ark.Wrapped>, Ark.Wrapped {
13
- }
14
- export declare namespace Ark {
15
- interface keywords extends arkTs, arkPrototypes, arkBuiltin {
16
- }
17
- interface Wrapped {
18
- string: string.submodule;
19
- number: number.submodule;
20
- }
21
- }
22
- export declare const ambient: Scope<Ark>;
23
- export declare const ark: Module<Ark>;
24
- export declare const type: TypeParser<{}>;
25
- export declare namespace type {
26
- type cast<t> = {
27
- [inferred]?: t;
28
- };
29
- type errors = ArkErrors;
30
- }
31
- export declare const generic: GenericParser<{}>;
32
- export declare const define: DefinitionParser<{}>;
33
- export declare const declare: DeclarationParser<{}>;
@@ -1,18 +0,0 @@
1
- import { scope } from "../scope.js";
2
- import { arkBuiltin } from "./builtin.js";
3
- import { arkPrototypes } from "./constructors/constructors.js";
4
- import { number } from "./number/number.js";
5
- import { string } from "./string/string.js";
6
- import { arkTs } from "./ts.js";
7
- export const ambient = scope({
8
- ...arkTs,
9
- ...arkPrototypes,
10
- ...arkBuiltin,
11
- string,
12
- number
13
- }, { prereducedAliases: true, ambient: true });
14
- export const ark = ambient.export();
15
- export const type = ambient.type;
16
- export const generic = ambient.generic;
17
- export const define = ambient.define;
18
- export const declare = ambient.declare;