arktype 1.1.0-dev-2 → 2.0.0-dev.1

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 (296) hide show
  1. package/__tests__/array.test.ts +272 -0
  2. package/__tests__/astUtils.test.ts +17 -0
  3. package/__tests__/badDefinitionType.test.ts +51 -0
  4. package/__tests__/basis.test.ts +46 -0
  5. package/__tests__/bounds.test.ts +388 -0
  6. package/__tests__/cast.test.ts +32 -0
  7. package/__tests__/completions.test.ts +46 -0
  8. package/__tests__/config.test.ts +168 -0
  9. package/__tests__/cyclic.test.ts +144 -0
  10. package/__tests__/dateLiteral.test.ts +32 -0
  11. package/__tests__/declared.test.ts +123 -0
  12. package/__tests__/discrimination.test.ts +74 -0
  13. package/__tests__/divisor.test.ts +90 -0
  14. package/__tests__/enclosed.test.ts +93 -0
  15. package/__tests__/examples.test.ts +56 -0
  16. package/__tests__/expressions.test.ts +138 -0
  17. package/__tests__/generateBenchData.ts +89 -0
  18. package/__tests__/generated/cyclic.ts +3058 -0
  19. package/__tests__/generic.test.ts +294 -0
  20. package/__tests__/group.test.ts +72 -0
  21. package/__tests__/hkt.test.ts +22 -0
  22. package/__tests__/imports.test.ts +113 -0
  23. package/__tests__/instanceof.test.ts +102 -0
  24. package/__tests__/intersection.test.ts +104 -0
  25. package/__tests__/jsdoc.test.ts +64 -0
  26. package/__tests__/keyTraversal.test.ts +73 -0
  27. package/__tests__/keyof.test.ts +134 -0
  28. package/__tests__/keywords.test.ts +188 -0
  29. package/__tests__/literal.test.ts +51 -0
  30. package/__tests__/match.test.ts +34 -0
  31. package/__tests__/methods.bench.ts +22 -0
  32. package/__tests__/morph.test.ts +409 -0
  33. package/__tests__/narrow.test.ts +102 -0
  34. package/__tests__/nodeDefinitions.test.ts +190 -0
  35. package/__tests__/object.bench.ts +28 -0
  36. package/__tests__/objectLiteral.test.ts +300 -0
  37. package/__tests__/operand.bench.ts +45 -0
  38. package/__tests__/operator.bench.ts +130 -0
  39. package/__tests__/regex.test.ts +58 -0
  40. package/__tests__/scope.bench.ts +39 -0
  41. package/__tests__/scope.test.ts +140 -0
  42. package/__tests__/string.test.ts +48 -0
  43. package/__tests__/submodule.test.ts +99 -0
  44. package/__tests__/this.test.ts +57 -0
  45. package/__tests__/thunk.test.ts +80 -0
  46. package/__tests__/traverse.test.ts +119 -0
  47. package/__tests__/tsGenerics.test.ts +12 -0
  48. package/__tests__/typeMethods.test.ts +69 -0
  49. package/__tests__/typeReference.test.ts +39 -0
  50. package/__tests__/unenclosed.test.ts +120 -0
  51. package/__tests__/union.test.ts +147 -0
  52. package/ark.ts +91 -0
  53. package/main.ts +7 -9
  54. package/match.ts +47 -68
  55. package/out/ark.d.ts +52 -0
  56. package/out/ark.d.ts.map +1 -0
  57. package/out/ark.js +19 -0
  58. package/out/ark.js.map +1 -0
  59. package/out/arkConfig.d.ts +2 -0
  60. package/out/arkConfig.d.ts.map +1 -0
  61. package/out/arkConfig.js +10 -0
  62. package/out/arkConfig.js.map +1 -0
  63. package/out/main.d.ts +7 -0
  64. package/out/main.d.ts.map +1 -0
  65. package/out/main.js +4 -0
  66. package/out/main.js.map +1 -0
  67. package/out/match.d.ts +48 -0
  68. package/out/match.d.ts.map +1 -0
  69. package/{dist → out}/match.js +0 -5
  70. package/out/match.js.map +1 -0
  71. package/{dist → out}/parser/definition.d.ts +3 -3
  72. package/out/parser/definition.d.ts.map +1 -0
  73. package/{dist → out}/parser/definition.js +8 -5
  74. package/out/parser/definition.js.map +1 -0
  75. package/out/parser/generic.d.ts +18 -0
  76. package/out/parser/generic.d.ts.map +1 -0
  77. package/out/parser/generic.js +19 -0
  78. package/out/parser/generic.js.map +1 -0
  79. package/{dist → out}/parser/objectLiteral.d.ts +14 -5
  80. package/out/parser/objectLiteral.d.ts.map +1 -0
  81. package/out/parser/objectLiteral.js +66 -0
  82. package/out/parser/objectLiteral.js.map +1 -0
  83. package/out/parser/semantic/bounds.d.ts +13 -0
  84. package/out/parser/semantic/bounds.d.ts.map +1 -0
  85. package/{dist → out}/parser/semantic/bounds.js.map +1 -1
  86. package/{dist → out}/parser/semantic/divisor.d.ts +2 -2
  87. package/{dist → out}/parser/semantic/divisor.d.ts.map +1 -1
  88. package/{dist → out}/parser/semantic/intersections.d.ts +1 -2
  89. package/out/parser/semantic/intersections.d.ts.map +1 -0
  90. package/out/parser/semantic/semantic.d.ts +33 -0
  91. package/out/parser/semantic/semantic.d.ts.map +1 -0
  92. package/{dist → out}/parser/semantic/utils.d.ts +1 -1
  93. package/{dist → out}/parser/semantic/utils.d.ts.map +1 -1
  94. package/{dist → out}/parser/semantic/validate.d.ts +3 -3
  95. package/out/parser/semantic/validate.d.ts.map +1 -0
  96. package/{dist → out}/parser/semantic/validate.js.map +1 -1
  97. package/{dist → out}/parser/shared.d.ts +7 -1
  98. package/out/parser/shared.d.ts.map +1 -0
  99. package/{dist → out}/parser/shared.js +7 -1
  100. package/out/parser/shared.js.map +1 -0
  101. package/{dist → out}/parser/string/reduce/dynamic.d.ts +11 -9
  102. package/out/parser/string/reduce/dynamic.d.ts.map +1 -0
  103. package/{dist → out}/parser/string/reduce/dynamic.js +13 -2
  104. package/out/parser/string/reduce/dynamic.js.map +1 -0
  105. package/{dist → out}/parser/string/reduce/shared.d.ts +29 -2
  106. package/{dist → out}/parser/string/reduce/shared.d.ts.map +1 -1
  107. package/{dist → out}/parser/string/reduce/shared.js +20 -1
  108. package/out/parser/string/reduce/shared.js.map +1 -0
  109. package/{dist → out}/parser/string/reduce/static.d.ts +1 -2
  110. package/out/parser/string/reduce/static.d.ts.map +1 -0
  111. package/{dist → out}/parser/string/shift/operand/date.d.ts +3 -3
  112. package/out/parser/string/shift/operand/date.d.ts.map +1 -0
  113. package/{dist → out}/parser/string/shift/operand/date.js.map +1 -1
  114. package/{dist → out}/parser/string/shift/operand/enclosed.d.ts +2 -2
  115. package/{dist → out}/parser/string/shift/operand/enclosed.d.ts.map +1 -1
  116. package/{dist → out}/parser/string/shift/operand/enclosed.js +4 -4
  117. package/out/parser/string/shift/operand/enclosed.js.map +1 -0
  118. package/out/parser/string/shift/operand/genericArgs.d.ts +29 -0
  119. package/out/parser/string/shift/operand/genericArgs.d.ts.map +1 -0
  120. package/out/parser/string/shift/operand/genericArgs.js +25 -0
  121. package/out/parser/string/shift/operand/genericArgs.js.map +1 -0
  122. package/{dist → out}/parser/string/shift/operand/operand.d.ts.map +1 -1
  123. package/{dist → out}/parser/string/shift/operand/operand.js.map +1 -1
  124. package/{dist → out}/parser/string/shift/operand/unenclosed.d.ts +7 -9
  125. package/out/parser/string/shift/operand/unenclosed.d.ts.map +1 -0
  126. package/{dist → out}/parser/string/shift/operand/unenclosed.js +10 -10
  127. package/out/parser/string/shift/operand/unenclosed.js.map +1 -0
  128. package/{dist → out}/parser/string/shift/operator/bounds.d.ts +11 -33
  129. package/out/parser/string/shift/operator/bounds.d.ts.map +1 -0
  130. package/out/parser/string/shift/operator/bounds.js +106 -0
  131. package/out/parser/string/shift/operator/bounds.js.map +1 -0
  132. package/{dist → out}/parser/string/shift/operator/divisor.js.map +1 -1
  133. package/out/parser/string/shift/operator/operator.d.ts.map +1 -0
  134. package/{dist → out}/parser/string/shift/operator/operator.js.map +1 -1
  135. package/{dist → out}/parser/string/shift/scanner.d.ts +1 -1
  136. package/{dist → out}/parser/string/shift/scanner.d.ts.map +1 -1
  137. package/out/parser/string/shift/scanner.js.map +1 -0
  138. package/{dist → out}/parser/string/string.d.ts +9 -11
  139. package/out/parser/string/string.d.ts.map +1 -0
  140. package/out/parser/string/string.js +23 -0
  141. package/out/parser/string/string.js.map +1 -0
  142. package/{dist → out}/parser/tuple.d.ts +9 -11
  143. package/out/parser/tuple.d.ts.map +1 -0
  144. package/{dist → out}/parser/tuple.js +17 -14
  145. package/out/parser/tuple.js.map +1 -0
  146. package/{dist → out}/scope.d.ts +16 -16
  147. package/out/scope.d.ts.map +1 -0
  148. package/{dist → out}/scope.js +27 -22
  149. package/out/scope.js.map +1 -0
  150. package/out/test.d.ts +2 -0
  151. package/out/test.d.ts.map +1 -0
  152. package/out/test.js +7 -0
  153. package/out/test.js.map +1 -0
  154. package/out/tsconfig.build.tsbuildinfo +1 -0
  155. package/{dist → out}/type.d.ts +23 -47
  156. package/out/type.d.ts.map +1 -0
  157. package/{dist → out}/type.js +27 -31
  158. package/out/type.js.map +1 -0
  159. package/out/util.d.ts +19 -0
  160. package/out/util.d.ts.map +1 -0
  161. package/out/util.js +7 -0
  162. package/out/util.js.map +1 -0
  163. package/package.json +13 -15
  164. package/parser/definition.ts +58 -55
  165. package/parser/generic.ts +17 -157
  166. package/parser/objectLiteral.ts +107 -30
  167. package/parser/semantic/bounds.ts +22 -24
  168. package/parser/semantic/divisor.ts +2 -2
  169. package/parser/semantic/intersections.ts +17 -18
  170. package/parser/semantic/semantic.ts +80 -43
  171. package/parser/semantic/utils.ts +7 -7
  172. package/parser/semantic/validate.ts +40 -36
  173. package/parser/shared.ts +28 -17
  174. package/parser/string/reduce/dynamic.ts +26 -11
  175. package/parser/string/reduce/shared.ts +35 -7
  176. package/parser/string/reduce/static.ts +20 -22
  177. package/parser/string/shift/operand/date.ts +1 -4
  178. package/parser/string/shift/operand/enclosed.ts +5 -6
  179. package/parser/string/shift/operand/genericArgs.ts +127 -0
  180. package/parser/string/shift/operand/operand.ts +21 -21
  181. package/parser/string/shift/operand/unenclosed.ts +54 -68
  182. package/parser/string/shift/operator/bounds.ts +99 -64
  183. package/parser/string/shift/operator/operator.ts +31 -31
  184. package/parser/string/shift/scanner.ts +7 -7
  185. package/parser/string/string.ts +29 -32
  186. package/parser/tuple.ts +87 -88
  187. package/scope.ts +81 -80
  188. package/type.ts +76 -132
  189. package/util.ts +36 -0
  190. package/dist/main.d.ts +0 -11
  191. package/dist/main.d.ts.map +0 -1
  192. package/dist/main.js +0 -8
  193. package/dist/main.js.map +0 -1
  194. package/dist/match.d.ts +0 -46
  195. package/dist/match.d.ts.map +0 -1
  196. package/dist/match.js.map +0 -1
  197. package/dist/parser/definition.d.ts.map +0 -1
  198. package/dist/parser/definition.js.map +0 -1
  199. package/dist/parser/generic.d.ts +0 -44
  200. package/dist/parser/generic.d.ts.map +0 -1
  201. package/dist/parser/generic.js +0 -46
  202. package/dist/parser/generic.js.map +0 -1
  203. package/dist/parser/objectLiteral.d.ts.map +0 -1
  204. package/dist/parser/objectLiteral.js +0 -38
  205. package/dist/parser/objectLiteral.js.map +0 -1
  206. package/dist/parser/semantic/bounds.d.ts +0 -13
  207. package/dist/parser/semantic/bounds.d.ts.map +0 -1
  208. package/dist/parser/semantic/intersections.d.ts.map +0 -1
  209. package/dist/parser/semantic/semantic.d.ts +0 -22
  210. package/dist/parser/semantic/semantic.d.ts.map +0 -1
  211. package/dist/parser/semantic/validate.d.ts.map +0 -1
  212. package/dist/parser/shared.d.ts.map +0 -1
  213. package/dist/parser/shared.js.map +0 -1
  214. package/dist/parser/string/reduce/dynamic.d.ts.map +0 -1
  215. package/dist/parser/string/reduce/dynamic.js.map +0 -1
  216. package/dist/parser/string/reduce/shared.js.map +0 -1
  217. package/dist/parser/string/reduce/static.d.ts.map +0 -1
  218. package/dist/parser/string/shift/operand/date.d.ts.map +0 -1
  219. package/dist/parser/string/shift/operand/enclosed.js.map +0 -1
  220. package/dist/parser/string/shift/operand/unenclosed.d.ts.map +0 -1
  221. package/dist/parser/string/shift/operand/unenclosed.js.map +0 -1
  222. package/dist/parser/string/shift/operator/bounds.d.ts.map +0 -1
  223. package/dist/parser/string/shift/operator/bounds.js +0 -95
  224. package/dist/parser/string/shift/operator/bounds.js.map +0 -1
  225. package/dist/parser/string/shift/operator/operator.d.ts.map +0 -1
  226. package/dist/parser/string/shift/scanner.js.map +0 -1
  227. package/dist/parser/string/string.d.ts.map +0 -1
  228. package/dist/parser/string/string.js +0 -31
  229. package/dist/parser/string/string.js.map +0 -1
  230. package/dist/parser/tuple.d.ts.map +0 -1
  231. package/dist/parser/tuple.js.map +0 -1
  232. package/dist/scope.d.ts.map +0 -1
  233. package/dist/scope.js.map +0 -1
  234. package/dist/scopes/ark.d.ts +0 -38
  235. package/dist/scopes/ark.d.ts.map +0 -1
  236. package/dist/scopes/ark.js +0 -24
  237. package/dist/scopes/ark.js.map +0 -1
  238. package/dist/scopes/jsObjects.d.ts +0 -19
  239. package/dist/scopes/jsObjects.d.ts.map +0 -1
  240. package/dist/scopes/jsObjects.js +0 -15
  241. package/dist/scopes/jsObjects.js.map +0 -1
  242. package/dist/scopes/parsing.d.ts +0 -18
  243. package/dist/scopes/parsing.d.ts.map +0 -1
  244. package/dist/scopes/parsing.js +0 -61
  245. package/dist/scopes/parsing.js.map +0 -1
  246. package/dist/scopes/tsGenerics.d.ts +0 -17
  247. package/dist/scopes/tsGenerics.d.ts.map +0 -1
  248. package/dist/scopes/tsGenerics.js +0 -13
  249. package/dist/scopes/tsGenerics.js.map +0 -1
  250. package/dist/scopes/tsKeywords.d.ts +0 -24
  251. package/dist/scopes/tsKeywords.d.ts.map +0 -1
  252. package/dist/scopes/tsKeywords.js +0 -20
  253. package/dist/scopes/tsKeywords.js.map +0 -1
  254. package/dist/scopes/utils/creditCard.d.ts +0 -3
  255. package/dist/scopes/utils/creditCard.d.ts.map +0 -1
  256. package/dist/scopes/utils/creditCard.js +0 -36
  257. package/dist/scopes/utils/creditCard.js.map +0 -1
  258. package/dist/scopes/utils/date.d.ts +0 -24
  259. package/dist/scopes/utils/date.d.ts.map +0 -1
  260. package/dist/scopes/utils/date.js +0 -53
  261. package/dist/scopes/utils/date.js.map +0 -1
  262. package/dist/scopes/validation.d.ts +0 -20
  263. package/dist/scopes/validation.d.ts.map +0 -1
  264. package/dist/scopes/validation.js +0 -57
  265. package/dist/scopes/validation.js.map +0 -1
  266. package/dist/type.d.ts.map +0 -1
  267. package/dist/type.js.map +0 -1
  268. package/scopes/ark.ts +0 -64
  269. package/scopes/jsObjects.ts +0 -29
  270. package/scopes/parsing.ts +0 -81
  271. package/scopes/tsGenerics.ts +0 -28
  272. package/scopes/tsKeywords.ts +0 -39
  273. package/scopes/utils/creditCard.ts +0 -37
  274. package/scopes/utils/date.ts +0 -108
  275. package/scopes/validation.ts +0 -81
  276. /package/{dist → out}/parser/semantic/bounds.js +0 -0
  277. /package/{dist → out}/parser/semantic/divisor.js +0 -0
  278. /package/{dist → out}/parser/semantic/divisor.js.map +0 -0
  279. /package/{dist → out}/parser/semantic/intersections.js +0 -0
  280. /package/{dist → out}/parser/semantic/intersections.js.map +0 -0
  281. /package/{dist → out}/parser/semantic/semantic.js +0 -0
  282. /package/{dist → out}/parser/semantic/semantic.js.map +0 -0
  283. /package/{dist → out}/parser/semantic/utils.js +0 -0
  284. /package/{dist → out}/parser/semantic/utils.js.map +0 -0
  285. /package/{dist → out}/parser/semantic/validate.js +0 -0
  286. /package/{dist → out}/parser/string/reduce/static.js +0 -0
  287. /package/{dist → out}/parser/string/reduce/static.js.map +0 -0
  288. /package/{dist → out}/parser/string/shift/operand/date.js +0 -0
  289. /package/{dist → out}/parser/string/shift/operand/operand.d.ts +0 -0
  290. /package/{dist → out}/parser/string/shift/operand/operand.js +0 -0
  291. /package/{dist → out}/parser/string/shift/operator/divisor.d.ts +0 -0
  292. /package/{dist → out}/parser/string/shift/operator/divisor.d.ts.map +0 -0
  293. /package/{dist → out}/parser/string/shift/operator/divisor.js +0 -0
  294. /package/{dist → out}/parser/string/shift/operator/operator.d.ts +0 -0
  295. /package/{dist → out}/parser/string/shift/operator/operator.js +0 -0
  296. /package/{dist → out}/parser/string/shift/scanner.js +0 -0
