arktype 2.0.0-dev.2 → 2.0.0-dev.20

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 (223) hide show
  1. package/LICENSE +7 -0
  2. package/out/api.d.ts +6 -0
  3. package/out/api.js +5 -0
  4. package/out/ark.d.ts +15 -39
  5. package/out/ark.js +9 -18
  6. package/out/config.d.ts +1 -0
  7. package/out/config.js +2 -0
  8. package/out/generic.d.ts +21 -0
  9. package/out/generic.js +24 -0
  10. package/out/match.d.ts +18 -24
  11. package/out/match.js +50 -60
  12. package/out/module.d.ts +11 -0
  13. package/out/module.js +2 -0
  14. package/out/parser/definition.d.ts +13 -15
  15. package/out/parser/definition.js +10 -13
  16. package/out/parser/generic.d.ts +5 -7
  17. package/out/parser/generic.js +7 -11
  18. package/out/parser/objectLiteral.d.ts +57 -18
  19. package/out/parser/objectLiteral.js +93 -52
  20. package/out/parser/semantic/bounds.d.ts +10 -8
  21. package/out/parser/semantic/bounds.js +0 -1
  22. package/out/parser/semantic/divisor.d.ts +5 -7
  23. package/out/parser/semantic/divisor.js +0 -1
  24. package/out/parser/semantic/infer.d.ts +24 -0
  25. package/out/parser/semantic/infer.js +1 -0
  26. package/out/parser/semantic/utils.d.ts +3 -7
  27. package/out/parser/semantic/utils.js +0 -1
  28. package/out/parser/semantic/validate.d.ts +16 -13
  29. package/out/parser/semantic/validate.js +1 -1
  30. package/out/parser/string/reduce/dynamic.d.ts +13 -13
  31. package/out/parser/string/reduce/dynamic.js +31 -26
  32. package/out/parser/string/reduce/shared.d.ts +14 -28
  33. package/out/parser/string/reduce/shared.js +4 -3
  34. package/out/parser/string/reduce/static.d.ts +26 -23
  35. package/out/parser/string/reduce/static.js +0 -1
  36. package/out/parser/string/shift/operand/date.d.ts +2 -2
  37. package/out/parser/string/shift/operand/date.js +4 -7
  38. package/out/parser/string/shift/operand/enclosed.d.ts +3 -4
  39. package/out/parser/string/shift/operand/enclosed.js +14 -14
  40. package/out/parser/string/shift/operand/genericArgs.d.ts +9 -10
  41. package/out/parser/string/shift/operand/genericArgs.js +6 -10
  42. package/out/parser/string/shift/operand/operand.d.ts +3 -4
  43. package/out/parser/string/shift/operand/operand.js +9 -13
  44. package/out/parser/string/shift/operand/unenclosed.d.ts +15 -20
  45. package/out/parser/string/shift/operand/unenclosed.js +24 -36
  46. package/out/parser/string/shift/operator/bounds.d.ts +8 -12
  47. package/out/parser/string/shift/operator/bounds.js +45 -56
  48. package/out/parser/string/shift/operator/divisor.d.ts +1 -3
  49. package/out/parser/string/shift/operator/divisor.js +1 -3
  50. package/out/parser/string/shift/operator/operator.d.ts +1 -2
  51. package/out/parser/string/shift/operator/operator.js +12 -19
  52. package/out/parser/string/shift/scanner.d.ts +8 -7
  53. package/out/parser/string/shift/scanner.js +17 -16
  54. package/out/parser/string/string.d.ts +9 -7
  55. package/out/parser/string/string.js +1 -3
  56. package/out/parser/tuple.d.ts +101 -42
  57. package/out/parser/tuple.js +107 -86
  58. package/out/scope.d.ts +44 -115
  59. package/out/scope.js +39 -188
  60. package/out/type.d.ts +63 -61
  61. package/out/type.js +30 -135
  62. package/package.json +33 -39
  63. package/__tests__/array.test.ts +0 -310
  64. package/__tests__/astUtils.test.ts +0 -17
  65. package/__tests__/badDefinitionType.test.ts +0 -51
  66. package/__tests__/basis.test.ts +0 -46
  67. package/__tests__/bounds.test.ts +0 -388
  68. package/__tests__/cast.test.ts +0 -32
  69. package/__tests__/completions.test.ts +0 -46
  70. package/__tests__/config.test.ts +0 -77
  71. package/__tests__/cyclic.test.ts +0 -144
  72. package/__tests__/dateLiteral.test.ts +0 -32
  73. package/__tests__/declared.test.ts +0 -124
  74. package/__tests__/discrimination.test.ts +0 -74
  75. package/__tests__/divisor.test.ts +0 -90
  76. package/__tests__/enclosed.test.ts +0 -93
  77. package/__tests__/examples.test.ts +0 -56
  78. package/__tests__/expressions.test.ts +0 -138
  79. package/__tests__/generateBenchData.ts +0 -89
  80. package/__tests__/generated/cyclic.ts +0 -3058
  81. package/__tests__/generic.test.ts +0 -294
  82. package/__tests__/group.test.ts +0 -72
  83. package/__tests__/hkt.test.ts +0 -22
  84. package/__tests__/imports.test.ts +0 -113
  85. package/__tests__/instanceof.test.ts +0 -102
  86. package/__tests__/intersection.test.ts +0 -104
  87. package/__tests__/jsdoc.test.ts +0 -64
  88. package/__tests__/keyTraversal.test.ts +0 -74
  89. package/__tests__/keyof.test.ts +0 -134
  90. package/__tests__/keywords.test.ts +0 -188
  91. package/__tests__/literal.test.ts +0 -51
  92. package/__tests__/match.bench.ts +0 -31
  93. package/__tests__/match.test.ts +0 -224
  94. package/__tests__/methods.bench.ts +0 -22
  95. package/__tests__/morph.test.ts +0 -409
  96. package/__tests__/narrow.test.ts +0 -102
  97. package/__tests__/nodeDefinitions.test.ts +0 -190
  98. package/__tests__/object.bench.ts +0 -28
  99. package/__tests__/objectLiteral.test.ts +0 -301
  100. package/__tests__/operand.bench.ts +0 -45
  101. package/__tests__/operator.bench.ts +0 -130
  102. package/__tests__/regex.test.ts +0 -58
  103. package/__tests__/scope.bench.ts +0 -39
  104. package/__tests__/scope.test.ts +0 -140
  105. package/__tests__/string.test.ts +0 -48
  106. package/__tests__/submodule.test.ts +0 -99
  107. package/__tests__/this.test.ts +0 -57
  108. package/__tests__/thunk.test.ts +0 -80
  109. package/__tests__/traverse.test.ts +0 -119
  110. package/__tests__/tsGenerics.test.ts +0 -12
  111. package/__tests__/typeMethods.test.ts +0 -69
  112. package/__tests__/typeReference.test.ts +0 -39
  113. package/__tests__/unenclosed.test.ts +0 -120
  114. package/__tests__/union.test.ts +0 -147
  115. package/ark.ts +0 -91
  116. package/main.ts +0 -18
  117. package/match.ts +0 -243
  118. package/out/ark.d.ts.map +0 -1
  119. package/out/ark.js.map +0 -1
  120. package/out/arkConfig.d.ts +0 -2
  121. package/out/arkConfig.d.ts.map +0 -1
  122. package/out/arkConfig.js +0 -10
  123. package/out/arkConfig.js.map +0 -1
  124. package/out/main.d.ts +0 -7
  125. package/out/main.d.ts.map +0 -1
  126. package/out/main.js +0 -4
  127. package/out/main.js.map +0 -1
  128. package/out/match.d.ts.map +0 -1
  129. package/out/match.js.map +0 -1
  130. package/out/parser/definition.d.ts.map +0 -1
  131. package/out/parser/definition.js.map +0 -1
  132. package/out/parser/generic.d.ts.map +0 -1
  133. package/out/parser/generic.js.map +0 -1
  134. package/out/parser/objectLiteral.d.ts.map +0 -1
  135. package/out/parser/objectLiteral.js.map +0 -1
  136. package/out/parser/semantic/bounds.d.ts.map +0 -1
  137. package/out/parser/semantic/bounds.js.map +0 -1
  138. package/out/parser/semantic/divisor.d.ts.map +0 -1
  139. package/out/parser/semantic/divisor.js.map +0 -1
  140. package/out/parser/semantic/intersections.d.ts +0 -14
  141. package/out/parser/semantic/intersections.d.ts.map +0 -1
  142. package/out/parser/semantic/intersections.js +0 -2
  143. package/out/parser/semantic/intersections.js.map +0 -1
  144. package/out/parser/semantic/semantic.d.ts +0 -33
  145. package/out/parser/semantic/semantic.d.ts.map +0 -1
  146. package/out/parser/semantic/semantic.js +0 -2
  147. package/out/parser/semantic/semantic.js.map +0 -1
  148. package/out/parser/semantic/utils.d.ts.map +0 -1
  149. package/out/parser/semantic/utils.js.map +0 -1
  150. package/out/parser/semantic/validate.d.ts.map +0 -1
  151. package/out/parser/semantic/validate.js.map +0 -1
  152. package/out/parser/shared.d.ts +0 -56
  153. package/out/parser/shared.d.ts.map +0 -1
  154. package/out/parser/shared.js +0 -55
  155. package/out/parser/shared.js.map +0 -1
  156. package/out/parser/string/reduce/dynamic.d.ts.map +0 -1
  157. package/out/parser/string/reduce/dynamic.js.map +0 -1
  158. package/out/parser/string/reduce/shared.d.ts.map +0 -1
  159. package/out/parser/string/reduce/shared.js.map +0 -1
  160. package/out/parser/string/reduce/static.d.ts.map +0 -1
  161. package/out/parser/string/reduce/static.js.map +0 -1
  162. package/out/parser/string/shift/operand/date.d.ts.map +0 -1
  163. package/out/parser/string/shift/operand/date.js.map +0 -1
  164. package/out/parser/string/shift/operand/enclosed.d.ts.map +0 -1
  165. package/out/parser/string/shift/operand/enclosed.js.map +0 -1
  166. package/out/parser/string/shift/operand/genericArgs.d.ts.map +0 -1
  167. package/out/parser/string/shift/operand/genericArgs.js.map +0 -1
  168. package/out/parser/string/shift/operand/operand.d.ts.map +0 -1
  169. package/out/parser/string/shift/operand/operand.js.map +0 -1
  170. package/out/parser/string/shift/operand/unenclosed.d.ts.map +0 -1
  171. package/out/parser/string/shift/operand/unenclosed.js.map +0 -1
  172. package/out/parser/string/shift/operator/bounds.d.ts.map +0 -1
  173. package/out/parser/string/shift/operator/bounds.js.map +0 -1
  174. package/out/parser/string/shift/operator/divisor.d.ts.map +0 -1
  175. package/out/parser/string/shift/operator/divisor.js.map +0 -1
  176. package/out/parser/string/shift/operator/operator.d.ts.map +0 -1
  177. package/out/parser/string/shift/operator/operator.js.map +0 -1
  178. package/out/parser/string/shift/scanner.d.ts.map +0 -1
  179. package/out/parser/string/shift/scanner.js.map +0 -1
  180. package/out/parser/string/string.d.ts.map +0 -1
  181. package/out/parser/string/string.js.map +0 -1
  182. package/out/parser/tuple.d.ts.map +0 -1
  183. package/out/parser/tuple.js.map +0 -1
  184. package/out/scope.d.ts.map +0 -1
  185. package/out/scope.js.map +0 -1
  186. package/out/test.d.ts +0 -2
  187. package/out/test.d.ts.map +0 -1
  188. package/out/test.js +0 -7
  189. package/out/test.js.map +0 -1
  190. package/out/tsconfig.build.tsbuildinfo +0 -1
  191. package/out/type.d.ts.map +0 -1
  192. package/out/type.js.map +0 -1
  193. package/out/util.d.ts +0 -19
  194. package/out/util.d.ts.map +0 -1
  195. package/out/util.js +0 -7
  196. package/out/util.js.map +0 -1
  197. package/parser/definition.ts +0 -159
  198. package/parser/generic.ts +0 -68
  199. package/parser/objectLiteral.ts +0 -197
  200. package/parser/semantic/bounds.ts +0 -61
  201. package/parser/semantic/divisor.ts +0 -13
  202. package/parser/semantic/intersections.ts +0 -46
  203. package/parser/semantic/semantic.ts +0 -143
  204. package/parser/semantic/utils.ts +0 -26
  205. package/parser/semantic/validate.ts +0 -109
  206. package/parser/shared.ts +0 -167
  207. package/parser/string/reduce/dynamic.ts +0 -196
  208. package/parser/string/reduce/shared.ts +0 -101
  209. package/parser/string/reduce/static.ts +0 -298
  210. package/parser/string/shift/operand/date.ts +0 -54
  211. package/parser/string/shift/operand/enclosed.ts +0 -118
  212. package/parser/string/shift/operand/genericArgs.ts +0 -127
  213. package/parser/string/shift/operand/operand.ts +0 -48
  214. package/parser/string/shift/operand/unenclosed.ts +0 -268
  215. package/parser/string/shift/operator/bounds.ts +0 -264
  216. package/parser/string/shift/operator/divisor.ts +0 -36
  217. package/parser/string/shift/operator/operator.ts +0 -82
  218. package/parser/string/shift/scanner.ts +0 -209
  219. package/parser/string/string.ts +0 -90
  220. package/parser/tuple.ts +0 -475
  221. package/scope.ts +0 -548
  222. package/type.ts +0 -349
  223. package/util.ts +0 -36
