arktype 2.0.0-dev.1 → 2.0.0-dev.11

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 (221) hide show
  1. package/out/api.d.ts +6 -0
  2. package/out/api.js +5 -0
  3. package/out/ark.d.ts +15 -39
  4. package/out/ark.js +9 -18
  5. package/out/config.d.ts +1 -0
  6. package/out/config.js +2 -0
  7. package/out/generic.d.ts +21 -0
  8. package/out/generic.js +24 -0
  9. package/out/main.d.ts +6 -7
  10. package/out/main.js +4 -3
  11. package/out/match.d.ts +57 -34
  12. package/out/match.js +51 -17
  13. package/out/module.d.ts +12 -0
  14. package/out/module.js +3 -0
  15. package/out/parser/definition.d.ts +14 -14
  16. package/out/parser/definition.js +10 -13
  17. package/out/parser/generic.d.ts +5 -7
  18. package/out/parser/generic.js +7 -11
  19. package/out/parser/objectLiteral.d.ts +44 -18
  20. package/out/parser/objectLiteral.js +57 -51
  21. package/out/parser/semantic/bounds.d.ts +10 -8
  22. package/out/parser/semantic/bounds.js +0 -1
  23. package/out/parser/semantic/divisor.d.ts +5 -7
  24. package/out/parser/semantic/divisor.js +0 -1
  25. package/out/parser/semantic/infer.d.ts +24 -0
  26. package/out/parser/semantic/infer.js +1 -0
  27. package/out/parser/semantic/utils.d.ts +3 -7
  28. package/out/parser/semantic/utils.js +0 -1
  29. package/out/parser/semantic/validate.d.ts +16 -13
  30. package/out/parser/semantic/validate.js +1 -1
  31. package/out/parser/string/reduce/dynamic.d.ts +13 -13
  32. package/out/parser/string/reduce/dynamic.js +31 -26
  33. package/out/parser/string/reduce/shared.d.ts +14 -28
  34. package/out/parser/string/reduce/shared.js +4 -3
  35. package/out/parser/string/reduce/static.d.ts +27 -24
  36. package/out/parser/string/reduce/static.js +0 -1
  37. package/out/parser/string/shift/operand/date.d.ts +2 -2
  38. package/out/parser/string/shift/operand/date.js +4 -7
  39. package/out/parser/string/shift/operand/enclosed.d.ts +3 -4
  40. package/out/parser/string/shift/operand/enclosed.js +14 -14
  41. package/out/parser/string/shift/operand/genericArgs.d.ts +9 -10
  42. package/out/parser/string/shift/operand/genericArgs.js +6 -10
  43. package/out/parser/string/shift/operand/operand.d.ts +3 -4
  44. package/out/parser/string/shift/operand/operand.js +8 -12
  45. package/out/parser/string/shift/operand/unenclosed.d.ts +16 -21
  46. package/out/parser/string/shift/operand/unenclosed.js +24 -36
  47. package/out/parser/string/shift/operator/bounds.d.ts +8 -12
  48. package/out/parser/string/shift/operator/bounds.js +45 -56
  49. package/out/parser/string/shift/operator/divisor.d.ts +1 -3
  50. package/out/parser/string/shift/operator/divisor.js +1 -3
  51. package/out/parser/string/shift/operator/operator.d.ts +1 -2
  52. package/out/parser/string/shift/operator/operator.js +12 -19
  53. package/out/parser/string/shift/scanner.d.ts +2 -2
  54. package/out/parser/string/shift/scanner.js +14 -16
  55. package/out/parser/string/string.d.ts +8 -6
  56. package/out/parser/string/string.js +1 -3
  57. package/out/parser/tuple.d.ts +102 -43
  58. package/out/parser/tuple.js +119 -87
  59. package/out/scope.d.ts +44 -119
  60. package/out/scope.js +39 -209
  61. package/out/type.d.ts +43 -64
  62. package/out/type.js +30 -131
  63. package/package.json +12 -10
  64. package/__tests__/array.test.ts +0 -272
  65. package/__tests__/astUtils.test.ts +0 -17
  66. package/__tests__/badDefinitionType.test.ts +0 -51
  67. package/__tests__/basis.test.ts +0 -46
  68. package/__tests__/bounds.test.ts +0 -388
  69. package/__tests__/cast.test.ts +0 -32
  70. package/__tests__/completions.test.ts +0 -46
  71. package/__tests__/config.test.ts +0 -168
  72. package/__tests__/cyclic.test.ts +0 -144
  73. package/__tests__/dateLiteral.test.ts +0 -32
  74. package/__tests__/declared.test.ts +0 -123
  75. package/__tests__/discrimination.test.ts +0 -74
  76. package/__tests__/divisor.test.ts +0 -90
  77. package/__tests__/enclosed.test.ts +0 -93
  78. package/__tests__/examples.test.ts +0 -56
  79. package/__tests__/expressions.test.ts +0 -138
  80. package/__tests__/generateBenchData.ts +0 -89
  81. package/__tests__/generated/cyclic.ts +0 -3058
  82. package/__tests__/generic.test.ts +0 -294
  83. package/__tests__/group.test.ts +0 -72
  84. package/__tests__/hkt.test.ts +0 -22
  85. package/__tests__/imports.test.ts +0 -113
  86. package/__tests__/instanceof.test.ts +0 -102
  87. package/__tests__/intersection.test.ts +0 -104
  88. package/__tests__/jsdoc.test.ts +0 -64
  89. package/__tests__/keyTraversal.test.ts +0 -73
  90. package/__tests__/keyof.test.ts +0 -134
  91. package/__tests__/keywords.test.ts +0 -188
  92. package/__tests__/literal.test.ts +0 -51
  93. package/__tests__/match.test.ts +0 -34
  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 -300
  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 -116
  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.map +0 -1
  125. package/out/main.js.map +0 -1
  126. package/out/match.d.ts.map +0 -1
  127. package/out/match.js.map +0 -1
  128. package/out/parser/definition.d.ts.map +0 -1
  129. package/out/parser/definition.js.map +0 -1
  130. package/out/parser/generic.d.ts.map +0 -1
  131. package/out/parser/generic.js.map +0 -1
  132. package/out/parser/objectLiteral.d.ts.map +0 -1
  133. package/out/parser/objectLiteral.js.map +0 -1
  134. package/out/parser/semantic/bounds.d.ts.map +0 -1
  135. package/out/parser/semantic/bounds.js.map +0 -1
  136. package/out/parser/semantic/divisor.d.ts.map +0 -1
  137. package/out/parser/semantic/divisor.js.map +0 -1
  138. package/out/parser/semantic/intersections.d.ts +0 -14
  139. package/out/parser/semantic/intersections.d.ts.map +0 -1
  140. package/out/parser/semantic/intersections.js +0 -2
  141. package/out/parser/semantic/intersections.js.map +0 -1
  142. package/out/parser/semantic/semantic.d.ts +0 -33
  143. package/out/parser/semantic/semantic.d.ts.map +0 -1
  144. package/out/parser/semantic/semantic.js +0 -2
  145. package/out/parser/semantic/semantic.js.map +0 -1
  146. package/out/parser/semantic/utils.d.ts.map +0 -1
  147. package/out/parser/semantic/utils.js.map +0 -1
  148. package/out/parser/semantic/validate.d.ts.map +0 -1
  149. package/out/parser/semantic/validate.js.map +0 -1
  150. package/out/parser/shared.d.ts +0 -54
  151. package/out/parser/shared.d.ts.map +0 -1
  152. package/out/parser/shared.js +0 -47
  153. package/out/parser/shared.js.map +0 -1
  154. package/out/parser/string/reduce/dynamic.d.ts.map +0 -1
  155. package/out/parser/string/reduce/dynamic.js.map +0 -1
  156. package/out/parser/string/reduce/shared.d.ts.map +0 -1
  157. package/out/parser/string/reduce/shared.js.map +0 -1
  158. package/out/parser/string/reduce/static.d.ts.map +0 -1
  159. package/out/parser/string/reduce/static.js.map +0 -1
  160. package/out/parser/string/shift/operand/date.d.ts.map +0 -1
  161. package/out/parser/string/shift/operand/date.js.map +0 -1
  162. package/out/parser/string/shift/operand/enclosed.d.ts.map +0 -1
  163. package/out/parser/string/shift/operand/enclosed.js.map +0 -1
  164. package/out/parser/string/shift/operand/genericArgs.d.ts.map +0 -1
  165. package/out/parser/string/shift/operand/genericArgs.js.map +0 -1
  166. package/out/parser/string/shift/operand/operand.d.ts.map +0 -1
  167. package/out/parser/string/shift/operand/operand.js.map +0 -1
  168. package/out/parser/string/shift/operand/unenclosed.d.ts.map +0 -1
  169. package/out/parser/string/shift/operand/unenclosed.js.map +0 -1
  170. package/out/parser/string/shift/operator/bounds.d.ts.map +0 -1
  171. package/out/parser/string/shift/operator/bounds.js.map +0 -1
  172. package/out/parser/string/shift/operator/divisor.d.ts.map +0 -1
  173. package/out/parser/string/shift/operator/divisor.js.map +0 -1
  174. package/out/parser/string/shift/operator/operator.d.ts.map +0 -1
  175. package/out/parser/string/shift/operator/operator.js.map +0 -1
  176. package/out/parser/string/shift/scanner.d.ts.map +0 -1
  177. package/out/parser/string/shift/scanner.js.map +0 -1
  178. package/out/parser/string/string.d.ts.map +0 -1
  179. package/out/parser/string/string.js.map +0 -1
  180. package/out/parser/tuple.d.ts.map +0 -1
  181. package/out/parser/tuple.js.map +0 -1
  182. package/out/scope.d.ts.map +0 -1
  183. package/out/scope.js.map +0 -1
  184. package/out/test.d.ts +0 -2
  185. package/out/test.d.ts.map +0 -1
  186. package/out/test.js +0 -7
  187. package/out/test.js.map +0 -1
  188. package/out/tsconfig.build.tsbuildinfo +0 -1
  189. package/out/type.d.ts.map +0 -1
  190. package/out/type.js.map +0 -1
  191. package/out/util.d.ts +0 -19
  192. package/out/util.d.ts.map +0 -1
  193. package/out/util.js +0 -7
  194. package/out/util.js.map +0 -1
  195. package/parser/definition.ts +0 -159
  196. package/parser/generic.ts +0 -68
  197. package/parser/objectLiteral.ts +0 -197
  198. package/parser/semantic/bounds.ts +0 -61
  199. package/parser/semantic/divisor.ts +0 -13
  200. package/parser/semantic/intersections.ts +0 -46
  201. package/parser/semantic/semantic.ts +0 -144
  202. package/parser/semantic/utils.ts +0 -26
  203. package/parser/semantic/validate.ts +0 -109
  204. package/parser/shared.ts +0 -152
  205. package/parser/string/reduce/dynamic.ts +0 -196
  206. package/parser/string/reduce/shared.ts +0 -101
  207. package/parser/string/reduce/static.ts +0 -298
  208. package/parser/string/shift/operand/date.ts +0 -54
  209. package/parser/string/shift/operand/enclosed.ts +0 -118
  210. package/parser/string/shift/operand/genericArgs.ts +0 -127
  211. package/parser/string/shift/operand/operand.ts +0 -48
  212. package/parser/string/shift/operand/unenclosed.ts +0 -268
  213. package/parser/string/shift/operator/bounds.ts +0 -264
  214. package/parser/string/shift/operator/divisor.ts +0 -36
  215. package/parser/string/shift/operator/operator.ts +0 -82
  216. package/parser/string/shift/scanner.ts +0 -209
  217. package/parser/string/string.ts +0 -90
  218. package/parser/tuple.ts +0 -474
  219. package/scope.ts +0 -574
  220. package/type.ts +0 -347
  221. package/util.ts +0 -36