package/parser/tuple.ts CHANGED
@@ -1,18 +1,22 @@
1
1
  import {
2
- builtins,
3
- node,
4
- type BaseAttributes,
2
+ keywords,
3
+ schema,
4
+ type BaseMeta,
5
5
  type Morph,
6
6
  type Out,
7
7
  type Predicate,
8
- type Root,
8
+ type Schema,
9
+ type TypeNode,
10
+ type ValidatorKind,
11
+ type extractIn,
12
+ type extractOut,
9
13
  type inferMorphOut,
10
14
  type inferNarrow
11
15
  } from "@arktype/schema"
12
16
  import {
13
17
  isArray,
14
18
  objectKindOrDomainOf,
15
- stringify,
19
+ printable,
16
20
  throwParseError,
17
21
  type BuiltinObjectKind,
18
22
  type Constructor,
@@ -23,7 +27,6 @@ import {
23
27
  type isAny
24
28
  } from "@arktype/util"
25
29
  import type { ParseContext } from "../scope.js"
26
- import type { extractIn, extractOut } from "../type.js"
27
30
  import type { inferDefinition, validateDefinition } from "./definition.js"
28
31
  import type { inferIntersection } from "./semantic/intersections.js"
29
32
  import type { InfixOperator, PostfixExpression } from "./semantic/semantic.js"
@@ -39,7 +42,7 @@ import type { BaseCompletions } from "./string/string.js"
39
42
  export const parseTuple = (def: List, ctx: ParseContext) =>
40
43
  maybeParseTupleExpression(def, ctx) ?? parseTupleLiteral(def, ctx)
41
44
 
42
- export const parseTupleLiteral = (def: List, ctx: ParseContext): Root => {
45
+ export const parseTupleLiteral = (def: List, ctx: ParseContext): TypeNode => {
43
46
  const props: unknown[] = []
44
47
  let isVariadic = false
45
48
  for (let i = 0; i < def.length; i++) {
@@ -59,7 +62,7 @@ export const parseTupleLiteral = (def: List, ctx: ParseContext): Root => {
59
62
  const parsedEntry = parseEntry([`${i}`, elementDef])
60
63
  const value = ctx.scope.parse(parsedEntry.innerValue, ctx)
61
64
  if (isVariadic) {
62
- if (!value.extends(builtins.array)) {
65
+ if (!value.extends(keywords.Array)) {
63
66
  return throwParseError(writeNonArrayRestMessage(elementDef))
64
67
  }
65
68
  if (i !== def.length - 1) {
@@ -68,7 +71,7 @@ export const parseTupleLiteral = (def: List, ctx: ParseContext): Root => {
68
71
  // TODO: Fix builtins.arrayIndexTypeNode()
69
72
  const elementType = value.getPath()
70
73
  // TODO: first variadic i
71
- props.push({ key: builtins.number, value: elementType })
74
+ props.push({ key: keywords.number, value: elementType })
72
75
  } else {
73
76
  props.push({
74
77
  key: {
@@ -89,28 +92,28 @@ export const parseTupleLiteral = (def: List, ctx: ParseContext): Root => {
89
92
  optional: false
90
93
  },
91
94
  // , ctx
92
- value: node({ is: def.length })
95
+ value: schema({ unit: def.length })
93
96
  })
94
97
  }
95
98
  // props , ctx
96
- return node(Array)
99
+ return schema(Array)
97
100
  }
98
101
 
99
102
  export const maybeParseTupleExpression = (
100
103
  def: List,
101
104
  ctx: ParseContext
102
- ): Root | undefined => {
105
+ ): TypeNode | undefined => {
103
106
  const tupleExpressionResult = isIndexOneExpression(def)
104
107
  ? indexOneParsers[def[1]](def as never, ctx)
105
108
  : isIndexZeroExpression(def)
106
- ? prefixParsers[def[0]](def as never, ctx)
107
- : undefined
109
+ ? prefixParsers[def[0]](def as never, ctx)
110
+ : undefined
108
111
  if (tupleExpressionResult) {
109
112
  return tupleExpressionResult.isNever()
110
113
  ? throwParseError(
111
114
  writeUnsatisfiableExpressionError(
112
115
  def
113
- .map((def) => (typeof def === "string" ? def : stringify(def)))
116
+ .map((def) => (typeof def === "string" ? def : printable(def)))
114
117
  .join(" ")
115
118
  )
116
119
  )
@@ -130,21 +133,17 @@ export type validateTuple<
130
133
  > = def extends IndexZeroExpression
131
134
  ? validatePrefixExpression<def, $, args>
132
135
  : def extends PostfixExpression
133
- ? validatePostfixExpression<def, $, args>
134
- : def extends InfixExpression
135
- ? validateInfixExpression<def, $, args>
136
- : def extends
137
- | readonly ["", ...unknown[]]
138
- | readonly [unknown, "", ...unknown[]]
139
- ? readonly [
140
- def[0] extends ""
141
- ? BaseCompletions<$, args, IndexZeroOperator>
142
- : def[0],
143
- def[1] extends ""
144
- ? BaseCompletions<$, args, IndexOneOperator>
145
- : def[1]
146
- ]
147
- : validateTupleLiteral<def, $, args>
136
+ ? validatePostfixExpression<def, $, args>
137
+ : def extends InfixExpression
138
+ ? validateInfixExpression<def, $, args>
139
+ : def extends
140
+ | readonly ["", ...unknown[]]
141
+ | readonly [unknown, "", ...unknown[]]
142
+ ? readonly [
143
+ def[0] extends "" ? BaseCompletions<$, args, IndexZeroOperator> : def[0],
144
+ def[1] extends "" ? BaseCompletions<$, args, IndexOneOperator> : def[1]
145
+ ]
146
+ : validateTupleLiteral<def, $, args>
148
147
 
149
148
  export type validateTupleLiteral<
150
149
  def extends List,
@@ -187,10 +186,10 @@ type semanticallyValidateRestElement<operand, $, args> = inferDefinition<
187
186
  ? result extends never
188
187
  ? writeNonArrayRestMessage<operand>
189
188
  : isAny<result> extends true
190
- ? writeNonArrayRestMessage<operand>
191
- : result extends readonly unknown[]
192
- ? operand
193
- : writeNonArrayRestMessage<operand>
189
+ ? writeNonArrayRestMessage<operand>
190
+ : result extends readonly unknown[]
191
+ ? operand
192
+ : writeNonArrayRestMessage<operand>
194
193
  : never
195
194
 
196
195
  export const writeNonArrayRestMessage = <operand>(operand: operand) =>
@@ -260,28 +259,28 @@ export type inferTupleExpression<
260
259
  > = def[1] extends "[]"
261
260
  ? inferDefinition<def[0], $, args>[]
262
261
  : def[1] extends "&"
263
- ? inferIntersection<
264
- inferDefinition<def[0], $, args>,
265
- inferDefinition<def[2], $, args>
266
- >
267
- : def[1] extends "|"
268
- ? inferDefinition<def[0], $, args> | inferDefinition<def[2], $, args>
269
- : def[1] extends ":"
270
- ? inferNarrow<inferDefinition<def[0], $, args>, def[2]>
271
- : def[1] extends "=>"
272
- ? parseMorph<def[0], def[2], $, args>
273
- : def[1] extends "@"
274
- ? inferDefinition<def[0], $, args>
275
- : def extends readonly ["===", ...infer values]
276
- ? values[number]
277
- : def extends readonly [
278
- "instanceof",
279
- ...infer constructors extends Constructor[]
280
- ]
281
- ? InstanceType<constructors[number]>
282
- : def[0] extends "keyof"
283
- ? inferKeyOfExpression<def[1], $, args>
284
- : never
262
+ ? inferIntersection<
263
+ inferDefinition<def[0], $, args>,
264
+ inferDefinition<def[2], $, args>
265
+ >
266
+ : def[1] extends "|"
267
+ ? inferDefinition<def[0], $, args> | inferDefinition<def[2], $, args>
268
+ : def[1] extends ":"
269
+ ? inferNarrow<inferDefinition<def[0], $, args>, def[2]>
270
+ : def[1] extends "=>"
271
+ ? parseMorph<def[0], def[2], $, args>
272
+ : def[1] extends "@"
273
+ ? inferDefinition<def[0], $, args>
274
+ : def extends readonly ["===", ...infer values]
275
+ ? values[number]
276
+ : def extends readonly [
277
+ "instanceof",
278
+ ...infer constructors extends Constructor[]
279
+ ]
280
+ ? InstanceType<constructors[number]>
281
+ : def[0] extends "keyof"
282
+ ? inferKeyOfExpression<def[1], $, args>
283
+ : never
285
284
 
286
285
  export type validatePrefixExpression<
287
286
  def extends IndexZeroExpression,
@@ -290,12 +289,12 @@ export type validatePrefixExpression<
290
289
  > = def["length"] extends 1
291
290
  ? readonly [writeMissingRightOperandMessage<def[0]>]
292
291
  : def[0] extends "keyof"
293
- ? readonly [def[0], validateDefinition<def[1], $, args>]
294
- : def[0] extends "==="
295
- ? readonly [def[0], ...unknown[]]
296
- : def[0] extends "instanceof"
297
- ? readonly [def[0], ...Constructor[]]
298
- : never
292
+ ? readonly [def[0], validateDefinition<def[1], $, args>]
293
+ : def[0] extends "==="
294
+ ? readonly [def[0], ...unknown[]]
295
+ : def[0] extends "instanceof"
296
+ ? readonly [def[0], ...Constructor[]]
297
+ : never
299
298
 
300
299
  export type validatePostfixExpression<
301
300
  def extends PostfixExpression,
@@ -317,14 +316,14 @@ export type validateInfixExpression<
317
316
  def[1] extends "|"
318
317
  ? validateDefinition<def[2], $, args>
319
318
  : def[1] extends "&"
320
- ? validateDefinition<def[2], $, args>
321
- : def[1] extends ":"
322
- ? Predicate<extractIn<inferDefinition<def[0], $, args>>>
323
- : def[1] extends "=>"
324
- ? Morph<extractOut<inferDefinition<def[0], $, args>>, unknown>
325
- : def[1] extends "@"
326
- ? BaseAttributes | string
327
- : validateDefinition<def[2], $, args>
319
+ ? validateDefinition<def[2], $, args>
320
+ : def[1] extends ":"
321
+ ? Predicate<extractIn<inferDefinition<def[0], $, args>>>
322
+ : def[1] extends "=>"
323
+ ? Morph<extractOut<inferDefinition<def[0], $, args>>, unknown>
324
+ : def[1] extends "@"
325
+ ? BaseMeta | string
326
+ : validateDefinition<def[2], $, args>
328
327
  ]
329
328
 
330
329
  export type UnparsedTupleExpressionInput = {
@@ -356,12 +355,12 @@ const parseArrayTuple: PostfixParser<"[]"> = (def, ctx) =>
356
355
  export type PostfixParser<token extends IndexOneOperator> = (
357
356
  def: IndexOneExpression<token>,
358
357
  ctx: ParseContext
359
- ) => Root
358
+ ) => TypeNode
360
359
 
361
360
  export type PrefixParser<token extends IndexZeroOperator> = (
362
361
  def: IndexZeroExpression<token>,
363
362
  ctx: ParseContext
364
- ) => Root
363
+ ) => TypeNode
365
364
 
366
365
  export type TupleExpression = IndexZeroExpression | IndexOneExpression
367
366
 
@@ -386,19 +385,13 @@ export const parseMorphTuple: PostfixParser<"=>"> = (def, ctx) => {
386
385
  writeMalformedFunctionalExpressionMessage("=>", def[2])
387
386
  )
388
387
  }
389
- // TODO: fix
390
- return ctx.scope.parse(def[0], ctx) //.constrain("morph", def[2] as Morph)
388
+ // TODO: nested morphs?
389
+ return schema({
390
+ in: ctx.scope.parse(def[0], ctx) as Schema<ValidatorKind>,
391
+ morph: def[2] as Morph
392
+ })
391
393
  }
392
394
 
393
- export type parseMorph<inDef, morph, $, args> = morph extends Morph
394
- ? (
395
- // TODO: should this be extractOut
396
- In: extractIn<inferDefinition<inDef, $, args>>
397
- ) => Out<inferMorphOut<ReturnType<morph>>>
398
- : never
399
-
400
- export type MorphAst<i = any, o = any> = (In: i) => Out<o>
401
-
402
395
  export const writeMalformedFunctionalExpressionMessage = (
403
396
  operator: FunctionalTupleOperator,
404
397
  value: unknown
@@ -407,6 +400,12 @@ export const writeMalformedFunctionalExpressionMessage = (
407
400
  operator === ":" ? "Narrow" : "Morph"
408
401
  } expression requires a function following '${operator}' (was ${typeof value})`
409
402
 
403
+ export type parseMorph<inDef, morph, $, args> = morph extends Morph
404
+ ? (
405
+ In: extractIn<inferDefinition<inDef, $, args>>
406
+ ) => Out<inferMorphOut<ReturnType<morph>>>
407
+ : never
408
+
410
409
  export const parseNarrowTuple: PostfixParser<":"> = (def, ctx) => {
411
410
  if (typeof def[2] !== "function") {
412
411
  return throwParseError(
@@ -445,7 +444,7 @@ const prefixParsers: {
445
444
  [token in IndexZeroOperator]: PrefixParser<token>
446
445
  } = {
447
446
  keyof: parseKeyOfTuple,
448
- instanceof: (def, ctx) => {
447
+ instanceof: (def) => {
449
448
  if (typeof def[1] !== "function") {
450
449
  return throwParseError(
451
450
  writeInvalidConstructorMessage(objectKindOrDomainOf(def[1]))
@@ -455,14 +454,14 @@ const prefixParsers: {
455
454
  .slice(1)
456
455
  .map((ctor) =>
457
456
  typeof ctor === "function"
458
- ? { basis: ctor as Constructor }
457
+ ? { proto: ctor as Constructor }
459
458
  : throwParseError(
460
459
  writeInvalidConstructorMessage(objectKindOrDomainOf(ctor))
461
460
  )
462
461
  )
463
- return node(...branches)
462
+ return schema(...branches)
464
463
  },
465
- "===": (def) => node({ is: def.slice(1) })
464
+ "===": (def) => schema.units(...def.slice(1))
466
465
  }
467
466
 
468
467
  const isIndexZeroExpression = (def: List): def is IndexZeroExpression =>
@@ -472,4 +471,4 @@ export const writeInvalidConstructorMessage = <
472
471
  actual extends Domain | BuiltinObjectKind
473
472
  >(
474
473
  actual: actual
475
- ) => `Expected a constructor following 'instanceof' operator (was ${actual}).`
474
+ ) => `Expected a constructor following 'instanceof' operator (was ${actual})`
package/scope.ts CHANGED
@@ -1,21 +1,25 @@
1
- import { builtins, type ProblemCode, type Root } from "@arktype/schema"
1
+ import {
2
+ BaseType,
3
+ keywords,
4
+ type ArkErrorCode,
5
+ type KeyCheckKind,
6
+ type TypeNode,
7
+ type extractIn,
8
+ type extractOut
9
+ } from "@arktype/schema"
2
10
  import {
3
11
  domainOf,
4
12
  hasDomain,
5
13
  isThunk,
14
+ map,
6
15
  throwParseError,
7
- transform,
8
16
  type Dict,
9
17
  type evaluate,
10
18
  type isAny,
11
19
  type nominal
12
20
  } from "@arktype/util"
13
- import {
14
- createMatchParser,
15
- createWhenParser,
16
- type MatchParser,
17
- type WhenParser
18
- } from "./match.js"
21
+ import type { type } from "./ark.js"
22
+ import { createMatchParser, type MatchParser } from "./match.js"
19
23
  import {
20
24
  parseObject,
21
25
  writeBadDefinitionTypeMessage,
@@ -27,31 +31,26 @@ import {
27
31
  type GenericDeclaration,
28
32
  type GenericParamsParseError
29
33
  } from "./parser/generic.js"
34
+ import { DynamicState } from "./parser/string/reduce/dynamic.js"
30
35
  import {
31
36
  writeMissingSubmoduleAccessMessage,
32
37
  writeNonSubmoduleDotMessage,
33
38
  writeUnresolvableMessage
34
39
  } from "./parser/string/shift/operand/unenclosed.js"
35
- import { parseString } from "./parser/string/string.js"
36
- import type { type } from "./scopes/ark.js"
40
+ import { fullStringParse } from "./parser/string/string.js"
37
41
  import {
38
- addArkKind,
42
+ Type,
39
43
  createTypeParser,
40
44
  generic,
41
- hasArkKind,
42
- Type,
43
45
  validateUninstantiatedGeneric,
44
- type arkKind,
45
46
  type DeclarationParser,
46
47
  type DefinitionParser,
47
- type extractIn,
48
- type extractOut,
49
48
  type Generic,
50
49
  type GenericProps,
51
- type KeyCheckKind,
52
50
  type TypeConfig,
53
51
  type TypeParser
54
52
  } from "./type.js"
53
+ import { addArkKind, hasArkKind, type arkKind } from "./util.js"
55
54
 
56
55
  export type ScopeParser<parent, ambient> = {
57
56
  <const def>(def: validateScope<def, parent & ambient>): Scope<{
@@ -71,7 +70,7 @@ export type ScopeParser<parent, ambient> = {
71
70
 
72
71
  export type ScopeConfig = {
73
72
  ambient?: Scope | null
74
- codes?: Record<ProblemCode, { mustBe?: string }>
73
+ codes?: Record<ArkErrorCode, { mustBe?: string }>
75
74
  keys?: KeyCheckKind
76
75
  }
77
76
 
@@ -82,27 +81,22 @@ type validateScope<def, $> = {
82
81
  ? def[k]
83
82
  : validateDefinition<def[k], $ & bootstrap<def>, {}>
84
83
  : parseScopeKey<k>["params"] extends GenericParamsParseError
85
- ? // use the full nominal type here to avoid an overlap between the
86
- // error message and a possible value for the property
87
- parseScopeKey<k>["params"][0]
88
- : validateDefinition<
89
- def[k],
90
- $ & bootstrap<def>,
91
- {
92
- // once we support constraints on generic parameters, we'd use
93
- // the base type here: https://github.com/arktypeio/arktype/issues/796
94
- [param in parseScopeKey<k>["params"][number]]: unknown
95
- }
96
- >
84
+ ? // use the full nominal type here to avoid an overlap between the
85
+ // error message and a possible value for the property
86
+ parseScopeKey<k>["params"][0]
87
+ : validateDefinition<
88
+ def[k],
89
+ $ & bootstrap<def>,
90
+ {
91
+ // once we support constraints on generic parameters, we'd use
92
+ // the base type here: https://github.com/arktypeio/arktype/issues/796
93
+ [param in parseScopeKey<k>["params"][number]]: unknown
94
+ }
95
+ >
97
96
  }
98
97
 
99
98
  export type bindThis<def> = { this: Def<def> }
100
99
 
101
- export const bindThis = () => ({
102
- // TODO: fix
103
- this: builtins.unknown
104
- })
105
-
106
100
  /** nominal type for an unparsed definition used during scope bootstrapping */
107
101
  type Def<def = {}> = nominal<def, "unparsed">
108
102
 
@@ -132,8 +126,8 @@ type bootstrapAliases<def> = {
132
126
  >]: def[k] extends PreparsedResolution
133
127
  ? def[k]
134
128
  : def[k] extends (() => infer thunkReturn extends PreparsedResolution)
135
- ? thunkReturn
136
- : Def<def[k]>
129
+ ? thunkReturn
130
+ : Def<def[k]>
137
131
  } & {
138
132
  [k in keyof def & GenericDeclaration as extractGenericName<k>]: Generic<
139
133
  parseGenericParams<extractGenericParameters<k>>,
@@ -146,10 +140,10 @@ type inferBootstrapped<r extends Resolutions> = evaluate<{
146
140
  [name in keyof r["exports"]]: r["exports"][name] extends Def<infer def>
147
141
  ? inferDefinition<def, $<r>, {}>
148
142
  : r["exports"][name] extends GenericProps<infer params, infer def>
149
- ? // add the scope in which the generic was defined here
150
- Generic<params, def, $<r>>
151
- : // otherwise should be a submodule
152
- r["exports"][name]
143
+ ? // add the scope in which the generic was defined here
144
+ Generic<params, def, $<r>>
145
+ : // otherwise should be a submodule
146
+ r["exports"][name]
153
147
  }>
154
148
 
155
149
  type extractGenericName<k> = k extends GenericDeclaration<infer name>
@@ -175,10 +169,10 @@ export type resolve<reference extends keyof $ | keyof args, $, args> = (
175
169
  ? [resolution] extends [never]
176
170
  ? never
177
171
  : isAny<resolution> extends true
178
- ? any
179
- : resolution extends Def<infer def>
180
- ? inferDefinition<def, $, args>
181
- : resolution
172
+ ? any
173
+ : resolution extends Def<infer def>
174
+ ? inferDefinition<def, $, args>
175
+ : resolution
182
176
  : never
183
177
 
184
178
  export type moduleKeyOf<$> = {
@@ -204,10 +198,10 @@ export type Module<r extends Resolutions = any> = {
204
198
  ? [r["exports"][k]] extends [never]
205
199
  ? Type<never, $<r>>
206
200
  : isAny<r["exports"][k]> extends true
207
- ? Type<any, $<r>>
208
- : r["exports"][k] extends PreparsedResolution
209
- ? r["exports"][k]
210
- : Type<r["exports"][k], $<r>>
201
+ ? Type<any, $<r>>
202
+ : r["exports"][k] extends PreparsedResolution
203
+ ? r["exports"][k]
204
+ : Type<r["exports"][k], $<r>>
211
205
  : // set the nominal symbol's value to something validation won't care about
212
206
  // since the inferred type will be omitted anyways
213
207
  type.cast<"module">
@@ -223,10 +217,10 @@ export type ParseContext = {
223
217
  baseName: string
224
218
  path: string[]
225
219
  scope: Scope
226
- args: Record<string, Root> | undefined
220
+ args: Record<string, TypeNode> | undefined
227
221
  }
228
222
 
229
- type MergedResolutions = Record<string, Root | Generic>
223
+ type MergedResolutions = Record<string, TypeNode | Generic>
230
224
 
231
225
  type ParseContextInput = Pick<ParseContext, "baseName" | "args">
232
226
 
@@ -236,7 +230,7 @@ export class Scope<r extends Resolutions = any> {
236
230
 
237
231
  config: TypeConfig
238
232
 
239
- private parseCache: Record<string, Root> = {}
233
+ private parseCache: Record<string, TypeNode> = {}
240
234
  private resolutions: MergedResolutions
241
235
 
242
236
  /** The set of names defined at the root-level of the scope mapped to their
@@ -244,7 +238,6 @@ export class Scope<r extends Resolutions = any> {
244
238
  aliases: Record<string, unknown> = {}
245
239
  private exportedNames: exportedName<r>[] = []
246
240
  private ambient: Scope | null
247
- private references: Root[] = []
248
241
 
249
242
  constructor(def: Dict, config: ScopeConfig) {
250
243
  for (const k in def) {
@@ -275,8 +268,6 @@ export class Scope<r extends Resolutions = any> {
275
268
 
276
269
  match: MatchParser<$<r>> = createMatchParser(this as never) as never
277
270
 
278
- when: WhenParser<$<r>> = createWhenParser(this as never) as never
279
-
280
271
  // TODO: decide if this API will be used for non-validated types
281
272
  declare: DeclarationParser<$<r>> = () => ({ type: this.type }) as never
282
273
 
@@ -324,15 +315,15 @@ export class Scope<r extends Resolutions = any> {
324
315
  return this.parse(def, this.createRootContext(input))
325
316
  }
326
317
 
327
- parse(def: unknown, ctx: ParseContext): Root {
318
+ parse(def: unknown, ctx: ParseContext): TypeNode {
328
319
  if (typeof def === "string") {
329
320
  if (ctx.args !== undefined) {
330
321
  // we can only rely on the cache if there are no contextual
331
322
  // resolutions like "this" or generic args
332
- return parseString(def, ctx)
323
+ return this.parseString(def, ctx)
333
324
  }
334
325
  if (!this.parseCache[def]) {
335
- this.parseCache[def] = parseString(def, ctx)
326
+ this.parseCache[def] = this.parseString(def, ctx)
336
327
  }
337
328
  return this.parseCache[def]
338
329
  }
@@ -341,7 +332,16 @@ export class Scope<r extends Resolutions = any> {
341
332
  : throwParseError(writeBadDefinitionTypeMessage(domainOf(def)))
342
333
  }
343
334
 
344
- maybeResolve(name: string): Root | Generic | undefined {
335
+ parseString(def: string, ctx: ParseContext): TypeNode {
336
+ return (
337
+ this.maybeResolveNode(def) ??
338
+ ((def.endsWith("[]") &&
339
+ this.maybeResolveNode(def.slice(0, -2))?.array()) ||
340
+ fullStringParse(new DynamicState(def, ctx)))
341
+ )
342
+ }
343
+
344
+ maybeResolve(name: string): TypeNode | Generic | undefined {
345
345
  const cached = this.resolutions[name]
346
346
  if (cached) {
347
347
  return cached
@@ -357,11 +357,11 @@ export class Scope<r extends Resolutions = any> {
357
357
  const resolution = hasArkKind(def, "generic")
358
358
  ? validateUninstantiatedGeneric(def)
359
359
  : hasArkKind(def, "module")
360
- ? throwParseError(writeMissingSubmoduleAccessMessage(name))
361
- : this.parseDefinition(
362
- def,
363
- this.createRootContext({ baseName: name, args: {} })
364
- )
360
+ ? throwParseError(writeMissingSubmoduleAccessMessage(name))
361
+ : this.parseDefinition(
362
+ def,
363
+ this.createRootContext({ baseName: name, args: {} })
364
+ )
365
365
  this.resolutions[name] = resolution
366
366
  return resolution
367
367
  }
@@ -391,20 +391,20 @@ export class Scope<r extends Resolutions = any> {
391
391
  // might be something like a decimal literal, so just fall through to return
392
392
  }
393
393
 
394
- maybeResolveNode(name: string): Root | undefined {
394
+ maybeResolveNode(name: string): TypeNode | undefined {
395
395
  const result = this.maybeResolve(name)
396
- return hasArkKind(result, "node") ? (result as never) : undefined
396
+ return result instanceof BaseType ? (result as never) : undefined
397
397
  }
398
398
 
399
399
  import<names extends exportedName<r>[]>(
400
400
  ...names: names
401
401
  ): destructuredImportContext<
402
402
  r,
403
- names extends [] ? keyof r["exports"] : names[number]
403
+ names extends [] ? keyof r["exports"] & string : names[number]
404
404
  > {
405
405
  return addArkKind(
406
- transform(this.export(...names), ([alias, value]) => [
407
- `#${alias as string}`,
406
+ map(this.export(...names) as Dict, (alias, value) => [
407
+ `#${alias}`,
408
408
  value
409
409
  ]) as never,
410
410
  "module"
@@ -414,7 +414,7 @@ export class Scope<r extends Resolutions = any> {
414
414
  compile() {
415
415
  return ""
416
416
  // this.export()
417
- // const references: Set<TypeNode> = new Set()
417
+ // const references: Set<Root> = new Set()
418
418
  // for (const k in this.exportedResolutions!) {
419
419
  // const resolution = this.exportedResolutions[k]
420
420
  // if (hasArkKind(resolution, "node") && !references.has(resolution)) {
@@ -433,6 +433,11 @@ export class Scope<r extends Resolutions = any> {
433
433
  // .join("\n")
434
434
  }
435
435
 
436
+ bindThis() {
437
+ // TODO: fix
438
+ return { this: keywords.unknown }
439
+ }
440
+
436
441
  private exportedResolutions: MergedResolutions | undefined
437
442
  private exportCache: ExportCache | undefined
438
443
  export<names extends exportedName<r>[]>(
@@ -465,10 +470,7 @@ export class Scope<r extends Resolutions = any> {
465
470
  }
466
471
  const namesToExport = names.length ? names : this.exportedNames
467
472
  return addArkKind(
468
- transform(namesToExport, ([, name]) => [
469
- name,
470
- this.exportCache![name]
471
- ]) as never,
473
+ map(namesToExport, (_, name) => [name, this.exportCache![name]]) as never,
472
474
  "module"
473
475
  ) as never
474
476
  }
@@ -482,16 +484,15 @@ const resolutionsOfModule = (typeSet: ExportCache) => {
482
484
  const v = typeSet[k]
483
485
  if (hasArkKind(v, "module")) {
484
486
  const innerResolutions = resolutionsOfModule(v as never)
485
- const prefixedResolutions = transform(
486
- innerResolutions,
487
- ([innerK, innerV]) => [`${k}.${innerK}`, innerV]
488
- )
487
+ const prefixedResolutions = map(innerResolutions, (innerK, innerV) => [
488
+ `${k}.${innerK}`,
489
+ innerV
490
+ ])
489
491
  Object.assign(result, prefixedResolutions)
490
492
  } else if (hasArkKind(v, "generic")) {
491
493
  result[k] = v
492
494
  } else {
493
- // TODO: needed?
494
- result[k] = v.root as never
495
+ result[k] = (v as Type).root
495
496
  }
496
497
  }
497
498
  return result