arktype 2.0.0-dev.1 → 2.0.0-dev.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (219) hide show
  1. package/__tests__/array.test.ts +270 -151
  2. package/__tests__/badDefinitionType.test.ts +10 -3
  3. package/__tests__/basis.test.ts +6 -2
  4. package/__tests__/bounds.test.ts +282 -352
  5. package/__tests__/cast.test.ts +7 -2
  6. package/__tests__/completions.test.ts +8 -3
  7. package/__tests__/config.test.ts +95 -167
  8. package/__tests__/dateLiteral.test.ts +8 -4
  9. package/__tests__/declared.test.ts +7 -6
  10. package/__tests__/discrimination.test.ts +1 -1
  11. package/__tests__/divisor.test.ts +46 -22
  12. package/__tests__/enclosed.test.ts +39 -25
  13. package/__tests__/examples.test.ts +56 -56
  14. package/__tests__/expressions.test.ts +184 -112
  15. package/__tests__/generic.test.ts +307 -288
  16. package/__tests__/group.test.ts +67 -54
  17. package/__tests__/imports.test.ts +62 -49
  18. package/__tests__/instanceof.test.ts +25 -23
  19. package/__tests__/intersection.test.ts +52 -48
  20. package/__tests__/keyTraversal.test.ts +77 -73
  21. package/__tests__/keyof.test.ts +68 -52
  22. package/__tests__/keywords.test.ts +164 -138
  23. package/__tests__/literal.test.ts +60 -21
  24. package/__tests__/match.bench.ts +31 -0
  25. package/__tests__/match.test.ts +222 -22
  26. package/__tests__/narrow.test.ts +52 -54
  27. package/__tests__/objectLiteral.test.ts +347 -205
  28. package/__tests__/operator.bench.ts +4 -4
  29. package/__tests__/pipe.test.ts +501 -0
  30. package/__tests__/realWorld.test.ts +63 -0
  31. package/__tests__/regex.test.ts +17 -7
  32. package/__tests__/scope.bench.ts +1 -1
  33. package/__tests__/scope.test.ts +257 -23
  34. package/__tests__/serialization.test.ts +19 -0
  35. package/__tests__/string.test.ts +11 -7
  36. package/__tests__/submodule.test.ts +26 -39
  37. package/__tests__/this.test.ts +83 -57
  38. package/__tests__/thunk.test.ts +13 -7
  39. package/__tests__/traverse.test.ts +76 -54
  40. package/__tests__/tsGenerics.test.ts +12 -12
  41. package/__tests__/type.test.ts +36 -0
  42. package/__tests__/typeReference.test.ts +7 -7
  43. package/__tests__/unenclosed.test.ts +116 -100
  44. package/__tests__/union.test.ts +171 -104
  45. package/ark.ts +21 -65
  46. package/generic.ts +69 -0
  47. package/main.ts +6 -18
  48. package/match.ts +195 -93
  49. package/module.ts +15 -0
  50. package/out/ark.d.ts +15 -39
  51. package/out/ark.js +9 -18
  52. package/out/ark.js.map +1 -1
  53. package/out/generic.d.ts +21 -0
  54. package/out/generic.js +24 -0
  55. package/out/generic.js.map +1 -0
  56. package/out/main.d.ts +6 -7
  57. package/out/main.js +4 -3
  58. package/out/main.js.map +1 -1
  59. package/out/match.d.ts +57 -34
  60. package/out/match.js +51 -17
  61. package/out/match.js.map +1 -1
  62. package/out/module.d.ts +10 -0
  63. package/out/module.js +3 -0
  64. package/out/module.js.map +1 -0
  65. package/out/parser/definition.d.ts +11 -13
  66. package/out/parser/definition.js +10 -13
  67. package/out/parser/definition.js.map +1 -1
  68. package/out/parser/generic.d.ts +5 -7
  69. package/out/parser/generic.js +7 -11
  70. package/out/parser/generic.js.map +1 -1
  71. package/out/parser/objectLiteral.d.ts +45 -14
  72. package/out/parser/objectLiteral.js +61 -52
  73. package/out/parser/objectLiteral.js.map +1 -1
  74. package/out/parser/semantic/bounds.d.ts +10 -8
  75. package/out/parser/semantic/bounds.js +0 -1
  76. package/out/parser/semantic/bounds.js.map +1 -1
  77. package/out/parser/semantic/divisor.d.ts +5 -7
  78. package/out/parser/semantic/divisor.js +0 -1
  79. package/out/parser/semantic/infer.d.ts +24 -0
  80. package/out/parser/semantic/infer.js +1 -0
  81. package/out/parser/semantic/infer.js.map +1 -0
  82. package/out/parser/semantic/utils.d.ts +3 -7
  83. package/out/parser/semantic/utils.js +0 -1
  84. package/out/parser/semantic/validate.d.ts +15 -14
  85. package/out/parser/semantic/validate.js +1 -1
  86. package/out/parser/semantic/validate.js.map +1 -1
  87. package/out/parser/string/reduce/dynamic.d.ts +13 -13
  88. package/out/parser/string/reduce/dynamic.js +31 -26
  89. package/out/parser/string/reduce/dynamic.js.map +1 -1
  90. package/out/parser/string/reduce/shared.d.ts +6 -20
  91. package/out/parser/string/reduce/shared.js +0 -1
  92. package/out/parser/string/reduce/shared.js.map +1 -1
  93. package/out/parser/string/reduce/static.d.ts +27 -24
  94. package/out/parser/string/reduce/static.js +0 -1
  95. package/out/parser/string/shift/operand/date.d.ts +3 -3
  96. package/out/parser/string/shift/operand/date.js +4 -7
  97. package/out/parser/string/shift/operand/date.js.map +1 -1
  98. package/out/parser/string/shift/operand/enclosed.d.ts +4 -19
  99. package/out/parser/string/shift/operand/enclosed.js +12 -13
  100. package/out/parser/string/shift/operand/enclosed.js.map +1 -1
  101. package/out/parser/string/shift/operand/genericArgs.d.ts +9 -10
  102. package/out/parser/string/shift/operand/genericArgs.js +6 -10
  103. package/out/parser/string/shift/operand/genericArgs.js.map +1 -1
  104. package/out/parser/string/shift/operand/operand.d.ts +3 -4
  105. package/out/parser/string/shift/operand/operand.js +8 -12
  106. package/out/parser/string/shift/operand/operand.js.map +1 -1
  107. package/out/parser/string/shift/operand/unenclosed.d.ts +15 -22
  108. package/out/parser/string/shift/operand/unenclosed.js +24 -36
  109. package/out/parser/string/shift/operand/unenclosed.js.map +1 -1
  110. package/out/parser/string/shift/operator/bounds.d.ts +9 -13
  111. package/out/parser/string/shift/operator/bounds.js +43 -54
  112. package/out/parser/string/shift/operator/bounds.js.map +1 -1
  113. package/out/parser/string/shift/operator/divisor.d.ts +2 -4
  114. package/out/parser/string/shift/operator/divisor.js +1 -3
  115. package/out/parser/string/shift/operator/divisor.js.map +1 -1
  116. package/out/parser/string/shift/operator/operator.d.ts +2 -3
  117. package/out/parser/string/shift/operator/operator.js +12 -19
  118. package/out/parser/string/shift/operator/operator.js.map +1 -1
  119. package/out/parser/string/shift/scanner.d.ts +2 -2
  120. package/out/parser/string/shift/scanner.js +14 -16
  121. package/out/parser/string/shift/scanner.js.map +1 -1
  122. package/out/parser/string/string.d.ts +8 -6
  123. package/out/parser/string/string.js +1 -3
  124. package/out/parser/string/string.js.map +1 -1
  125. package/out/parser/tuple.d.ts +102 -43
  126. package/out/parser/tuple.js +119 -87
  127. package/out/parser/tuple.js.map +1 -1
  128. package/out/scope.d.ts +43 -120
  129. package/out/scope.js +39 -209
  130. package/out/scope.js.map +1 -1
  131. package/out/type.d.ts +43 -64
  132. package/out/type.js +30 -131
  133. package/out/type.js.map +1 -1
  134. package/package.json +7 -3
  135. package/parser/definition.ts +77 -93
  136. package/parser/generic.ts +38 -40
  137. package/parser/objectLiteral.ts +172 -136
  138. package/parser/semantic/bounds.ts +33 -32
  139. package/parser/semantic/divisor.ts +8 -10
  140. package/parser/semantic/infer.ts +141 -0
  141. package/parser/semantic/utils.ts +12 -23
  142. package/parser/semantic/validate.ts +81 -73
  143. package/parser/string/reduce/dynamic.ts +67 -55
  144. package/parser/string/reduce/shared.ts +1 -3
  145. package/parser/string/reduce/static.ts +67 -69
  146. package/parser/string/shift/operand/date.ts +15 -14
  147. package/parser/string/shift/operand/enclosed.ts +30 -25
  148. package/parser/string/shift/operand/genericArgs.ts +46 -45
  149. package/parser/string/shift/operand/operand.ts +32 -38
  150. package/parser/string/shift/operand/unenclosed.ts +107 -158
  151. package/parser/string/shift/operator/bounds.ts +118 -112
  152. package/parser/string/shift/operator/divisor.ts +14 -17
  153. package/parser/string/shift/operator/operator.ts +35 -46
  154. package/parser/string/shift/scanner.ts +51 -50
  155. package/parser/string/string.ts +30 -35
  156. package/parser/tuple.ts +375 -307
  157. package/scope.ts +170 -453
  158. package/type.ts +193 -274
  159. package/__tests__/astUtils.test.ts +0 -17
  160. package/__tests__/cyclic.test.ts +0 -144
  161. package/__tests__/jsdoc.test.ts +0 -64
  162. package/__tests__/morph.test.ts +0 -409
  163. package/__tests__/nodeDefinitions.test.ts +0 -190
  164. package/__tests__/typeMethods.test.ts +0 -69
  165. package/out/ark.d.ts.map +0 -1
  166. package/out/arkConfig.d.ts +0 -2
  167. package/out/arkConfig.d.ts.map +0 -1
  168. package/out/arkConfig.js +0 -10
  169. package/out/arkConfig.js.map +0 -1
  170. package/out/main.d.ts.map +0 -1
  171. package/out/match.d.ts.map +0 -1
  172. package/out/parser/definition.d.ts.map +0 -1
  173. package/out/parser/generic.d.ts.map +0 -1
  174. package/out/parser/objectLiteral.d.ts.map +0 -1
  175. package/out/parser/semantic/bounds.d.ts.map +0 -1
  176. package/out/parser/semantic/divisor.d.ts.map +0 -1
  177. package/out/parser/semantic/intersections.d.ts +0 -14
  178. package/out/parser/semantic/intersections.d.ts.map +0 -1
  179. package/out/parser/semantic/intersections.js +0 -2
  180. package/out/parser/semantic/intersections.js.map +0 -1
  181. package/out/parser/semantic/semantic.d.ts +0 -33
  182. package/out/parser/semantic/semantic.d.ts.map +0 -1
  183. package/out/parser/semantic/semantic.js +0 -2
  184. package/out/parser/semantic/semantic.js.map +0 -1
  185. package/out/parser/semantic/utils.d.ts.map +0 -1
  186. package/out/parser/semantic/validate.d.ts.map +0 -1
  187. package/out/parser/shared.d.ts +0 -54
  188. package/out/parser/shared.d.ts.map +0 -1
  189. package/out/parser/shared.js +0 -47
  190. package/out/parser/shared.js.map +0 -1
  191. package/out/parser/string/reduce/dynamic.d.ts.map +0 -1
  192. package/out/parser/string/reduce/shared.d.ts.map +0 -1
  193. package/out/parser/string/reduce/static.d.ts.map +0 -1
  194. package/out/parser/string/shift/operand/date.d.ts.map +0 -1
  195. package/out/parser/string/shift/operand/enclosed.d.ts.map +0 -1
  196. package/out/parser/string/shift/operand/genericArgs.d.ts.map +0 -1
  197. package/out/parser/string/shift/operand/operand.d.ts.map +0 -1
  198. package/out/parser/string/shift/operand/unenclosed.d.ts.map +0 -1
  199. package/out/parser/string/shift/operator/bounds.d.ts.map +0 -1
  200. package/out/parser/string/shift/operator/divisor.d.ts.map +0 -1
  201. package/out/parser/string/shift/operator/operator.d.ts.map +0 -1
  202. package/out/parser/string/shift/scanner.d.ts.map +0 -1
  203. package/out/parser/string/string.d.ts.map +0 -1
  204. package/out/parser/tuple.d.ts.map +0 -1
  205. package/out/scope.d.ts.map +0 -1
  206. package/out/test.d.ts +0 -2
  207. package/out/test.d.ts.map +0 -1
  208. package/out/test.js +0 -7
  209. package/out/test.js.map +0 -1
  210. package/out/tsconfig.build.tsbuildinfo +0 -1
  211. package/out/type.d.ts.map +0 -1
  212. package/out/util.d.ts +0 -19
  213. package/out/util.d.ts.map +0 -1
  214. package/out/util.js +0 -7
  215. package/out/util.js.map +0 -1
  216. package/parser/semantic/intersections.ts +0 -46
  217. package/parser/semantic/semantic.ts +0 -144
  218. package/parser/shared.ts +0 -152
  219. package/util.ts +0 -36