package/out/scope.js CHANGED
@@ -1,243 +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
- compile() {
155
- return "";
156
- // this.export()
157
- // const references: Set<Root> = new Set()
158
- // for (const k in this.exportedResolutions!) {
159
- // const resolution = this.exportedResolutions[k]
160
- // if (hasArkKind(resolution, "node") && !references.has(resolution)) {
161
- // for (const reference of resolution.references) {
162
- // references.add(reference)
163
- // }
164
- // }
165
- // }
166
- // return [...references]
167
- // .map(
168
- // (ref) => `const ${ref.alias} = (${In}) => {
169
- // ${ref.condition}
170
- // return true
171
- // }`
172
- // )
173
- // .join("\n")
174
- }
175
- bindThis() {
176
- // TODO: fix
177
- return { this: keywords.unknown };
178
- }
179
- exportedResolutions;
180
- exportCache;
181
- export(...names) {
182
- if (!this.exportCache) {
183
- this.exportCache = {};
184
- for (const name of this.exportedNames) {
185
- let def = this.aliases[name];
186
- if (hasArkKind(def, "generic")) {
187
- this.exportCache[name] = def;
188
- continue;
189
- }
190
- // TODO: thunk generics?
191
- // handle generics before invoking thunks, since they use
192
- // varargs they will incorrectly be considered thunks
193
- if (isThunk(def)) {
194
- def = def();
195
- }
196
- if (hasArkKind(def, "module")) {
197
- this.exportCache[name] = def;
198
- }
199
- else {
200
- this.exportCache[name] = new Type(this.maybeResolve(name), this);
201
- }
202
- }
203
- this.exportedResolutions = resolutionsOfModule(this.exportCache);
204
- Object.assign(this.resolutions, this.exportedResolutions);
205
- }
206
- const namesToExport = names.length ? names : this.exportedNames;
207
- return addArkKind(map(namesToExport, (_, name) => [name, this.exportCache[name]]), "module");
208
- }
209
66
  }
