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,34 +1,234 @@
1
- // import { match } from "arktype"
1
+ // import { attest } from "@arktype/attest"
2
+ // import { match, scope } from "arktype"
2
3
 
3
- // const matcher = match({
4
- // boolean: (b) => !b,
5
- // semver: (s) => s.length
6
- // }).when({ condition: "true" }, (data) => {
7
- // return data.condition
4
+ // it("cases only", () => {
5
+ // const sizeOf = match({
6
+ // "string|Array": (v) => v.length,
7
+ // number: (v) => v,
8
+ // bigint: (v) => v
9
+ // }).orThrow()
10
+
11
+ // attest<number>(sizeOf("abc")).equals(3)
12
+ // attest<number>(sizeOf([1, 2, 3])).equals(3)
13
+ // attest<bigint>(sizeOf(5n)).equals(5n)
14
+ // })
15
+
16
+ // it("properly infers types of inputs/outputs", () => {
17
+ // const matcher = match({ string: (s) => s, number: (n) => n })
18
+ // .when("boolean", (b) => b)
19
+ // .orThrow()
20
+
21
+ // // properly infers the type of the output based on the input
22
+ // attest<string>(matcher("abc")).equals("abc")
23
+ // attest<number>(matcher(4)).equals(4)
24
+ // attest<boolean>(matcher(true)).equals(true)
25
+
26
+ // // and properly handles unions in the input type
27
+ // attest<string | number>(matcher(0 as string | number))
28
+ // })
29
+
30
+ // it("`.when` errors on redundant cases", () => {
31
+ // const matcher = match().when("string", (s) => s)
32
+
33
+ // // @ts-expect-error
34
+ // attest(() => matcher.when("string", (s) => s)).throwsAndHasTypeError(
35
+ // "This branch is redundant and will never be reached" // TODO: rewrite error message
36
+ // )
37
+ // })
38
+
39
+ // it("errors on cases redundant to a previous `cases` block", () => {
40
+ // const matcher = match({ string: (s) => s })
41
+
42
+ // // @ts-expect-error
43
+ // attest(() => matcher.cases({ string: (s) => s })).throwsAndHasTypeError(
44
+ // "This branch is redundant and will never be reached"
45
+ // )
8
46
  // })
9
47
 
10
- // const greeting = match({
11
- // "integer<12": () => "Good morning",
12
- // "integer<18": () => "Good afternoon",
13
- // default: () => "Good evening"
48
+ // // describe("constraint handling", () => {
49
+ // // it("properly considers constrained types as different from their base", () => {
50
+ // // const matcher = match
51
+ // // .only<number>()
52
+ // // .when("number>2", (n) => {
53
+ // // attest<number>(n)
54
+ // // return n
55
+ // // })
56
+ // // .when("number", (n) => n)
57
+ // // .finalize()
58
+
59
+ // // // for assertions
60
+ // // matcher(5)
61
+ // // })
62
+ // // })
63
+
64
+ // describe('"finalizations"', () => {
65
+ // it(".orThrow()", () => {
66
+ // const matcher = match()
67
+ // .when("string", (s) => s)
68
+ // .orThrow()
69
+
70
+ // // properly returns the `never` type and throws given a guaranteed-to-be-invalid input
71
+ // attest<never>(matcher(4))
72
+
73
+ // attest(() => matcher(4)).throws("TODO: what's the error message?") // TODO
74
+ // })
75
+
76
+ // // TODO: reenable
77
+ // // describe(".default", () => {
78
+ // // it("chained, given a callback", () => {
79
+ // // const matcher = match()
80
+ // // .when("string", (s) => s)
81
+ // // .default((_) => 0)
82
+
83
+ // // attest<string>(matcher("abc")).equals("abc")
84
+ // // attest<number>(matcher(4)).equals(0)
85
+
86
+ // // attest<string | number>(matcher(0 as unknown))
87
+ // // })
88
+
89
+ // // it("chained, given a value", () => {
90
+ // // const matcher = match()
91
+ // // .when("string", (s) => s)
92
+ // // .default(0)
93
+
94
+ // // attest<string>(matcher("abc")).equals("abc")
95
+ // // attest<number>(matcher(4)).equals(0)
96
+
97
+ // // attest<string | number>(matcher(0 as unknown))
98
+ // // })
99
+
100
+ // // it("in `cases`, given a callback", () => {
101
+ // // const matcher = match({ string: (s) => s, default: (_) => 0 })
102
+
103
+ // // attest<string>(matcher("abc")).equals("abc")
104
+ // // attest<number>(matcher(4)).equals(0)
105
+
106
+ // // attest<string | number>(matcher(0 as unknown))
107
+ // // })
108
+ // // })
109
+
110
+ // it("errors when attempting to `.finalize()` a non-exhaustive matcher", () => {
111
+ // const matcher = match().when("string", (s) => s)
112
+
113
+ // // @ts-expect-error
114
+ // attest(() => matcher.finalize()).throwsAndHasTypeError(
115
+ // "Cannot manually finalize a non-exhaustive matcher: consider adding a `.default` case, using one of the `.orX` methods, or using `match.only<T>`" // TODO: rewrite message
116
+ // )
117
+ // })
118
+
119
+ // it("considers `unknown` exhaustive", () => {
120
+ // const matcher = match()
121
+ // .when("unknown", (x) => x)
122
+ // .finalize()
123
+
124
+ // attest(matcher(4)).equals(4)
125
+ // })
14
126
  // })