@@ -1,10 +1,12 @@
1
- import { attest } from "@arktype/attest"
2
- import { scope, type } from "arktype"
3
- import { writeUnresolvableMessage } from "../parser/string/shift/operand/unenclosed.js"
4
- import { writeUnboundableMessage } from "../parser/string/shift/operator/bounds.js"
1
+ import { attest, contextualize } from "@arktype/attest"
2
+ import {
3
+ writeUnboundableMessage,
4
+ writeUnresolvableMessage
5
+ } from "@arktype/schema"
6
+ import { define, scope, type } from "arktype"
5
7
  import { writeUnexpectedCharacterMessage } from "../parser/string/shift/operator/operator.js"
6
8
 
7
- describe("scope", () => {
9
+ contextualize(() => {
8
10
  it("base definition", () => {
9
11
  const types = scope({ a: "string" }).export()
10
12
  attest<string>(types.a.infer)
@@ -13,6 +15,7 @@ describe("scope", () => {
13
15
  scope({ a: "strong" }).export()
14
16
  ).throwsAndHasTypeError(writeUnresolvableMessage("strong"))
15
17
  })
18
+
16
19
  it("type definition", () => {
17
20
  const types = scope({ a: type("string") }).export()
18
21
  attest<string>(types.a.infer)
@@ -21,6 +24,7 @@ describe("scope", () => {
21
24
  scope({ a: type("strong") })
22
25
  ).throwsAndHasTypeError(writeUnresolvableMessage("strong"))
23
26
  })
27
+
24
28
  it("interdependent", () => {
25
29
  const types = scope({
26
30
  a: "string>5",
@@ -29,6 +33,7 @@ describe("scope", () => {
29
33
  }).export()
30
34
  attest<string>(types.c.infer)
31
35
  })
36
+
32
37
  it("object array", () => {
33
38
  const types = scope({ a: "string", b: [{ c: "a" }] }).export()
34
39
  attest<
@@ -39,28 +44,22 @@ describe("scope", () => {
39
44
  ]
40
45
  >(types.b.infer)
41
46
  })
47
+
42
48
  it("doesn't try to validate any in scope", () => {
43
- // const $ = scope({ a: {} as any })
44
- // attest<{ a: never }>($.infer)
45
- // attest<[number, never]>($.type(["number", "a"]).infer)
49
+ const $ = scope({ a: {} as any })
50
+ attest<{ a: never }>($.infer)
51
+ attest<[number, never]>($.type(["number", "a"]).infer)
46
52
  })
53
+
47
54
  it("infers input and output", () => {
48
55
  const $ = scope({
49
- a: ["string", "=>", (s) => s.length]
56
+ a: ["string", "=>", s => s.length]
50
57
  })
51
58
  attest<{ a: number }>($.infer)
52
- // TODO: API?
53
- // attest<{ a: string }>($.in.infer)
54
- })
55
- it("scope.scope", () => {
56
- const $ = scope({
57
- a: "string"
58
- })
59
- const importer = $.scope({ b: "a[]" })
60
- attest<{ b: string[] }>(importer.infer)
61
- const t = importer.type("b")
62
- attest(t.json).is(type("string[]").json)
59
+
60
+ attest<{ a: string }>($.inferIn)
63
61
  })
62
+
64
63
  it("infers its own helpers", () => {
65
64
  const $ = scope({
66
65
  a: () => $.type("string"),
@@ -70,6 +69,7 @@ describe("scope", () => {
70
69
  attest<string>(types.a.infer)
71
70
  attest<number>(types.b.infer)
72
71
  })
72
+
73
73
  it("allows semantically valid helpers", () => {
74
74
  const $ = scope({
75
75
  n: () => $.type("number"),
@@ -79,6 +79,7 @@ describe("scope", () => {
79
79
  attest<number>(types.n.infer)
80
80
  attest<number>(types.lessThan10.infer)
81
81
  })
82
+
82
83
  it("errors on helper parse error", () => {
83
84
  attest(() => {
84
85
  const $ = scope({
@@ -88,6 +89,7 @@ describe("scope", () => {
88
89
  $.export()
89
90
  }).throwsAndHasTypeError(writeUnresolvableMessage("kung"))
90
91
  })
92
+
91
93
  it("errors on semantically invalid helper", () => {
92
94
  attest(() => {
93
95
  const $ = scope({
@@ -96,8 +98,11 @@ describe("scope", () => {
96
98
  lessThan10: () => $.type("b<10")
97
99
  })
98
100
  $.export()
99
- }).throwsAndHasTypeError(writeUnboundableMessage("'b'"))
101
+ })
102
+ .throws(writeUnboundableMessage("boolean"))
103
+ .type.errors(writeUnboundableMessage("'b'"))
100
104
  })
105
+
101
106
  it("errors on ridiculous unexpected alias scenario", () => {
102
107
  attest(() =>
103
108
  scope({
@@ -111,6 +116,7 @@ describe("scope", () => {
111
116
  }).export()
112
117
  ).throwsAndHasTypeError(writeUnexpectedCharacterMessage("c"))
113
118
  })
119
+
114
120
  it("autocompletion", () => {
115
121
  attest(() => {
116
122
  scope({
@@ -120,6 +126,7 @@ describe("scope", () => {
120
126
  }).export()
121
127
  }).type.errors(`Type '"fo"' is not assignable to type '"foobar"'`)
122
128
  })
129
+
123
130
  it("cross-scope reference", () => {
124
131
  const { Apple } = scope({
125
132
  Apple: {
@@ -134,7 +141,234 @@ describe("scope", () => {
134
141
  X: Apple
135
142
  }).export()
136
143
 
137
- const { out: data } = X({ pear: { tasty: true } })
138
- attest(data).snap({ pear: { tasty: true } })
144
+ const out = X({ pear: { tasty: true } })
145
+ attest(out).snap({ pear: { tasty: true } })
146
+ })
147
+ describe("define", () => {
148
+ it("ark", () => {
149
+ const def = define({
150
+ a: "string|number",
151
+ b: ["boolean"],
152
+ c: "this"
153
+ })
154
+ attest<{ a: "string|number"; b: ["boolean"]; c: "this" }>(def)
155
+ })
156
+
157
+ it("ark error", () => {
158
+ // currently is a no-op, so only has type error
159
+ // @ts-expect-error
160
+ attest(define({ a: "boolean|foo" })).type.errors(
161
+ writeUnresolvableMessage("foo")
162
+ )
163
+ })
164
+
165
+ it("custom scope", () => {
166
+ const $ = scope({
167
+ a: "string[]"
168
+ })
169
+ const ok = $.define(["a[]|boolean"])
170
+ attest<["a[]|boolean"]>(ok)
171
+ // @ts-expect-error
172
+ attest($.define({ not: "ok" })).type.errors(
173
+ writeUnresolvableMessage("ok")
174
+ )
175
+ })
176
+ })
177
+ describe("cyclic", () => {
178
+ it("base", () => {
179
+ const types = scope({ a: { b: "b" }, b: { a: "a" } }).export()
180
+
181
+ const a = {} as { b: typeof b }
182
+ const b = { a }
183
+ a.b = b
184
+
185
+ attest(types.a(a)).equals(a)
186
+ attest(types.a({ b: { a: { b: { a: 5 } } } }).toString()).snap(
187
+ "b.a.b.a must be an object (was number)"
188
+ )
189
+
190
+ // Type hint displays as "..." on hitting cycle (or any if "noErrorTruncation" is true)
191
+ attest({} as typeof types.a.infer).type.toString.snap()
192
+ attest({} as typeof types.b.infer.a.b.a.b.a.b.a).type.toString.snap()
193
+
194
+ // @ts-expect-error
195
+ attest({} as typeof types.a.infer.b.a.b.c).type.errors.snap(
196
+ `Property 'c' does not exist on type '{ a: { b: ...; }; }'.`
197
+ )
198
+ })
199
+
200
+ const getCyclicScope = () =>
201
+ scope({
202
+ package: {
203
+ name: "string",
204
+ "dependencies?": "package[]",
205
+ "contributors?": "contributor[]"
206
+ },
207
+ contributor: {
208
+ email: "email",
209
+ "packages?": "package[]"
210
+ }
211
+ })
212
+
213
+ type Package = ReturnType<typeof getCyclicScope>["infer"]["package"]
214
+
215
+ const getCyclicData = () => {
216
+ const packageData = {
217
+ name: "arktype",
218
+ dependencies: [{ name: "typescript" }],
219
+ contributors: [{ email: "david@arktype.io" }]
220
+ } satisfies Package
221
+ packageData.dependencies.push(packageData)
222
+ return packageData
223
+ }
224
+
225
+ it("cyclic union", () => {
226
+ const $ = scope({
227
+ a: { b: "b|false" },
228
+ b: { a: "a|true" }
229
+ })
230
+ attest($.infer).type.toString.snap(
231
+ "{ a: { b: false | { a: true | any; }; }; b: { a: true | { b: false | any; }; }; }"
232
+ )
233
+ })
234
+
235
+ it("cyclic intersection", () => {
236
+ const $ = scope({
237
+ a: { b: "b&a" },
238
+ b: { a: "a&b" }
239
+ })
240
+ attest($.infer).type.toString.snap(
241
+ "{ a: { b: { a: { b: any; a: any; }; b: any; }; }; b: { a: { b: { a: any; b: any; }; a: any; }; }; }"
242
+ )
243
+ })
244
+
245
+ it("allows valid", () => {
246
+ const types = getCyclicScope().export()
247
+ const data = getCyclicData()
248
+ attest(types.package(data)).snap({
249
+ name: "arktype",
250
+ dependencies: [{ name: "typescript" }, "(cycle)" as never],
251
+ contributors: [{ email: "david@arktype.io" }]
252
+ })
253
+ })
254
+
255
+ it("adds errors on invalid", () => {
256
+ const types = getCyclicScope().export()
257
+ const data = getCyclicData()
258
+ data.contributors[0].email = "ssalbdivad"
259
+ attest(types.package(data).toString()).snap(
260
+ 'contributors[0].email must be a valid email (was "ssalbdivad")'
261
+ )
262
+ })
263
+
264
+ it("can include cyclic data in message", () => {
265
+ const data = getCyclicData()
266
+ const nonSelfDependent = getCyclicScope().type([
267
+ "package",
268
+ ":",
269
+ p => !p.dependencies?.some(d => d.name === p.name)
270
+ ])
271
+ attest(nonSelfDependent(data).toString()).snap(
272
+ 'must be valid according to an anonymous predicate (was {"name":"arktype","dependencies":[{"name":"typescript"},"(cycle)"],"contributors":[{"email":"david@arktype.io"}]})'
273
+ )
274
+ })
275
+
276
+ it("union cyclic reference", () => {
277
+ const types = scope({
278
+ a: {
279
+ b: "b"
280
+ },
281
+ b: {
282
+ a: "a|3"
283
+ }
284
+ }).export()
285
+ attest(types.a.infer).type.toString.snap("{ b: { a: 3 | any; }")
286
+
287
+ attest(types.a.json).snap({
288
+ domain: "object",
289
+ prop: [
290
+ {
291
+ key: "b",
292
+ value: {
293
+ domain: "object",
294
+ prop: [{ key: "a", value: ["$a", { unit: 3 }] }]
295
+ }
296
+ }
297
+ ]
298
+ })
299
+
300
+ const valid: typeof types.a.infer = { b: { a: 3 } }
301
+
302
+ attest(types.a(valid)).equals(valid)
303
+
304
+ valid.b.a = valid
305
+
306
+ // check cyclic
307
+ attest(types.a(valid)).equals(valid)
308
+
309
+ attest(types.a({ b: { a: { b: { a: 4 } } } }).toString()).snap(
310
+ 'b.a.b.a must be an object or 3 (was number, 4) or b.a must be 3 (was {"b":{"a":4}})'
311
+ )
312
+
313
+ attest(types.b.infer).type.toString.snap("{ a: 3 | { b: any; }; }")
314
+ attest(types.b.json).snap({
315
+ domain: "object",
316
+ prop: [{ key: "a", value: ["$a", { unit: 3 }] }]
317
+ })
318
+ })
319
+
320
+ it("intersect cyclic reference", () => {
321
+ const types = scope({
322
+ a: {
323
+ b: "b"
324
+ },
325
+ b: {
326
+ c: "a&b"
327
+ }
328
+ }).export()
329
+ attest(types.a.infer).type.toString.snap()
330
+ attest(types.b.infer).type.toString.snap()
331
+
332
+ const expectedCyclicJson =
333
+ types.a.raw.firstReferenceOfKindOrThrow("alias").json
334
+
335
+ attest(types.a.json).snap({
336
+ domain: "object",
337
+ prop: [
338
+ {
339
+ key: "b",
340
+ value: {
341
+ domain: "object",
342
+ prop: [
343
+ {
344
+ key: "c",
345
+ value: expectedCyclicJson
346
+ }
347
+ ]
348
+ }
349
+ }
350
+ ]
351
+ })
352
+ const a = {} as typeof types.a.infer
353
+ const b = { c: {} } as typeof types.b.infer
354
+ a.b = b
355
+ b.c.b = b
356
+ b.c.c = b.c
357
+
358
+ attest(types.a(a)).equals(a)
359
+ attest(types.a({ b: { c: {} } }).toString())
360
+ .snap(`b.c.b must be { c: a&b } (was missing)
361
+ b.c.c must be a&b (was missing)`)
362
+
363
+ attest(types.b.json).snap({
364
+ domain: "object",
365
+ prop: [
366
+ {
367
+ key: "c",
368
+ value: expectedCyclicJson
369
+ }
370
+ ]
371
+ })
372
+ })
139
373
  })
140
374
  })
@@ -0,0 +1,19 @@
1
+ import { attest, contextualize } from "@arktype/attest"
2
+ import { schema } from "@arktype/schema"
3
+ import { type } from "arktype"
4
+
5
+ contextualize(() => {
6
+ it("builtin prototypes", () => {
7
+ const a = type({
8
+ age: "number"
9
+ })
10
+
11
+ const b = type({
12
+ ages: a.array()
13
+ })
14
+
15
+ const c = schema(b.json as never)
16
+
17
+ attest(b.json).equals(c.json)
18
+ })
19
+ })
@@ -1,22 +1,22 @@
1
- import { attest } from "@arktype/attest"
1
+ import { attest, contextualize } from "@arktype/attest"
2
+ import { writeUnresolvableMessage } from "@arktype/schema"
2
3
  import { type } from "arktype"
3
4
  import { writeUnterminatedEnclosedMessage } from "../parser/string/shift/operand/enclosed.js"
4
- import {
5
- writeExpressionExpectedMessage,
6
- writeUnresolvableMessage
7
- } from "../parser/string/shift/operand/unenclosed.js"
5
+ import { writeExpressionExpectedMessage } from "../parser/string/shift/operand/unenclosed.js"
8
6
 
9
- describe("string", () => {
7
+ contextualize(() => {
10
8
  it("errors on empty string", () => {
11
9
  // @ts-expect-error
12
10
  attest(() => type("")).throws(writeExpressionExpectedMessage(""))
13
11
  })
12
+
14
13
  it("ignores whitespace between identifiers/operators", () => {
15
14
  const t = type(` \n string |
16
- boolean [] `)
15
+ \tboolean [] `)
17
16
  attest<string | boolean[]>(t.infer)
18
17
  attest(t.json).equals(type("string|boolean[]").json)
19
18
  })
19
+
20
20
  it("errors on bad whitespace", () => {
21
21
  attest(() =>
22
22
  // @ts-expect-error
@@ -25,22 +25,26 @@ describe("string", () => {
25
25
  .throws(writeUnresolvableMessage("boo"))
26
26
  .type.errors("string | boolean")
27
27
  })
28
+
28
29
  it("unterminated string", () => {
29
30
  // @ts-expect-error
30
31
  attest(() => type("'bob")).throwsAndHasTypeError(
31
32
  writeUnterminatedEnclosedMessage("bob", "'")
32
33
  )
33
34
  })
35
+
34
36
  it("shallow single autocomplete", () => {
35
37
  // @ts-expect-error
36
38
  attest(() => type("str")).type.errors(
37
39
  `Argument of type '"str"' is not assignable to parameter of type '"string"'`
38
40
  )
39
41
  })
42
+
40
43
  it("shallow multi autocomplete", () => {
41
44
  // @ts-expect-error
42
45
  attest(() => type("s")).type.errors(`"string" | "symbol" | "semver"`)
43
46
  })
47
+
44
48
  it("post-operator autocomplete", () => {
45
49
  // @ts-expect-error
46
50
  attest(() => type("string|num")).type.errors(`"string|number"`)
@@ -1,38 +1,27 @@
1
- import { attest } from "@arktype/attest"
2
- import { lazily } from "@arktype/util"
3
- import { scope, type, type Ark, type Module, type Scope } from "arktype"
1
+ import { attest, contextualize } from "@arktype/attest"
4
2
  import {
5
3
  writeMissingSubmoduleAccessMessage,
6
4
  writeNonSubmoduleDotMessage,
7
5
  writeUnresolvableMessage
8
- } from "../parser/string/shift/operand/unenclosed.js"
6
+ } from "@arktype/schema"
7
+ import { type Module, type Scope, scope, type } from "arktype"
9
8
 
10
- const $ = lazily(() =>
11
- scope({
9
+ contextualize(() => {
10
+ const $ = scope({
12
11
  a: "string",
13
12
  b: "sub.alias",
14
13
  sub: scope({ alias: "number" }).export()
15
14
  })
16
- )
17
15
 
18
- describe("submodules", () => {
19
16
  it("base", () => {
20
17
  const types = $.export()
21
18
  attest<
22
19
  Module<{
23
- exports: {
24
- a: string
25
- b: number
26
- sub: Module<{
27
- exports: {
28
- alias: number
29
- }
30
- locals: {}
31
- ambient: Ark
32
- }>
33
- }
34
- locals: {}
35
- ambient: Ark
20
+ a: string
21
+ b: number
22
+ sub: Module<{
23
+ alias: number
24
+ }>
36
25
  }>
37
26
  >(types)
38
27
 
@@ -41,59 +30,57 @@ describe("submodules", () => {
41
30
  attest(types.sub.alias.json).is(expected)
42
31
  attest(types.b.json).is(expected)
43
32
  })
33
+
44
34
  it("non-submodule dot access", () => {
45
35
  // @ts-expect-error
46
36
  attest(() => $.type("b.foo")).throwsAndHasTypeError(
47
37
  writeNonSubmoduleDotMessage("b")
48
38
  )
49
39
  })
40
+
50
41
  it("thunk submodule", () => {
51
42
  const $ = scope({
52
43
  a: "string",
53
44
  c: "a",
54
45
  sub: () =>
55
- $.scope({
46
+ scope({
47
+ ...$.import("a", "c"),
56
48
  foo: "a",
57
49
  bar: "foo"
58
50
  }).export()
59
51
  })
60
52
  attest<
61
53
  Scope<{
62
- exports: {
63
- a: string
64
- c: string
65
- sub: Module<{
66
- exports: {
67
- foo: string
68
- bar: string
69
- }
70
- locals: {}
71
- ambient: Ark
72
- }>
73
- }
74
- locals: {}
75
- ambient: Ark
54
+ a: string
55
+ c: string
56
+ sub: Module<{
57
+ foo: string
58
+ bar: string
59
+ }>
76
60
  }>
77
61
  >($)
78
62
  })
63
+
79
64
  it("no alias reference", () => {
80
65
  // @ts-expect-error
81
66
  attest(() => $.type("sub")).throwsAndHasTypeError(
82
67
  writeMissingSubmoduleAccessMessage("sub")
83
68
  )
84
69
  })
70
+
85
71
  it("bad alias reference", () => {
86
72
  // @ts-expect-error
87
73
  attest(() => $.type("sub.marine")).throwsAndHasTypeError(
88
74
  writeUnresolvableMessage("sub.marine")
89
75
  )
90
76
  })
91
- it("autocompletion", () => {
92
- const base = scope({ foo: "true" })
77
+
78
+ it("completions", () => {
79
+ const base = scope({ foo: "true" }).export()
93
80
  // @ts-expect-error
94
81
  attest(() => scope({ base, reference: "base." }).export())
95
82
  .throws(writeUnresolvableMessage("base."))
96
- .type.errors("base.foo")
83
+ .type.completions({ "base.": ["base.foo"] })
97
84
  })
98
85
  // TODO: private aliases
99
86
  })