package/out/scope.js CHANGED
@@ -1,222 +1,75 @@
1
- import { BaseType, keywords } from "@arktype/schema";
2
- import { domainOf, hasDomain, isThunk, map, throwParseError } from "@arktype/util";
1
+ import { RawRootScope, hasArkKind } from "@arktype/schema";
2
+ import { domainOf, hasDomain, isThunk, throwParseError } from "@arktype/util";
3
+ import { Generic } from "./generic.js";
3
4
  import { createMatchParser } from "./match.js";
4
5
  import { parseObject, writeBadDefinitionTypeMessage } from "./parser/definition.js";
5
6
  import { parseGenericParams } from "./parser/generic.js";
6
7
  import { DynamicState } from "./parser/string/reduce/dynamic.js";
7
- import { writeMissingSubmoduleAccessMessage, writeNonSubmoduleDotMessage, writeUnresolvableMessage } from "./parser/string/shift/operand/unenclosed.js";
8
8
  import { fullStringParse } from "./parser/string/string.js";
9
- import { Type, createTypeParser, generic, validateUninstantiatedGeneric } from "./type.js";
10
- import { addArkKind, hasArkKind } from "./util.js";
11
- export class Scope {
12
- config;
9
+ import { RawTypeParser } from "./type.js";
10
+ export const scope = ((def, config = {}) => new RawScope(def, config));
11
+ export class RawScope extends RawRootScope {
13
12
  parseCache = {};
14
- resolutions;
15
- /** The set of names defined at the root-level of the scope mapped to their
16
- * corresponding definitions.**/
17
- aliases = {};
18
- exportedNames = [];
19
- ambient;
20
13
  constructor(def, config) {
14
+ const aliases = {};
21
15
  for (const k in def) {
22
16
  const parsedKey = parseScopeKey(k);
23
- this.aliases[parsedKey.name] = parsedKey.params.length
24
- ? generic(parsedKey.params, def[k], this)
25
- : def[k];
26
- if (!parsedKey.isLocal) {
27
- this.exportedNames.push(parsedKey.name);
28
- }
29
- }
30
- this.ambient = config.ambient ?? null;
31
- if (this.ambient) {
32
- // ensure exportedResolutions is populated
33
- this.ambient.export();
34
- this.resolutions = { ...this.ambient.exportedResolutions };
17
+ aliases[parsedKey.name] =
18
+ parsedKey.params.length ?
19
+ // TODO: this
20
+ new Generic(parsedKey.params, def[k], {})
21
+ : def[k];
35
22
  }
36
- else {
37
- this.resolutions = {};
38
- }
39
- this.config = config;
23
+ super(aliases, config);
40
24
  }
41
- static root = (aliases) => {
42
- return new Scope(aliases, {});
43
- };
44
- type = createTypeParser(this);
25
+ type = new RawTypeParser(this);
45
26
  match = createMatchParser(this);
46
- // TODO: decide if this API will be used for non-validated types
47
- declare = () => ({ type: this.type });
48
- scope = ((def, config = {}) => {
49
- return new Scope(def, {
50
- ambient: this.ambient,
51
- ...this.config,
52
- ...config
53
- });
54
- });
55
- define = (def) => def;
56
- toAmbient() {
57
- // TODO: private?
58
- return new Scope(this.aliases, {
59
- ambient: this,
60
- ...this.config
61
- });
62
- }
63
- // TODO: name?
64
- get(name) {
65
- return this.export()[name];
66
- }
67
- createRootContext(input) {
68
- return {
69
- path: [],
70
- scope: this,
71
- ...input
72
- };
73
- }
74
- parseDefinition(def, input) {
75
- return this.parse(def, this.createRootContext(input));
27
+ declare = (() => ({
28
+ type: this.type
29
+ })).bind(this);
30
+ define = ((def) => def).bind(this);
31
+ preparseRoot(def) {
32
+ if (isThunk(def) && !hasArkKind(def, "generic"))
33
+ return def();
34
+ return def;
35
+ }
36
+ parseRoot(def) {
37
+ // args: { this: {} as RawRoot },
38
+ return this.parse(def, {
39
+ $: this,
40
+ args: {}
41
+ // type parsing can bypass nodes if it hits the cache,
42
+ // so bind it directly (could be optimized)
43
+ }).bindScope(this);
76
44
  }
77
45
  parse(def, ctx) {
78
46
  if (typeof def === "string") {
79
- if (ctx.args !== undefined) {
47
+ if (ctx.args && Object.keys(ctx.args).every(k => !def.includes(k))) {
80
48
  // we can only rely on the cache if there are no contextual
81
49
  // resolutions like "this" or generic args
82
50
  return this.parseString(def, ctx);
83
51
  }
84
- if (!this.parseCache[def]) {
52
+ if (!this.parseCache[def])
85
53
  this.parseCache[def] = this.parseString(def, ctx);
86
- }
87
54
  return this.parseCache[def];
88
55
  }
89
- return hasDomain(def, "object")
90
- ? parseObject(def, ctx)
56
+ return hasDomain(def, "object") ?
57
+ parseObject(def, ctx)
91
58
  : throwParseError(writeBadDefinitionTypeMessage(domainOf(def)));
92
59
  }
93
60
  parseString(def, ctx) {
94
- return (this.maybeResolveNode(def) ??
61
+ return (this.maybeResolveRoot(def) ??
95
62
  ((def.endsWith("[]") &&
96
- this.maybeResolveNode(def.slice(0, -2))?.array()) ||
63
+ this.maybeResolveRoot(def.slice(0, -2))?.array()) ||
97
64
  fullStringParse(new DynamicState(def, ctx))));
98
65
  }
99
- maybeResolve(name) {
100
- const cached = this.resolutions[name];
101
- if (cached) {
102
- return cached;
103
- }
104
- let def = this.aliases[name];
105
- if (!def) {
106
- return this.maybeResolveSubalias(name);
107
- }
108
- if (isThunk(def) && !hasArkKind(def, "generic")) {
109
- def = def();
110
- }
111
- // TODO: initialize here?
112
- const resolution = hasArkKind(def, "generic")
113
- ? validateUninstantiatedGeneric(def)
114
- : hasArkKind(def, "module")
115
- ? throwParseError(writeMissingSubmoduleAccessMessage(name))
116
- : this.parseDefinition(def, this.createRootContext({ baseName: name, args: {} }));
117
- this.resolutions[name] = resolution;
118
- return resolution;
119
- }
120
- /** If name is a valid reference to a submodule alias, return its resolution */
121
- maybeResolveSubalias(name) {
122
- const dotIndex = name.indexOf(".");
123
- if (dotIndex === -1) {
124
- return;
125
- }
126
- const dotPrefix = name.slice(0, dotIndex);
127
- const prefixDef = this.aliases[dotPrefix];
128
- if (hasArkKind(prefixDef, "module")) {
129
- const resolution = prefixDef[name.slice(dotIndex + 1)]?.root;
130
- // if the first part of name is a submodule but the suffix is
131
- // unresolvable, we can throw immediately
132
- if (!resolution) {
133
- return throwParseError(writeUnresolvableMessage(name));
134
- }
135
- this.resolutions[name] = resolution;
136
- return resolution;
137
- }
138
- if (prefixDef !== undefined) {
139
- return throwParseError(writeNonSubmoduleDotMessage(dotPrefix));
140
- }
141
- // if the name includes ".", but the prefix is not an alias, it
142
- // might be something like a decimal literal, so just fall through to return
143
- }
144
- maybeResolveNode(name) {
145
- const result = this.maybeResolve(name);
146
- return result instanceof BaseType ? result : undefined;
147
- }
148
- import(...names) {
149
- return addArkKind(map(this.export(...names), (alias, value) => [
150
- `#${alias}`,
151
- value
152
- ]), "module");
153
- }
154
- bindThis() {
155
- // TODO: fix
156
- return { this: keywords.unknown };
157
- }
158
- exportedResolutions;
159
- exportCache;
160
- export(...names) {
161
- if (!this.exportCache) {
162
- this.exportCache = {};
163
- for (const name of this.exportedNames) {
164
- let def = this.aliases[name];
165
- if (hasArkKind(def, "generic")) {
166
- this.exportCache[name] = def;
167
- continue;
168
- }
169
- // TODO: thunk generics?
170
- // handle generics before invoking thunks, since they use
171
- // varargs they will incorrectly be considered thunks
172
- if (isThunk(def)) {
173
- def = def();
174
- }
175
- if (hasArkKind(def, "module")) {
176
- this.exportCache[name] = def;
177
- }
178
- else {
179
- this.exportCache[name] = new Type(this.maybeResolve(name), this);
180
- }
181
- }
182
- this.exportedResolutions = resolutionsOfModule(this.exportCache);
183
- Object.assign(this.resolutions, this.exportedResolutions);
184
- }
185
- const namesToExport = names.length ? names : this.exportedNames;
186
- return addArkKind(map(namesToExport, (_, name) => [name, this.exportCache[name]]), "module");
187
- }
188
66
  }
189
- const resolutionsOfModule = (typeSet) => {
190
- const result = {};
191
- for (const k in typeSet) {
192
- const v = typeSet[k];
193
- if (hasArkKind(v, "module")) {
194
- const innerResolutions = resolutionsOfModule(v);
195
- const prefixedResolutions = map(innerResolutions, (innerK, innerV) => [
196
- `${k}.${innerK}`,
197
- innerV
198
- ]);
199
- Object.assign(result, prefixedResolutions);
200
- }
201
- else if (hasArkKind(v, "generic")) {
202
- result[k] = v;
203
- }
204
- else {
205
- result[k] = v.root;
206
- }
207
- }
208
- return result;
209
- };
210
67
  export const writeShallowCycleErrorMessage = (name, seen) => `Alias '${name}' has a shallow resolution cycle: ${[...seen, name].join(":")}`;
211
- export const writeDuplicateNameMessage = (name) => `Duplicate name '${name}'`;
212
68
  export const parseScopeKey = (k) => {
213
- const isLocal = k[0] === "#";
214
- const name = isLocal ? k.slice(1) : k;
215
69
  const firstParamIndex = k.indexOf("<");
216
70
  if (firstParamIndex === -1) {
217
71
  return {
218
- isLocal,
219
- name,
72
+ name: k,
220
73
  params: []
221
74
  };
222
75
  }
@@ -224,9 +77,7 @@ export const parseScopeKey = (k) => {
224
77
  throwParseError(`'>' must be the last character of a generic declaration in a scope`);
225
78
  }
226
79
  return {
227
- isLocal,
228
- name: name.slice(0, firstParamIndex),
80
+ name: k.slice(0, firstParamIndex),
229
81
  params: parseGenericParams(k.slice(firstParamIndex + 1, -1))
230
82
  };
231
83
  };
232
- //# sourceMappingURL=scope.js.map
package/out/type.d.ts CHANGED
@@ -1,74 +1,76 @@
1
- import { inferred, type ArkResult, type BaseMeta, type Morph, type Out, type Predicate, type TypeNode, type distill, type extractIn, type extractOut, type includesMorphs, type inferMorphOut, type inferNarrow } from "@arktype/schema";
2
- import { Callable, type Constructor, type Json, type conform } from "@arktype/util";
1
+ import { ArkErrors, BaseRoot, type BaseMeta, type ConstraintKind, type Disjoint, type DivisorSchema, type ExactLengthSchema, type ExclusiveDateRangeSchema, type ExclusiveNumericRangeSchema, type InclusiveDateRangeSchema, type InclusiveNumericRangeSchema, type InnerRoot, type Morph, type MorphAst, type NodeSchema, type Out, type Predicate, type Prerequisite, type PrimitiveConstraintKind, type RegexSchema, type Root, type ambient, type constrain, type constraintKindOf, type distillIn, type distillOut, type exclusivizeRangeSchema, type inferIntersection, type inferMorphOut, type inferPipes, type inferPredicate, type writeInvalidOperandMessage } from "@arktype/schema";
2
+ import { Callable, type Constructor, type ErrorMessage, type array, type conform } from "@arktype/util";
3
+ import { Generic, type validateParameterString } from "./generic.js";
3
4
  import type { inferDefinition, validateDeclared, validateDefinition } from "./parser/definition.js";
4
- import { parseGenericParams, type GenericParamsParseError } from "./parser/generic.js";
5
- import type { inferIntersection } from "./parser/semantic/intersections.js";
5
+ import { parseGenericParams } from "./parser/generic.js";
6
6
  import type { IndexOneOperator, IndexZeroOperator, TupleInfixOperator } from "./parser/tuple.js";
7
- import type { Scope, bindThis } from "./scope.js";
8
- import { arkKind } from "./util.js";
9
- export type TypeParser<$> = {
7
+ import type { RawScope, Scope, bindThis } from "./scope.js";
8
+ /** The convenience properties attached to `type` */
9
+ export type TypeParserAttachments = Omit<TypeParser, never>;
10
+ export interface TypeParser<$ = {}> {
10
11
  <const def>(def: validateTypeRoot<def, $>): Type<inferTypeRoot<def, $>, $>;
11
- <const zero, const one, const rest extends readonly unknown[]>(_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<extractIn<inferTypeRoot<zero, $>>>] : one extends "=>" ? [
12
- Morph<extractOut<inferTypeRoot<zero, $>>, unknown>
13
- ] : one extends "@" ? [string | BaseMeta] : [validateTypeRoot<rest[0], $>] : []): Type<inferTypeRoot<[zero, one, ...rest], $>, $>;
12
+ <const zero, const one, const rest extends array>(_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 "@" ? [string | BaseMeta] : [validateTypeRoot<rest[0], $>] : []): Type<inferTypeRoot<[zero, one, ...rest], $>, $>;
14
13
  <params extends string, const def>(params: `<${validateParameterString<params>}>`, def: validateDefinition<def, $, {
15
14
  [param in parseGenericParams<params>[number]]: unknown;
16
15
  }>): Generic<parseGenericParams<params>, def, $>;
17
- };
16
+ errors: typeof ArkErrors;
17
+ }
18
+ export declare class RawTypeParser extends Callable<(...args: unknown[]) => BaseRoot | Generic, TypeParserAttachments> {
19
+ constructor($: RawScope);
20
+ }
18
21
  export type DeclarationParser<$> = <preinferred>() => {
19
- type: <def>(def: validateDeclared<preinferred, def, $, bindThis<def>>) => Type<preinferred, $>;
22
+ type: <def>(def: validateDeclared<preinferred, def, $ & ambient, bindThis<def>>) => Type<preinferred, $>;
20
23
  };
21
- export declare const createTypeParser: <$>(scope: Scope) => TypeParser<$>;
22
- export type DefinitionParser<$> = <def>(def: validateDefinition<def, $, bindThis<def>>) => def;
23
- export declare class Type<t = unknown, $ = any> extends Callable<(data: unknown) => ArkResult<distill<extractOut<t>>>> {
24
- definition: unknown;
25
- scope: Scope;
26
- [inferred]: t;
27
- infer: distill<extractOut<t>>;
28
- root: TypeNode<t>;
29
- allows: this["root"]["allows"];
30
- description: string;
31
- json: Json;
32
- constructor(definition: unknown, scope: Scope);
33
- configure(configOrDescription: BaseMeta | string): this;
34
- describe(description: string): this;
35
- from(literal: this["in"]["infer"]): this["in"]["infer"];
24
+ type validateChainedConstraint<kind extends ConstraintKind, t extends {
25
+ inferIn: unknown;
26
+ }> = t["inferIn"] extends Prerequisite<kind> ? t : ErrorMessage<writeInvalidOperandMessage<kind, Root<t["inferIn"]>>>;
27
+ declare class _Type<t = unknown, $ = any> extends InnerRoot<t, $> {
28
+ $: Scope<$>;
29
+ get in(): Type<this["tIn"], $>;
30
+ get out(): Type<this["tOut"], $>;
31
+ intersect<def>(def: validateTypeRoot<def, $>): Type<inferIntersection<t, inferTypeRoot<def, $>>> | Disjoint;
36
32
  and<def>(def: validateTypeRoot<def, $>): Type<inferIntersection<t, inferTypeRoot<def, $>>, $>;
37
33
  or<def>(def: validateTypeRoot<def, $>): Type<t | inferTypeRoot<def, $>, $>;
38
- morph<morph extends Morph<this["infer"]>>(morph: morph): Type<(In: this["in"]["infer"]) => Out<inferMorphOut<ReturnType<morph>>>, $>;
39
- morph<morph extends Morph<this["infer"]>, def>(morph: morph, outValidator: validateTypeRoot<def, $>): Type<(In: this["in"]["infer"]) => Out<extractOut<inferTypeRoot<def, $>>>, $>;
40
- narrow<def extends Predicate<extractOut<t>>>(def: def): Type<includesMorphs<t> extends true ? (In: this["in"]["infer"]) => Out<inferNarrow<this["infer"], def>> : inferNarrow<this["infer"], def>, $>;
41
34
  array(): Type<t[], $>;
42
- keyof(): Type<keyof this["in"]["infer"], $>;
43
- assert(data: unknown): this["infer"];
44
- equals<def>(other: validateTypeRoot<def, $>): this is Type<inferTypeRoot<def, $>, $>;
45
- extends<def>(other: validateTypeRoot<def, $>): this is Type<inferTypeRoot<def, $>, $>;
46
- private inCache?;
47
- get in(): Type<extractIn<t>, $>;
48
- outCache?: Type<extractOut<t>, $>;
49
- get out(): Type<extractOut<t>, $>;
50
- toString(): string;
35
+ keyof(): Type<keyof this["inferIn"], $>;
36
+ pipe<a extends Morph<this["infer"]>>(a: a): Type<inferPipes<t, [a]>, $>;
37
+ pipe<a extends Morph<this["infer"]>, b extends Morph<inferMorphOut<a>>>(a: a, b: b): Type<inferPipes<t, [a, b]>, $>;
38
+ pipe<a extends Morph<this["infer"]>, b extends Morph<inferMorphOut<a>>, c extends Morph<inferMorphOut<b>>>(a: a, b: b, c: c): Type<inferPipes<t, [a, b, c]>, $>;
39
+ pipe<a extends Morph<this["infer"]>, b extends Morph<inferMorphOut<a>>, c extends Morph<inferMorphOut<b>>, d extends Morph<inferMorphOut<c>>>(a: a, b: b, c: c, d: d): Type<inferPipes<t, [a, b, c, d]>, $>;
40
+ pipe<a extends Morph<this["infer"]>, b extends Morph<inferMorphOut<a>>, c extends Morph<inferMorphOut<b>>, d extends Morph<inferMorphOut<c>>, e extends Morph<inferMorphOut<d>>>(a: a, b: b, c: c, d: d, e: e): Type<inferPipes<t, [a, b, c, d, e]>, $>;
41
+ pipe<a extends Morph<this["infer"]>, b extends Morph<inferMorphOut<a>>, c extends Morph<inferMorphOut<b>>, d extends Morph<inferMorphOut<c>>, e extends Morph<inferMorphOut<d>>, f extends Morph<inferMorphOut<e>>>(a: a, b: b, c: c, d: d, e: e, f: f): Type<inferPipes<t, [a, b, c, d, e, f]>, $>;
42
+ pipe<a extends Morph<this["infer"]>, b extends Morph<inferMorphOut<a>>, c extends Morph<inferMorphOut<b>>, d extends Morph<inferMorphOut<c>>, e extends Morph<inferMorphOut<d>>, f extends Morph<inferMorphOut<e>>, g extends Morph<inferMorphOut<f>>>(a: a, b: b, c: c, d: d, e: e, f: f, g: g): Type<inferPipes<t, [a, b, c, d, e, f, g]>, $>;
43
+ narrow<const predicate extends Predicate<distillOut<t>>>(predicate: predicate): Type<t extends MorphAst ? (In: this["tIn"]) => Out<inferPredicate<this["tOut"], predicate>> : inferPredicate<t, predicate>, $>;
44
+ equals<def>(def: validateTypeRoot<def, $>): this is Type<inferTypeRoot<def>, $>;
45
+ extract<def>(r: validateTypeRoot<def, $>): Type<t, $>;
46
+ exclude<def>(r: validateTypeRoot<def, $>): Type<t, $>;
47
+ extends<def>(other: validateTypeRoot<def, $>): this is Type<inferTypeRoot<def>, $>;
48
+ overlaps<def>(r: validateTypeRoot<def, $>): boolean;
49
+ constrain<kind extends PrimitiveConstraintKind, const def extends NodeSchema<kind>>(kind: conform<kind, constraintKindOf<this["inferIn"]>>, def: def): Type<constrain<t, kind, def>, $>;
50
+ satisfying<predicate extends Predicate<distillIn<t>>>(predicate: predicate): Type<t extends MorphAst ? (In: inferPredicate<this["tIn"], predicate>) => Out<this["tOut"]> : inferPredicate<t, predicate>, $>;
51
+ divisibleBy<const schema extends DivisorSchema>(this: validateChainedConstraint<"divisor", this>, schema: schema): Type<constrain<t, "divisor", schema>, $>;
52
+ matching<const schema extends RegexSchema>(this: validateChainedConstraint<"regex", this>, schema: schema): Type<constrain<t, "regex", schema>, $>;
53
+ atLeast<const schema extends InclusiveNumericRangeSchema>(this: validateChainedConstraint<"min", this>, schema: schema): Type<constrain<t, "min", schema>, $>;
54
+ atMost<const schema extends InclusiveNumericRangeSchema>(this: validateChainedConstraint<"max", this>, schema: schema): Type<constrain<t, "max", schema>, $>;
55
+ moreThan<const schema extends ExclusiveNumericRangeSchema>(this: validateChainedConstraint<"min", this>, schema: schema): Type<constrain<t, "min", exclusivizeRangeSchema<schema>>, $>;
56
+ lessThan<const schema extends ExclusiveNumericRangeSchema>(this: validateChainedConstraint<"max", this>, schema: schema): Type<constrain<t, "max", exclusivizeRangeSchema<schema>>, $>;
57
+ atLeastLength<const schema extends InclusiveNumericRangeSchema>(this: validateChainedConstraint<"minLength", this>, schema: schema): Type<constrain<t, "minLength", schema>, $>;
58
+ atMostLength<const schema extends InclusiveNumericRangeSchema>(this: validateChainedConstraint<"maxLength", this>, schema: schema): Type<constrain<t, "maxLength", schema>, $>;
59
+ moreThanLength<const schema extends ExclusiveNumericRangeSchema>(this: validateChainedConstraint<"minLength", this>, schema: schema): Type<constrain<t, "minLength", exclusivizeRangeSchema<schema>>, $>;
60
+ lessThanLength<const schema extends ExclusiveNumericRangeSchema>(this: validateChainedConstraint<"maxLength", this>, schema: schema): Type<constrain<t, "maxLength", exclusivizeRangeSchema<schema>>, $>;
61
+ exactlyLength<const schema extends ExactLengthSchema>(this: validateChainedConstraint<"exactLength", this>, schema: schema): Type<constrain<t, "exactLength", schema>, $>;
62
+ atOrAfter<const schema extends InclusiveDateRangeSchema>(this: validateChainedConstraint<"after", this>, schema: schema): Type<constrain<t, "after", schema>, $>;
63
+ atOrBefore<const schema extends InclusiveDateRangeSchema>(this: validateChainedConstraint<"before", this>, schema: schema): Type<constrain<t, "before", schema>, $>;
64
+ laterThan<const schema extends ExclusiveDateRangeSchema>(this: validateChainedConstraint<"after", this>, schema: schema): Type<constrain<t, "after", exclusivizeRangeSchema<schema>>, $>;
65
+ earlierThan<const schema extends ExclusiveDateRangeSchema>(this: validateChainedConstraint<"before", this>, schema: schema): Type<constrain<t, "before", exclusivizeRangeSchema<schema>>, $>;
51
66
  }
52
- export type validateTypeRoot<def, $> = validateDefinition<def, $, bindThis<def>>;
53
- export type inferTypeRoot<def, $> = inferDefinition<def, $, bindThis<def>>;
54
- type validateParameterString<params extends string> = parseGenericParams<params> extends GenericParamsParseError<infer message> ? message : params;
55
- export declare const validateUninstantiatedGeneric: (g: Generic) => Generic<string[], unknown, any>;
56
- export declare const generic: (parameters: string[], definition: unknown, scope: Scope) => Generic<string[], unknown, any>;
57
- export type GenericProps<params extends string[] = string[], def = unknown, $ = any> = {
58
- [arkKind]: "generic";
59
- $: $;
60
- parameters: params;
61
- definition: def;
62
- scope: Scope;
63
- };
64
- export type BoundArgs = Record<string, TypeNode>;
65
- export type Generic<params extends string[] = string[], def = unknown, $ = any> = {
66
- <args>(...args: conform<args, {
67
- [i in keyof params]: validateTypeRoot<args[i & keyof args], $>;
68
- }>): Type<inferDefinition<def, $, bindGenericInstantiation<params, $, args>>, $>;
69
- } & GenericProps<params, def, $>;
70
- type bindGenericInstantiation<params extends string[], $, args> = {
71
- [i in keyof params & `${number}` as params[i]]: inferTypeRoot<args[i & keyof args], $>;
72
- };
67
+ export interface Type<
68
+ /** @ts-expect-error allow instantiation assignment to the base type */
69
+ out t = unknown, $ = {}> extends _Type<t, $> {
70
+ }
71
+ export type TypeConstructor<t = unknown, $ = {}> = new (def: unknown, $: Scope<$>) => Type<t, $>;
72
+ export declare const Type: TypeConstructor;
73
+ export type DefinitionParser<$> = <def>(def: validateTypeRoot<def, $>) => def;
74
+ export type validateTypeRoot<def, $ = {}> = validateDefinition<def, $ & ambient, bindThis<def>>;
75
+ export type inferTypeRoot<def, $ = {}> = inferDefinition<def, $ & ambient, bindThis<def>>;
73
76
  export {};
74
- //# sourceMappingURL=type.d.ts.map
package/out/type.js CHANGED
@@ -1,138 +1,33 @@
1
- import { inferred, keywords } from "@arktype/schema";
2
- import { Callable, map } from "@arktype/util";
1
+ import { ArkErrors, BaseRoot } from "@arktype/schema";
2
+ import { Callable } from "@arktype/util";
3
+ import { Generic } from "./generic.js";
3
4
  import { parseGenericParams } from "./parser/generic.js";
4
- import { configureShallowDescendants } from "./parser/shared.js";
5
- import { arkKind } from "./util.js";
6
- export const createTypeParser = (scope) => {
7
- const parser = (...args) => {
8
- if (args.length === 1) {
9
- // treat as a simple definition
10
- return new Type(args[0], scope);
11
- }
12
- if (args.length === 2 &&
13
- typeof args[0] === "string" &&
14
- args[0][0] === "<" &&
15
- args[0].at(-1) === ">") {
16
- // if there are exactly two args, the first of which looks like <${string}>,
17
- // treat as a generic
18
- const params = parseGenericParams(args[0].slice(1, -1));
19
- const def = args[1];
20
- return validateUninstantiatedGeneric(generic(params, def, scope));
21
- }
22
- // otherwise, treat as a tuple expression. technically, this also allows
23
- // non-expression tuple definitions to be parsed, but it's not a supported
24
- // part of the API as specified by the associated types
25
- return new Type(args, scope);
26
- };
27
- return parser;
28
- };
29
- export class Type extends Callable {
30
- definition;
31
- scope;
32
- root;
33
- allows;
34
- description;
35
- json;
36
- constructor(definition, scope) {
37
- const root = parseTypeRoot(definition, scope);
38
- super(root.apply, root);
39
- this.definition = definition;
40
- this.scope = scope;
41
- this.root = root;
42
- this.allows = root.allows;
43
- this.json = root.json;
44
- this.description = this.root.description;
45
- }
46
- configure(configOrDescription) {
47
- return new Type(configureShallowDescendants(this.root, configOrDescription), this.scope);
48
- }
49
- describe(description) {
50
- return this.configure(description);
51
- }
52
- // TODO: should return out
53
- from(literal) {
54
- return literal;
55
- }
56
- // TODO: Morph intersections, ordering
57
- and(def) {
58
- return new Type(this.root.and(parseTypeRoot(def, this.scope)), this.scope);
59
- }
60
- or(def) {
61
- return new Type(this.root.or(parseTypeRoot(def, this.scope)), this.scope);
62
- }
63
- morph(morph, outValidator) {
64
- // TODO: tuple expression for out validator
65
- outValidator;
66
- return this;
67
- // return new Type(
68
- // this.root.constrain("morph", morph),
69
- // this.scope
70
- // ) as never
71
- }
72
- // TODO: based on below, should maybe narrow morph output if used after
73
- narrow(def) {
74
- return new Type(this.root.constrain("predicate", def), this.scope);
75
- }
76
- array() {
77
- return new Type(this.root.array(), this.scope);
78
- }
79
- keyof() {
80
- return new Type(this.root.keyof(), this.scope);
81
- }
82
- assert(data) {
83
- const result = this.call(null, data);
84
- return result.errors ? result.errors.throw() : result.out;
85
- }
86
- equals(other) {
87
- return this.root.equals(parseTypeRoot(other, this.scope));
88
- }
89
- extends(other) {
90
- return this.root.extends(parseTypeRoot(other, this.scope));
91
- }
92
- inCache;
93
- get in() {
94
- this.inCache ??= new Type(this.root.in, this.scope);
95
- return this.inCache;
96
- }
97
- outCache;
98
- get out() {
99
- this.outCache ??= new Type(this.root.out, this.scope);
100
- return this.outCache;
101
- }
102
- toString() {
103
- return this.description;
5
+ const typeParserAttachments = Object.freeze({
6
+ errors: ArkErrors
7
+ });
8
+ export class RawTypeParser extends Callable {
9
+ constructor($) {
10
+ super((...args) => {
11
+ if (args.length === 1) {
12
+ // treat as a simple definition
13
+ return $.parseRoot(args[0]);
14
+ }
15
+ if (args.length === 2 &&
16
+ typeof args[0] === "string" &&
17
+ args[0][0] === "<" &&
18
+ args[0].at(-1) === ">") {
19
+ // if there are exactly two args, the first of which looks like <${string}>,
20
+ // treat as a generic
21
+ const params = parseGenericParams(args[0].slice(1, -1));
22
+ const def = args[1];
23
+ // TODO: validateUninstantiatedGeneric, remove this cast
24
+ return new Generic(params, def, $);
25
+ }
26
+ // otherwise, treat as a tuple expression. technically, this also allows
27
+ // non-expression tuple definitions to be parsed, but it's not a supported
28
+ // part of the API as specified by the associated types
29
+ return $.parseRoot(args);
30
+ }, { bind: $, attach: typeParserAttachments });
104
31
  }
105
32
  }
106
- const parseTypeRoot = (def, scope, args) => scope.parseDefinition(def, {
107
- args: args ?? scope.bindThis(),
108
- baseName: "type"
109
- });
110
- export const validateUninstantiatedGeneric = (g) => {
111
- // the unconstrained instantiation of the generic is not used for now
112
- // other than to eagerly validate that the def does not contain any errors
113
- g.scope.parseDefinition(g.definition,
114
- // once we support constraints on generic parameters, we'd use
115
- // the base type here: https://github.com/arktypeio/arktype/issues/796
116
- {
117
- baseName: "generic",
118
- args: map(g.parameters, (_, name) => [name, keywords.unknown])
119
- });
120
- return g;
121
- };
122
- export const generic = (parameters, definition, scope) => {
123
- return Object.assign((...args) => {
124
- const argNodes = map(parameters, (i, param) => [
125
- param,
126
- parseTypeRoot(args[i], scope)
127
- ]);
128
- const root = parseTypeRoot(definition, scope, argNodes);
129
- return new Type(root, scope);
130
- }, {
131
- [arkKind]: "generic",
132
- parameters,
133
- definition,
134
- scope
135
- // $ is only needed at compile-time
136
- });
137
- };
138
- //# sourceMappingURL=type.js.map
33
+ export const Type = BaseRoot;