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