15
127
 
16
- // const result = greeting(5)
128
+ // describe(".only<T>", () => {
129
+ // it("does not accept invalid inputs at a type-level", () => {
130
+ // const matcher = match
131
+ // .only<string | number>()
132
+ // .when("string", (s) => s)
133
+ // .when("number", (n) => n)
134
+ // .finalize()
135
+
136
+ // // @ts-expect-error
137
+ // attest(() => matcher(true)).throwsAndHasTypeError(
138
+ // "Argument of type 'true' is not assignable to parameter of type 'string | number'"
139
+ // ) // TODO: what's the runtime error?
140
+ // })
141
+
142
+ // it("errors when attempting to `.finalize()` a non-exhaustive matcher", () => {
143
+ // const matcher = match.only<string | number>().when("string", (s) => s)
144
+
145
+ // // @ts-expect-error
146
+ // attest(() => matcher.finalize()).throwsAndHasTypeError(
147
+ // "Cannot manually finalize a non-exhaustive matcher: consider adding a `.default` case, using one of the `.orX` methods, or handling the cases explicitly" // TODO: rewrite message. at runtime can we even show a counterexample (serialize the cases not handled)?
148
+ // )
149
+ // })
150
+
151
+ // it("allows finalizing exhaustive matchers", (_) => {
152
+ // const matcher = match
153
+ // .only<string | number>()
154
+ // .when("string", (s) => s)
155
+ // .when("number", (n) => n)
156
+ // .finalize()
17
157
 
18
- // const base = matcher({} as unknown) //=>?
158
+ // attest<string>(matcher("abc")).equals("abc")
159
+ // attest<number>(matcher(4)).equals(4)
19
160
 
20
- // const oneResult = matcher({} as object) //=>?
161
+ // attest<string | number>(matcher(0 as string | number))
162
+ // })
21
163
 
22
- // const twoResults = matcher({} as boolean | string) //=>?
164
+ // it("infers the parameter to chained .default as the remaining cases", () => {
165
+ // const matcher = match
166
+ // .only<string | number | boolean>()
167
+ // .when("string", (s) => s)
168
+ // .default((n) => {
169
+ // attest<number | boolean>(n)
170
+ // return n
171
+ // })
172
+
173
+ // // for assertions
174
+ // matcher(4)
175
+ // })
176
+
177
+ // it("infers the parameter to in-cases .default", () => {
178
+ // const matcher = match.only<string | number | boolean>().cases({
179
+ // string: (s) => s,
180
+ // default: (n) => {
181
+ // // TS doesn't understand sequentiality in cases, so it's inferred as the in-type
182
+ // attest<string | number | boolean>(n)
183
+ // return n
184
+ // }
185
+ // })
186
+
187
+ // // for assertions
188
+ // matcher(4)
189
+ // })
190
+
191
+ // it("returns `never` on only the specific cases handled by `.orThrow`", () => {
192
+ // const matcher = match
193
+ // .only<string | number>()
194
+ // .when("string", (s) => s)
195
+ // .orThrow()
196
+
197
+ // attest<never>(matcher(4))
198
+ // })
199
+ // })
200
+
201
+ // it("within scope", () => {
202
+ // const threeSixtyNoScope = scope({ three: "3", sixty: "60", no: "'no'" })
203
+
204
+ // const matcher = threeSixtyNoScope
205
+ // .match({
206
+ // three: (three) => {
207
+ // attest<3>(three)
208
+ // return 3
209
+ // }
210
+ // })
211
+ // .when("sixty", (sixty) => {
212
+ // attest<60>(sixty)
213
+ // return 60
214
+ // })
215
+ // .orThrow()
216
+
217
+ // // for assertions
218
+ // matcher(3)
219
+ // matcher(60)
220
+ // })
23
221
 