210
- const resolutionsOfModule = (typeSet) => {
211
- const result = {};
212
- for (const k in typeSet) {
213
- const v = typeSet[k];
214
- if (hasArkKind(v, "module")) {
215
- const innerResolutions = resolutionsOfModule(v);
216
- const prefixedResolutions = map(innerResolutions, (innerK, innerV) => [
217
- `${k}.${innerK}`,
218
- innerV
219
- ]);
220
- Object.assign(result, prefixedResolutions);
221
- }
222
- else if (hasArkKind(v, "generic")) {
223
- result[k] = v;
224
- }
225
- else {
226
- result[k] = v.root;
227
- }
228
- }
229
- return result;
230
- };
231
67
  export const writeShallowCycleErrorMessage = (name, seen) => `Alias '${name}' has a shallow resolution cycle: ${[...seen, name].join(":")}`;
232
- export const writeDuplicateNameMessage = (name) => `Duplicate name '${name}'`;
233
68
  export const parseScopeKey = (k) => {
234
- const isLocal = k[0] === "#";
235
- const name = isLocal ? k.slice(1) : k;
236
69
  const firstParamIndex = k.indexOf("<");
237
70
  if (firstParamIndex === -1) {
238
71
  return {
239
- isLocal,
240
- name,
72
+ name: k,
241
73
  params: []
242
74
  };
243
75
  }
@@ -245,9 +77,7 @@ export const parseScopeKey = (k) => {
245
77
  throwParseError(`'>' must be the last character of a generic declaration in a scope`);
246
78
  }
247
79
  return {
248
- isLocal,
249
- name: name.slice(0, firstParamIndex),
80
+ name: k.slice(0, firstParamIndex),
250
81
  params: parseGenericParams(k.slice(firstParamIndex + 1, -1))
251
82
  };
252
83
  };
253
- //# sourceMappingURL=scope.js.map
package/out/type.d.ts CHANGED
@@ -1,77 +1,56 @@
1
- import { inferred, type ArkResult, type BaseMeta, type KeyCheckKind, 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 Disjoint, type InnerRoot, type Morph, type NodeSchema, type Out, type Predicate, type PrimitiveConstraintKind, type Root, type ambient, type constrain, type constraintKindOf, type distillConstrainableOut, type distillIn, type distillOut, type includesMorphs, type inferIntersection, type inferMorphOut, type inferNarrow, type inferPipes, type internalImplementationOf } from "@arktype/schema";
2
+ import { Callable, type Constructor, 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
+ export interface TypeParserAttachments {
9
+ errors: typeof ArkErrors;
10
+ }
11
+ export interface TypeParser<$> extends TypeParserAttachments {
10
12
  <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], $>, $>;
13
+ <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
14
  <params extends string, const def>(params: `<${validateParameterString<params>}>`, def: validateDefinition<def, $, {
15
15
  [param in parseGenericParams<params>[number]]: unknown;
16
16
  }>): Generic<parseGenericParams<params>, def, $>;
17
- };
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 type TypeConfig = {
24
- keys?: KeyCheckKind;
25
- mustBe?: string;
26
- };
27
- export declare class Type<t = unknown, $ = any> extends Callable<(data: unknown) => ArkResult<distill<extractOut<t>>>> {
28
- definition: unknown;
29
- scope: Scope;
30
- [inferred]: t;
31
- infer: distill<extractOut<t>>;
32
- config: TypeConfig;
33
- root: TypeNode<t>;
34
- allows: this["root"]["allows"];
35
- expected: string;
36
- json: Json;
37
- constructor(definition: unknown, scope: Scope);
38
- configure(config: TypeConfig): this;
39
- from(literal: this["in"]["infer"]): this["in"]["infer"];
24
+ declare class _Type<t = unknown, $ = any> extends InnerRoot<t, $> implements internalImplementationOf<Root> {
25
+ $: Scope<$>;
26
+ get in(): Type<this["tIn"], $>;
27
+ get out(): Type<this["tOut"], $>;
28
+ intersect<def>(def: validateTypeRoot<def, $>): Type<inferIntersection<t, inferTypeRoot<def, $>>> | Disjoint;
40
29
  and<def>(def: validateTypeRoot<def, $>): Type<inferIntersection<t, inferTypeRoot<def, $>>, $>;
41
30
  or<def>(def: validateTypeRoot<def, $>): Type<t | inferTypeRoot<def, $>, $>;
42
- morph<morph extends Morph<this["infer"]>>(morph: morph): Type<(In: this["in"]["infer"]) => Out<inferMorphOut<ReturnType<morph>>>, $>;
43
- morph<morph extends Morph<this["infer"]>, def>(morph: morph, outValidator: validateTypeRoot<def, $>): Type<(In: this["in"]["infer"]) => Out<extractOut<inferTypeRoot<def, $>>>, $>;
44
- 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>, $>;
45
31
  array(): Type<t[], $>;
46
- keyof(): Type<keyof this["in"]["infer"], $>;
47
- assert(data: unknown): this["infer"];
48
- equals<def>(other: validateTypeRoot<def, $>): this is Type<inferTypeRoot<def, $>, $>;
49
- extends<def>(other: validateTypeRoot<def, $>): this is Type<inferTypeRoot<def, $>, $>;
50
- private inCache?;
51
- get in(): Type<extractIn<t>, $>;
52
- outCache?: Type<extractOut<t>, $>;
53
- get out(): Type<extractOut<t>, $>;
32
+ keyof(): Type<keyof this["inferIn"], $>;
33
+ pipe<a extends Morph<this["infer"]>>(a: a): Type<inferPipes<t, [a]>, $>;
34
+ pipe<a extends Morph<this["infer"]>, b extends Morph<inferMorphOut<a>>>(a: a, b: b): Type<inferPipes<t, [a, b]>, $>;
35
+ 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]>, $>;
36
+ 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]>, $>;
37
+ 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]>, $>;
38
+ 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]>, $>;
39
+ 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]>, $>;
40
+ narrow<def extends Predicate<distillConstrainableOut<t>>>(def: def): Type<includesMorphs<t> extends true ? (In: this["tIn"]) => Out<inferNarrow<this["infer"], def>> : inferNarrow<this["infer"], def>, $>;
41
+ equals<def>(def: validateTypeRoot<def, $>): this is Type<inferTypeRoot<def>, $>;
42
+ extract<def>(r: validateTypeRoot<def, $>): Type<t, $>;
43
+ exclude<def>(r: validateTypeRoot<def, $>): Type<t, $>;
44
+ extends<def>(other: validateTypeRoot<def, $>): this is Type<inferTypeRoot<def>, $>;
45
+ constrain<kind extends PrimitiveConstraintKind, const def extends NodeSchema<kind>>(kind: conform<kind, constraintKindOf<this["inferIn"]>>, def: def): Type<constrain<t, kind, def>, $>;
54
46
  }