24
- // const validMatcher = match({
25
- // // ^?
26
- // number: (data) => data,
27
- // "string|unknown[]": (data) => data.length
222
+ // it("properly propagates errors from invalid type definitions in `when`", () => {
223
+ // // @ts-expect-error
224
+ // attest(() => match().when("strong", (s) => s)).type.errors(
225
+ // "'strong' is unresolvable"
226
+ // )
28
227
  // })
29
228
 
30
- // const invalidMatcher = match({
31
- // // ^?
32
- // "string|numbr": (data) => data,
33
- // "1<boolean<5": (data) => data
229
+ // it("properly propagates errors from invalid type definitions in `cases`", () => {
230
+ // // @ts-expect-error
231
+ // attest(() => match({ strong: (s) => s })).type.errors(
232
+ // "'strong' is unresolvable"
233
+ // )
34
234
  // })
@@ -1,76 +1,76 @@
1
- import { attest } from "@arktype/attest"
2
- import type { Out } from "@arktype/schema"
3
- import type { equals } from "@arktype/util"
1
+ import { attest, contextualize } from "@arktype/attest"
2
+ import type { Narrowed, Out, of, string } from "@arktype/schema"
3
+ import { registeredReference, type equals } from "@arktype/util"
4
4
  import { type, type Type } from "arktype"
5
5
 