55
- export type validateTypeRoot<def, $> = validateDefinition<def, $, bindThis<def>>;
56
- export type inferTypeRoot<def, $> = inferDefinition<def, $, bindThis<def>>;
57
- type validateParameterString<params extends string> = parseGenericParams<params> extends GenericParamsParseError<infer message> ? message : params;
58
- export declare const validateUninstantiatedGeneric: (g: Generic) => Generic<string[], unknown, any>;
59
- export declare const generic: (parameters: string[], definition: unknown, scope: Scope) => Generic<string[], unknown, any>;
60
- export type GenericProps<params extends string[] = string[], def = unknown, $ = any> = {
61
- [arkKind]: "generic";
62
- $: $;
63
- parameters: params;
64
- definition: def;
65
- scope: Scope;
66
- };
67
- export type BoundArgs = Record<string, TypeNode>;
68
- export type Generic<params extends string[] = string[], def = unknown, $ = any> = {
69
- <args>(...args: conform<args, {
70
- [i in keyof params]: validateTypeRoot<args[i & keyof args], $>;
71
- }>): Type<inferDefinition<def, $, bindGenericInstantiation<params, $, args>>, $>;
72
- } & GenericProps<params, def, $>;
73
- type bindGenericInstantiation<params extends string[], $, args> = {
74
- [i in keyof params & `${number}` as params[i]]: inferTypeRoot<args[i & keyof args], $>;
75
- };
47
+ export interface Type<
48
+ /** @ts-expect-error allow instantiation assignment to the base type */
49
+ out t = unknown, $ = any> extends _Type<t, $> {
50
+ }
51
+ export type TypeConstructor<t = unknown, $ = any> = new (def: unknown, $: Scope<$>) => Type<t, $>;
52
+ export declare const Type: TypeConstructor;
53
+ export type DefinitionParser<$> = <def>(def: validateTypeRoot<def, $>) => def;
54
+ export type validateTypeRoot<def, $ = {}> = validateDefinition<def, $ & ambient, bindThis<def>>;
55
+ export type inferTypeRoot<def, $ = {}> = inferDefinition<def, $ & ambient, bindThis<def>>;
76
56
  export {};
77
- //# sourceMappingURL=type.d.ts.map
package/out/type.js CHANGED
@@ -1,134 +1,33 @@
1
- import { inferred, keywords } from "@arktype/schema";
2
- import { Callable, CastableBase, 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 { arkKind } from "./util.js";
5
- export const createTypeParser = (scope) => {
6
- const parser = (...args) => {
7
- if (args.length === 1) {
8
- // treat as a simple definition
9
- return new Type(args[0], scope);
10
- }
11
- if (args.length === 2 &&
12
- typeof args[0] === "string" &&
13
- args[0][0] === "<" &&
14
- args[0].at(-1) === ">") {
15
- // if there are exactly two args, the first of which looks like <${string}>,
16
- // treat as a generic
17
- const params = parseGenericParams(args[0].slice(1, -1));
18
- const def = args[1];
19
- return validateUninstantiatedGeneric(generic(params, def, scope));
20
- }
21
- // otherwise, treat as a tuple expression. technically, this also allows
22
- // non-expression tuple definitions to be parsed, but it's not a supported
23
- // part of the API as specified by the associated types
24
- return new Type(args, scope);
25
- };
26
- return parser;
27
- };
28
- export class Type extends Callable {
29
- definition;
30
- scope;
31
- config;
32
- root;
33
- allows;
34
- expected;
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.config = scope.config;
44
- this.json = root.json;
45
- this.expected = this.root.description;
46
- }
47
- configure(config) {
48
- this.config = { ...this.config, ...config };
49
- return this;
50
- }
51
- // TODO: should return out
52
- from(literal) {
53
- return literal;
54
- }
55
- // TODO: Morph intersections, ordering
56
- and(def) {
57
- return new Type(this.root.and(parseTypeRoot(def, this.scope)), this.scope);
58
- }
59
- or(def) {
60
- return new Type(this.root.or(parseTypeRoot(def, this.scope)), this.scope);
61
- }
62
- morph(morph, outValidator) {
63
- // TODO: tuple expression for out validator
64
- outValidator;
65
- return this;
66
- // return new Type(
67
- // this.root.constrain("morph", morph),
68
- // this.scope
69
- // ) as never
70
- }
71
- // TODO: based on below, should maybe narrow morph output if used after
72
- narrow(def) {
73
- return new Type(this.root.constrain("predicate", def), this.scope);
74
- }
75
- array() {
76
- return new Type(this.root.array(), this.scope);
77
- }
78
- keyof() {
79
- return new Type(this.root.keyof(), this.scope);
80
- }
81
- assert(data) {
82
- const result = this.call(null, data);
83
- return result.errors ? result.errors.throw() : result.out;
84
- }
85
- equals(other) {
86
- return this.root.equals(parseTypeRoot(other, this.scope));
87
- }
88
- extends(other) {
89
- return this.root.extends(parseTypeRoot(other, this.scope));
90
- }
91
- inCache;
92
- get in() {
93
- this.inCache ??= new Type(this.root.in, this.scope);
94
- return this.inCache;
95
- }
96
- outCache;
97
- get out() {
98
- this.outCache ??= new Type(this.root.out, this.scope);
99
- return this.outCache;
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 });
100
31
  }