6
- describe("narrow", () => {
6
+ contextualize(() => {
7
7
  it("implicit problem", () => {
8
8
  const isOdd = (n: number) => n % 2 === 1
9
+ const isOddRef = registeredReference(isOdd)
9
10
  const odd = type(["number", ":", isOdd])
10
11
  attest<number>(odd.infer)
11
- // attest(odd.node).equals({ number: { narrow: isOdd as any } })
12
- attest(odd(1).out).equals(1)
13
- attest(odd(2).errors?.summary).snap(
14
- "Must be valid according to isOdd (was 2)"
15
- )
12
+ attest(odd.json).equals({ domain: "number", predicate: [isOddRef] })
13
+ attest(odd(1)).equals(1)
14
+ attest(odd(2).toString()).snap("must be valid according to isOdd (was 2)")
16
15
  })
16
+
17
17
  it("implicit problem anonymous", () => {
18
- const even = type(["number", ":", (n) => n % 2 === 0])
19
- attest(even(1).errors?.summary).snap("Must be valid (was 1)")
18
+ const even = type("number", ":", n => n % 2 === 0)
19
+ attest(even(1).toString()).snap(
20
+ "must be valid according to an anonymous predicate (was 1)"
21
+ )
20
22
  })
23
+
21
24
  it("explicit problem", () => {
22
25
  const even = type([
23
26
  "number",
24
27
  ":",
25
- (n, ctx) => n % 3 === 0 || ctx.invalid({ expected: "divisible by 3" })
28
+ (n, ctx) => n % 3 === 0 || ctx.invalid("divisible by 3")
29
+ ])
30
+ attest(even(1).toString()).snap("must be divisible by 3 (was 1)")
31
+ })
32
+
33
+ it("problem at path", () => {
34
+ const abEqual = type([
35
+ {
36
+ a: "number",
37
+ b: "number"
38
+ },
39
+ ":",
40
+ ({ a, b }, ctx) => {
41
+ if (a === b) return true
42
+
43
+ ctx.error({ expected: "equal to b", path: ["a"] })
44
+ ctx.error({ expected: "equal to a", path: ["b"] })
45
+ return false
46
+ }
26
47
  ])
27
- attest(even(1).errors?.summary).snap("Must be divisible by 3 (was 1)")
48
+ attest(abEqual({ a: 1, b: 1 })).equals({ a: 1, b: 1 })
49
+ attest(abEqual({ a: 1, b: 2 }).toString()).snap(
50
+ 'a must be equal to b (was {"a":1,"b":2})\nb must be equal to a (was {"a":1,"b":2})'
51
+ )
28
52
  })
29
- // it("problem at path", () => {
30
- // type([{ s: "string" }])
31
- // const abEqual = type([
32
- // {
33
- // a: "number",
34
- // b: "number"
35
- // },
36
- // ":",
37
- // ({ a, b }, ctx) => {
38
- // if (a === b) {
39
- // return true
40
- // }
41
- // // a, ["a"]
42
- // ctx.error("equal to b")
43
- // // b, ["b"]
44
- // ctx.error("equal to a")
45
- // return false
46
- // }
47
- // ])
48
- // attest(abEqual({ a: 1, b: 1 }).out).equals({ a: 1, b: 1 })
49
- // attest(abEqual({ a: 1, b: 2 }).errors?.summary).snap(
50
- // 'a must be equal to b (was {"a":1,"b":2})\nb must be equal to a (was {"a":1,"b":2})'
51
- // )
52
- // })
53
+
53
54
  it("functional predicate", () => {
54
55
  const one = type(["number", ":", (n): n is 1 => n === 1])
55
- attest<Type<1>>(one)
56
+ attest<1>(one.infer)
56
57
  })
58
+
57
59
  it("functional parameter inference", () => {
58
60
  type Expected = number | boolean[]
59
61
  const validateNumberOrBooleanList = <t>(
60
62
  t: equals<t, Expected> extends true ? t : Expected
61
63
  ) => true
62
64
  attest<number | boolean[]>(
63
- type([
64
- "number|boolean[]",
65
- ":",
66
- (data) => validateNumberOrBooleanList(data)
67
- ]).infer
65
+ type(["number|boolean[]", ":", data => validateNumberOrBooleanList(data)])
66
+ .infer
68
67
  )
69
68
  attest(() => {
70
69
  // @ts-expect-error
71
70
  type(["number|boolean[]", ":", (data: number | string[]) => !!data])
72
71
  }).type.errors("Type 'boolean' is not assignable to type 'string'.")
73
72
  })
73
+
74
74
  it("narrow problem", () => {
75
75
  const palindrome = type([
76
76
  "string",
@@ -78,25 +78,23 @@ describe("narrow", () => {
78
78
  (s, ctx) =>
79
79
  s === [...s].reverse().join("") ? true : ctx.invalid("a palindrome")
80
80
  ])
81
- attest<Type<string>>(palindrome)
82
- attest(palindrome("dad").out).snap("dad")
83
- attest(palindrome("david").errors?.summary).snap(
84
- "Must be a palindrome (was 'david')"
81
+ attest<Type<string.narrowed>>(palindrome)
82
+ attest(palindrome("dad")).snap("dad")
83
+ attest(palindrome("david").toString()).snap(
84
+ 'must be a palindrome (was "david")'
85
85
  )
86
86
  })
87
+
87
88
  it("narrows the output type of a morph", () => {
88
89
  const t = type("string")
89
- .morph((s) => s.length)
90
+ .pipe(s => s.length)
90
91
  .narrow((n): n is 5 => n === 5)
91
- attest<Type<(In: string) => Out<5>>>(t)
92
- attest(t.json).snap({ domain: "string" })
92
+
93
+ attest<Type<(In: string) => Out<of<5, Narrowed>>, {}>>(t)
93
94
  })
95
+
94
96
  it("expression", () => {
95
97
  const t = type("string", ":", (s): s is `f${string}` => s[0] === "f")
96
98
  attest<`f${string}`>(t.infer)
97
- attest(t.json).snap({
98
- basis: "string",
99
- predicate: ["$ark.anonymousFunction8"]
100
- })
101
99
  })
102
100
  })