101
32
  }
102
- const parseTypeRoot = (def, scope, args) => scope.parseDefinition(def, {
103
- args: args ?? scope.bindThis(),
104
- baseName: "type"
105
- });
106
- export const validateUninstantiatedGeneric = (g) => {
107
- // the unconstrained instantiation of the generic is not used for now
108
- // other than to eagerly validate that the def does not contain any errors
109
- g.scope.parseDefinition(g.definition,
110
- // once we support constraints on generic parameters, we'd use
111
- // the base type here: https://github.com/arktypeio/arktype/issues/796
112
- {
113
- baseName: "generic",
114
- args: map(g.parameters, (_, name) => [name, keywords.unknown])
115
- });
116
- return g;
117
- };
118
- export const generic = (parameters, definition, scope) => {
119
- return Object.assign((...args) => {
120
- const argNodes = map(parameters, (i, param) => [
121
- param,
122
- parseTypeRoot(args[i], scope)
123
- ]);
124
- const root = parseTypeRoot(definition, scope, argNodes);
125
- return new Type(root, scope);
126
- }, {
127
- [arkKind]: "generic",
128
- parameters,
129
- definition,
130
- scope
131
- // $ is only needed at compile-time
132
- });
133
- };
134
- //# sourceMappingURL=type.js.map
33
+ export const Type = BaseRoot;
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-dev.1",
4
+ "version": "2.0.0-dev.11",
5
5
  "license": "MIT",
6
6
  "author": {
7
7
  "name": "David Blass",
@@ -13,28 +13,30 @@
13
13
  "url": "https://github.com/arktypeio/arktype.git"
14
14
  },
15
15
  "type": "module",
16
- "main": "./out/main.js",
17
- "types": "./out/main.d.ts",
16
+ "main": "./out/api.js",
17
+ "types": "./out/api.d.ts",
18
18
  "exports": {
19
19
  ".": {
20
- "types": "./out/main.d.ts",
21
- "default": "./out/main.js"
20
+ "types": "./out/api.d.ts",
21
+ "default": "./out/api.js"
22
+ },
23
+ "./config": {
24
+ "types": "./out/config.d.ts",
25
+ "default": "./out/config.js"
22
26
  },
23
27
  "./internal/*": {
24
28
  "default": "./out/*"
25
29
  }
26
30
  },
27
31
  "files": [
28
- "out",
29
- "!__tests__",
30
- "**/*.ts"
32
+ "out"
31
33
  ],
32
34
  "scripts": {
33
35
  "build": "tsx ../repo/build.ts",
34
36
  "test": "tsx ../repo/testPackage.ts"
35
37
  },
36
38
  "dependencies": {
37
- "@arktype/util": "0.0.17",
38
- "@arktype/schema": "0.0.3"
39
+ "@arktype/util": "0.0.38",
40
+ "@arktype/schema": "0.1.2"
39
41
  }
40
42
  }