arktype 2.0.0-dev.2 → 2.0.0-dev.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (223) hide show
  1. package/LICENSE +7 -0
  2. package/out/api.d.ts +6 -0
  3. package/out/api.js +5 -0
  4. package/out/ark.d.ts +15 -39
  5. package/out/ark.js +9 -18
  6. package/out/config.d.ts +1 -0
  7. package/out/config.js +2 -0
  8. package/out/generic.d.ts +21 -0
  9. package/out/generic.js +24 -0
  10. package/out/match.d.ts +18 -24
  11. package/out/match.js +50 -60
  12. package/out/module.d.ts +11 -0
  13. package/out/module.js +2 -0
  14. package/out/parser/definition.d.ts +13 -15
  15. package/out/parser/definition.js +10 -13
  16. package/out/parser/generic.d.ts +5 -7
  17. package/out/parser/generic.js +7 -11
  18. package/out/parser/objectLiteral.d.ts +57 -18
  19. package/out/parser/objectLiteral.js +93 -52
  20. package/out/parser/semantic/bounds.d.ts +10 -8
  21. package/out/parser/semantic/bounds.js +0 -1
  22. package/out/parser/semantic/divisor.d.ts +5 -7
  23. package/out/parser/semantic/divisor.js +0 -1
  24. package/out/parser/semantic/infer.d.ts +24 -0
  25. package/out/parser/semantic/infer.js +1 -0
  26. package/out/parser/semantic/utils.d.ts +3 -7
  27. package/out/parser/semantic/utils.js +0 -1
  28. package/out/parser/semantic/validate.d.ts +16 -13
  29. package/out/parser/semantic/validate.js +1 -1
  30. package/out/parser/string/reduce/dynamic.d.ts +13 -13
  31. package/out/parser/string/reduce/dynamic.js +31 -26
  32. package/out/parser/string/reduce/shared.d.ts +14 -28
  33. package/out/parser/string/reduce/shared.js +4 -3
  34. package/out/parser/string/reduce/static.d.ts +26 -23
  35. package/out/parser/string/reduce/static.js +0 -1
  36. package/out/parser/string/shift/operand/date.d.ts +2 -2
  37. package/out/parser/string/shift/operand/date.js +4 -7
  38. package/out/parser/string/shift/operand/enclosed.d.ts +3 -4
  39. package/out/parser/string/shift/operand/enclosed.js +14 -14
  40. package/out/parser/string/shift/operand/genericArgs.d.ts +9 -10
  41. package/out/parser/string/shift/operand/genericArgs.js +6 -10
  42. package/out/parser/string/shift/operand/operand.d.ts +3 -4
  43. package/out/parser/string/shift/operand/operand.js +9 -13
  44. package/out/parser/string/shift/operand/unenclosed.d.ts +15 -20
  45. package/out/parser/string/shift/operand/unenclosed.js +24 -36
  46. package/out/parser/string/shift/operator/bounds.d.ts +8 -12
  47. package/out/parser/string/shift/operator/bounds.js +45 -56
  48. package/out/parser/string/shift/operator/divisor.d.ts +1 -3
  49. package/out/parser/string/shift/operator/divisor.js +1 -3
  50. package/out/parser/string/shift/operator/operator.d.ts +1 -2
  51. package/out/parser/string/shift/operator/operator.js +12 -19
  52. package/out/parser/string/shift/scanner.d.ts +8 -7
  53. package/out/parser/string/shift/scanner.js +17 -16
  54. package/out/parser/string/string.d.ts +9 -7
  55. package/out/parser/string/string.js +1 -3
  56. package/out/parser/tuple.d.ts +101 -42
  57. package/out/parser/tuple.js +107 -86
  58. package/out/scope.d.ts +44 -115
  59. package/out/scope.js +39 -188
  60. package/out/type.d.ts +63 -61
  61. package/out/type.js +30 -135
  62. package/package.json +33 -39
  63. package/__tests__/array.test.ts +0 -310
  64. package/__tests__/astUtils.test.ts +0 -17
  65. package/__tests__/badDefinitionType.test.ts +0 -51
  66. package/__tests__/basis.test.ts +0 -46
  67. package/__tests__/bounds.test.ts +0 -388
  68. package/__tests__/cast.test.ts +0 -32
  69. package/__tests__/completions.test.ts +0 -46
  70. package/__tests__/config.test.ts +0 -77
  71. package/__tests__/cyclic.test.ts +0 -144
  72. package/__tests__/dateLiteral.test.ts +0 -32
  73. package/__tests__/declared.test.ts +0 -124
  74. package/__tests__/discrimination.test.ts +0 -74
  75. package/__tests__/divisor.test.ts +0 -90
  76. package/__tests__/enclosed.test.ts +0 -93
  77. package/__tests__/examples.test.ts +0 -56
  78. package/__tests__/expressions.test.ts +0 -138
  79. package/__tests__/generateBenchData.ts +0 -89
  80. package/__tests__/generated/cyclic.ts +0 -3058
  81. package/__tests__/generic.test.ts +0 -294
  82. package/__tests__/group.test.ts +0 -72
  83. package/__tests__/hkt.test.ts +0 -22
  84. package/__tests__/imports.test.ts +0 -113
  85. package/__tests__/instanceof.test.ts +0 -102
  86. package/__tests__/intersection.test.ts +0 -104
  87. package/__tests__/jsdoc.test.ts +0 -64
  88. package/__tests__/keyTraversal.test.ts +0 -74
  89. package/__tests__/keyof.test.ts +0 -134
  90. package/__tests__/keywords.test.ts +0 -188
  91. package/__tests__/literal.test.ts +0 -51
  92. package/__tests__/match.bench.ts +0 -31
  93. package/__tests__/match.test.ts +0 -224
  94. package/__tests__/methods.bench.ts +0 -22
  95. package/__tests__/morph.test.ts +0 -409
  96. package/__tests__/narrow.test.ts +0 -102
  97. package/__tests__/nodeDefinitions.test.ts +0 -190
  98. package/__tests__/object.bench.ts +0 -28
  99. package/__tests__/objectLiteral.test.ts +0 -301
  100. package/__tests__/operand.bench.ts +0 -45
  101. package/__tests__/operator.bench.ts +0 -130
  102. package/__tests__/regex.test.ts +0 -58
  103. package/__tests__/scope.bench.ts +0 -39
  104. package/__tests__/scope.test.ts +0 -140
  105. package/__tests__/string.test.ts +0 -48
  106. package/__tests__/submodule.test.ts +0 -99
  107. package/__tests__/this.test.ts +0 -57
  108. package/__tests__/thunk.test.ts +0 -80
  109. package/__tests__/traverse.test.ts +0 -119
  110. package/__tests__/tsGenerics.test.ts +0 -12
  111. package/__tests__/typeMethods.test.ts +0 -69
  112. package/__tests__/typeReference.test.ts +0 -39
  113. package/__tests__/unenclosed.test.ts +0 -120
  114. package/__tests__/union.test.ts +0 -147
  115. package/ark.ts +0 -91
  116. package/main.ts +0 -18
  117. package/match.ts +0 -243
  118. package/out/ark.d.ts.map +0 -1
  119. package/out/ark.js.map +0 -1
  120. package/out/arkConfig.d.ts +0 -2
  121. package/out/arkConfig.d.ts.map +0 -1
  122. package/out/arkConfig.js +0 -10
  123. package/out/arkConfig.js.map +0 -1
  124. package/out/main.d.ts +0 -7
  125. package/out/main.d.ts.map +0 -1
  126. package/out/main.js +0 -4
  127. package/out/main.js.map +0 -1
  128. package/out/match.d.ts.map +0 -1
  129. package/out/match.js.map +0 -1
  130. package/out/parser/definition.d.ts.map +0 -1
  131. package/out/parser/definition.js.map +0 -1
  132. package/out/parser/generic.d.ts.map +0 -1
  133. package/out/parser/generic.js.map +0 -1
  134. package/out/parser/objectLiteral.d.ts.map +0 -1
  135. package/out/parser/objectLiteral.js.map +0 -1
  136. package/out/parser/semantic/bounds.d.ts.map +0 -1
  137. package/out/parser/semantic/bounds.js.map +0 -1
  138. package/out/parser/semantic/divisor.d.ts.map +0 -1
  139. package/out/parser/semantic/divisor.js.map +0 -1
  140. package/out/parser/semantic/intersections.d.ts +0 -14
  141. package/out/parser/semantic/intersections.d.ts.map +0 -1
  142. package/out/parser/semantic/intersections.js +0 -2
  143. package/out/parser/semantic/intersections.js.map +0 -1
  144. package/out/parser/semantic/semantic.d.ts +0 -33
  145. package/out/parser/semantic/semantic.d.ts.map +0 -1
  146. package/out/parser/semantic/semantic.js +0 -2
  147. package/out/parser/semantic/semantic.js.map +0 -1
  148. package/out/parser/semantic/utils.d.ts.map +0 -1
  149. package/out/parser/semantic/utils.js.map +0 -1
  150. package/out/parser/semantic/validate.d.ts.map +0 -1
  151. package/out/parser/semantic/validate.js.map +0 -1
  152. package/out/parser/shared.d.ts +0 -56
  153. package/out/parser/shared.d.ts.map +0 -1
  154. package/out/parser/shared.js +0 -55
  155. package/out/parser/shared.js.map +0 -1
  156. package/out/parser/string/reduce/dynamic.d.ts.map +0 -1
  157. package/out/parser/string/reduce/dynamic.js.map +0 -1
  158. package/out/parser/string/reduce/shared.d.ts.map +0 -1
  159. package/out/parser/string/reduce/shared.js.map +0 -1
  160. package/out/parser/string/reduce/static.d.ts.map +0 -1
  161. package/out/parser/string/reduce/static.js.map +0 -1
  162. package/out/parser/string/shift/operand/date.d.ts.map +0 -1
  163. package/out/parser/string/shift/operand/date.js.map +0 -1
  164. package/out/parser/string/shift/operand/enclosed.d.ts.map +0 -1
  165. package/out/parser/string/shift/operand/enclosed.js.map +0 -1
  166. package/out/parser/string/shift/operand/genericArgs.d.ts.map +0 -1
  167. package/out/parser/string/shift/operand/genericArgs.js.map +0 -1
  168. package/out/parser/string/shift/operand/operand.d.ts.map +0 -1
  169. package/out/parser/string/shift/operand/operand.js.map +0 -1
  170. package/out/parser/string/shift/operand/unenclosed.d.ts.map +0 -1
  171. package/out/parser/string/shift/operand/unenclosed.js.map +0 -1
  172. package/out/parser/string/shift/operator/bounds.d.ts.map +0 -1
  173. package/out/parser/string/shift/operator/bounds.js.map +0 -1
  174. package/out/parser/string/shift/operator/divisor.d.ts.map +0 -1
  175. package/out/parser/string/shift/operator/divisor.js.map +0 -1
  176. package/out/parser/string/shift/operator/operator.d.ts.map +0 -1
  177. package/out/parser/string/shift/operator/operator.js.map +0 -1
  178. package/out/parser/string/shift/scanner.d.ts.map +0 -1
  179. package/out/parser/string/shift/scanner.js.map +0 -1
  180. package/out/parser/string/string.d.ts.map +0 -1
  181. package/out/parser/string/string.js.map +0 -1
  182. package/out/parser/tuple.d.ts.map +0 -1
  183. package/out/parser/tuple.js.map +0 -1
  184. package/out/scope.d.ts.map +0 -1
  185. package/out/scope.js.map +0 -1
  186. package/out/test.d.ts +0 -2
  187. package/out/test.d.ts.map +0 -1
  188. package/out/test.js +0 -7
  189. package/out/test.js.map +0 -1
  190. package/out/tsconfig.build.tsbuildinfo +0 -1
  191. package/out/type.d.ts.map +0 -1
  192. package/out/type.js.map +0 -1
  193. package/out/util.d.ts +0 -19
  194. package/out/util.d.ts.map +0 -1
  195. package/out/util.js +0 -7
  196. package/out/util.js.map +0 -1
  197. package/parser/definition.ts +0 -159
  198. package/parser/generic.ts +0 -68
  199. package/parser/objectLiteral.ts +0 -197
  200. package/parser/semantic/bounds.ts +0 -61
  201. package/parser/semantic/divisor.ts +0 -13
  202. package/parser/semantic/intersections.ts +0 -46
  203. package/parser/semantic/semantic.ts +0 -143
  204. package/parser/semantic/utils.ts +0 -26
  205. package/parser/semantic/validate.ts +0 -109
  206. package/parser/shared.ts +0 -167
  207. package/parser/string/reduce/dynamic.ts +0 -196
  208. package/parser/string/reduce/shared.ts +0 -101
  209. package/parser/string/reduce/static.ts +0 -298
  210. package/parser/string/shift/operand/date.ts +0 -54
  211. package/parser/string/shift/operand/enclosed.ts +0 -118
  212. package/parser/string/shift/operand/genericArgs.ts +0 -127
  213. package/parser/string/shift/operand/operand.ts +0 -48
  214. package/parser/string/shift/operand/unenclosed.ts +0 -268
  215. package/parser/string/shift/operator/bounds.ts +0 -264
  216. package/parser/string/shift/operator/divisor.ts +0 -36
  217. package/parser/string/shift/operator/operator.ts +0 -82
  218. package/parser/string/shift/scanner.ts +0 -209
  219. package/parser/string/string.ts +0 -90
  220. package/parser/tuple.ts +0 -475
  221. package/scope.ts +0 -548
  222. package/type.ts +0 -349
  223. package/util.ts +0 -36
@@ -1,224 +0,0 @@
1
- import { attest } from "@arktype/attest"
2
- import { match } from "arktype"
3
- import { scope } from "../ark.js"
4
-
5
- describe("match", () => {
6
- it("properly infers types of inputs/outputs", () => {
7
- const matcher = match({ string: (s) => s, number: (n) => n })
8
- .when("boolean", (b) => b)
9
- .orThrow()
10
-
11
- // properly infers the type of the output based on the input
12
- attest<string>(matcher("abc")).equals("abc")
13
- attest<number>(matcher(4)).equals(4)
14
- attest<boolean>(matcher(true)).equals(true)
15
-
16
- // and properly handles unions in the input type
17
- attest<string | number>(matcher(0 as string | number))
18
- })
19
-
20
- it("`.when` errors on redundant cases", () => {
21
- const matcher = match().when("string", (s) => s)
22
-
23
- // @ts-expect-error
24
- attest(() => matcher.when("string", (s) => s)).throwsAndHasTypeError(
25
- "This branch is redundant and will never be reached" // TODO: rewrite error message
26
- )
27
- })
28
-
29
- it("errors on cases redundant to a previous `cases` block", () => {
30
- const matcher = match({ string: (s) => s })
31
-
32
- // @ts-expect-error
33
- attest(() => matcher.cases({ string: (s) => s })).throwsAndHasTypeError(
34
- "This branch is redundant and will never be reached"
35
- )
36
- })
37
-
38
- describe("constraint handling", () => {
39
- it("properly considers constrained types as different from their base", () => {
40
- const matcher = match
41
- .only<number>()
42
- .when("number>2", (n) => {
43
- attest<number>(n)
44
- return n
45
- })
46
- .when("number", (n) => n)
47
- .finalize()
48
-
49
- // for assertions
50
- matcher(5)
51
- })
52
- })
53
-
54
- describe('"finalizations"', () => {
55
- it(".orThrow()", () => {
56
- const matcher = match()
57
- .when("string", (s) => s)
58
- .orThrow()
59
-
60
- // properly returns the `never` type and throws given a guaranteed-to-be-invalid input
61
- attest<never>(matcher(4))
62
-
63
- attest(() => matcher(4)).throws("TODO: what's the error message?") // TODO
64
- })
65
-
66
- describe(".default", () => {
67
- it("chained, given a callback", () => {
68
- const matcher = match()
69
- .when("string", (s) => s)
70
- .default((_) => 0)
71
-
72
- attest<string>(matcher("abc")).equals("abc")
73
- attest<number>(matcher(4)).equals(0)
74
-
75
- attest<string | number>(matcher(0 as unknown))
76
- })
77
-
78
- it("chained, given a value", () => {
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("in `cases`, given a callback", () => {
90
- const matcher = match({ string: (s) => s, default: (_) => 0 })
91
-
92
- attest<string>(matcher("abc")).equals("abc")
93
- attest<number>(matcher(4)).equals(0)
94
-
95
- attest<string | number>(matcher(0 as unknown))
96
- })
97
- })
98
-
99
- it("errors when attempting to `.finalize()` a non-exhaustive matcher", () => {
100
- const matcher = match().when("string", (s) => s)
101
-
102
- // @ts-expect-error
103
- attest(() => matcher.finalize()).throwsAndHasTypeError(
104
- "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
105
- )
106
- })
107
-
108
- it("considers `unknown` exhaustive", () => {
109
- const matcher = match()
110
- .when("unknown", (x) => x)
111
- .finalize()
112
-
113
- attest(matcher(4)).equals(4)
114
- })
115
- })
116
-
117
- describe(".only<T>", () => {
118
- it("does not accept invalid inputs at a type-level", () => {
119
- const matcher = match
120
- .only<string | number>()
121
- .when("string", (s) => s)
122
- .when("number", (n) => n)
123
- .finalize()
124
-
125
- // @ts-expect-error
126
- attest(() => matcher(true)).throwsAndHasTypeError(
127
- "Argument of type 'true' is not assignable to parameter of type 'string | number'"
128
- ) // TODO: what's the runtime error?
129
- })
130
-
131
- it("errors when attempting to `.finalize()` a non-exhaustive matcher", () => {
132
- const matcher = match.only<string | number>().when("string", (s) => s)
133
-
134
- // @ts-expect-error
135
- attest(() => matcher.finalize()).throwsAndHasTypeError(
136
- "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)?
137
- )
138
- })
139
-
140
- it("allows finalizing exhaustive matchers", (_) => {
141
- const matcher = match
142
- .only<string | number>()
143
- .when("string", (s) => s)
144
- .when("number", (n) => n)
145
- .finalize()
146
-
147
- attest<string>(matcher("abc")).equals("abc")
148
- attest<number>(matcher(4)).equals(4)
149
-
150
- attest<string | number>(matcher(0 as string | number))
151
- })
152
-
153
- it("infers the parameter to chained .default as the remaining cases", () => {
154
- const matcher = match
155
- .only<string | number | boolean>()
156
- .when("string", (s) => s)
157
- .default((n) => {
158
- attest<number | boolean>(n)
159
- return n
160
- })
161
-
162
- // for assertions
163
- matcher(4)
164
- })
165
-
166
- it("infers the parameter to in-cases .default", () => {
167
- const matcher = match.only<string | number | boolean>().cases({
168
- string: (s) => s,
169
- default: (n) => {
170
- // TS doesn't understand sequentiality in cases, so it's inferred as the in-type
171
- attest<string | number | boolean>(n)
172
- return n
173
- }
174
- })
175
-
176
- // for assertions
177
- matcher(4)
178
- })
179
-
180
- it("returns `never` on only the specific cases handled by `.orThrow`", () => {
181
- const matcher = match
182
- .only<string | number>()
183
- .when("string", (s) => s)
184
- .orThrow()
185
-
186
- attest<never>(matcher(4))
187
- })
188
- })
189
-
190
- it("within scope", () => {
191
- const threeSixtyNoScope = scope({ three: "3", sixty: "60", no: "'no'" })
192
-
193
- const matcher = threeSixtyNoScope
194
- .match({
195
- three: (three) => {
196
- attest<3>(three)
197
- return 3
198
- }
199
- })
200
- .when("sixty", (sixty) => {
201
- attest<60>(sixty)
202
- return 60
203
- })
204
- .orThrow()
205
-
206
- // for assertions
207
- matcher(3)
208
- matcher(60)
209
- })
210
-
211
- it("properly propagates errors from invalid type definitions in `when`", () => {
212
- // @ts-expect-error
213
- attest(() => match().when("strong", (s) => s)).type.errors(
214
- "'strong' is unresolvable"
215
- )
216
- })
217
-
218
- it("properly propagates errors from invalid type definitions in `cases`", () => {
219
- // @ts-expect-error
220
- attest(() => match({ strong: (s) => s })).type.errors(
221
- "'strong' is unresolvable"
222
- )
223
- })
224
- })
@@ -1,22 +0,0 @@
1
- import { bench } from "@arktype/attest"
2
- import { type } from "arktype"
3
-
4
- bench("inline definition", () => {
5
- const _ = type({
6
- a: "string"
7
- })
8
- }).types([194, "instantiations"])
9
-
10
- bench("referenced type", () => {
11
- const a = type("string")
12
- const _ = type({
13
- a
14
- })
15
- }).types([138, "instantiations"])
16
-
17
- // https://github.com/arktypeio/arktype/issues/787
18
- bench("inline reference", () => {
19
- const _ = type({
20
- a: type("string")
21
- })
22
- }).types([5853, "instantiations"])
@@ -1,409 +0,0 @@
1
- import { attest } from "@arktype/attest"
2
- import {
3
- writeUndiscriminableMorphUnionMessage,
4
- type ArkTypeError,
5
- type Out
6
- } from "@arktype/schema"
7
- import { scope, type, type Ark, type Type } from "arktype"
8
-
9
- describe("morph", () => {
10
- it("base", () => {
11
- const t = type("boolean").morph((data) => `${data}`)
12
- attest<Type<(In: boolean) => Out<string>>>(t)
13
- attest<string>(t.infer)
14
- attest<boolean>(t.in.infer)
15
- const result = t(true)
16
- if (result.errors) {
17
- return result.errors.throw()
18
- }
19
- attest<string>(result.out).equals("true")
20
- attest(t("foo").errors?.summary).snap("Must be boolean (was string)")
21
- attest(t.root).equals(type(["boolean", "=>", (data) => `${data}`]).root)
22
- })
23
- it("endomorph", () => {
24
- const t = type(["boolean", "=>", (data) => !data])
25
- attest<Type<(In: boolean) => Out<boolean>>>(t)
26
- const result = t(true)
27
- if (result.errors) {
28
- return result.errors.throw()
29
- }
30
- attest<boolean>(result.out).equals(false)
31
- })
32
- // it("chained to type", () => {
33
- // const t = type(["string>5", "=>", arktypes.parse.date])
34
- // attest<Type<(In: string) => Out<Date>>>(t)
35
- // attest(t("5/21/1993").out?.getDate()).equals(21)
36
- // attest(t("foobar").errors?.summary).snap(
37
- // "Must be a valid date (was 'foobar')"
38
- // )
39
- // })
40
- it("validated output", () => {
41
- const parsedUser = type("string").morph((s) => JSON.parse(s), {
42
- name: "string",
43
- age: "number"
44
- })
45
- attest<
46
- Type<
47
- (In: string) => Out<{
48
- name: string
49
- age: number
50
- }>,
51
- Ark
52
- >
53
- >(parsedUser)
54
- })
55
- it("any as out", () => {
56
- const t = type("string", "=>", (s) => s as any)
57
- attest<string>(t.in.infer)
58
- attest<any>(t.infer)
59
- })
60
- it("never as out", () => {
61
- const t = type("string", "=>", (s) => s as never)
62
- attest<string>(t.in.infer)
63
- attest<never>(t.infer)
64
- })
65
- // it("return problem", () => {
66
- // const divide100By = type([
67
- // "number",
68
- // "=>",
69
- // (n, errors) => (n === 0 ? errors.mustBe("non-zero", n, []) : 100 / n)
70
- // ])
71
- // attest<Type<(In: number) => Out<number>>>(divide100By)
72
- // attest(divide100By(5).out).equals(20)
73
- // attest(divide100By(0).errors?.summary).snap("Must be non-zero (was 0)")
74
- // })
75
- it("adds a problem if one is returned without being added", () => {
76
- const divide100By = type([
77
- "number",
78
- "=>",
79
- (n, errors) => {
80
- if (n !== 0) {
81
- return 100 / n
82
- } else {
83
- // errors.mustBe("non-zero")
84
- // errors.byPath = {}
85
- return (errors as unknown as ArkTypeError[]).pop()
86
- }
87
- }
88
- ])
89
- attest(divide100By(0).errors?.summary).snap("Must be non-zero (was 0)")
90
- })
91
- it("at path", () => {
92
- const t = type({ a: ["string", "=>", (data) => data.length] })
93
- attest<Type<{ a: (In: string) => Out<number> }>>(t)
94
-
95
- const result = t({ a: "four" })
96
- if (result.errors) {
97
- return result.errors.throw()
98
- }
99
- attest<{ a: number }>(result.out).equals({ a: 4 })
100
- })
101
- it("in array", () => {
102
- const types = scope({
103
- lengthOfString: ["string", "=>", (data) => data.length],
104
- mapToLengths: "lengthOfString[]"
105
- }).export()
106
- attest<Type<((In: string) => Out<number>)[]>>(types.mapToLengths)
107
- const result = types.mapToLengths(["1", "22", "333"])
108
- if (result.errors) {
109
- return result.errors.throw()
110
- }
111
- attest<number[]>(result.out).equals([1, 2, 3])
112
- })
113
- it("object inference", () => {
114
- const t = type([{ a: "string" }, "=>", (data) => `${data}`])
115
- attest<Type<(In: { a: string }) => Out<string>>>(t)
116
- })
117
- it("intersection", () => {
118
- const $ = scope({
119
- b: "3.14",
120
- a: () => $.type("number"), //.morph((data) => `${data}`),
121
- aAndB: () => $.type("a&b"),
122
- bAndA: () => $.type("b&a")
123
- })
124
- // const types = $.compile()
125
- // attest<Type<(In: 3.14) => string>>(types.aAndB)
126
- // attest(types.aAndB.node).snap({
127
- // number: { rules: { value: 3.14 }, morph: "(function)" }
128
- // })
129
- // attest<typeof types.aAndB>(types.bAndA)
130
- // attest(types.bAndA.node).equals(types.aAndB.node)
131
- })
132
- it("object intersection", () => {
133
- // const $ = scope({
134
- // a: () => $.type({ a: "1" }).morph((data) => `${data}`),
135
- // b: { b: "2" },
136
- // c: "a&b"
137
- // })
138
- // const types = $.compile()
139
- // attest<Type<(In: { a: 1; b: 2 }) => string>>(types.c)
140
- // attest(types.c.node).snap({
141
- // object: {
142
- // rules: {
143
- // props: {
144
- // a: { number: { value: 1 } },
145
- // b: { number: { value: 2 } }
146
- // }
147
- // },
148
- // morph: "(function)"
149
- // }
150
- // })
151
- })
152
- it("union", () => {
153
- const types = scope({
154
- a: ["number", "=>", (data) => `${data}`],
155
- b: "boolean",
156
- aOrB: "a|b",
157
- bOrA: "b|a"
158
- }).export()
159
- attest<Type<boolean | ((In: number) => Out<string>)>>(types.aOrB)
160
- // attest(types.aOrB.node).snap({
161
- // number: { rules: {}, morph: "(function)" },
162
- // boolean: true
163
- // })
164
- // attest<typeof types.aOrB>(types.bOrA)
165
- // attest(types.bOrA.node).equals(types.aOrB.node)
166
- })
167
- it("union with output", () => {
168
- const t = type("number|parse.number")
169
- attest<number>(t.infer)
170
- })
171
- it("deep intersection", () => {
172
- // TODO: Fix
173
- // const types = scope({
174
- // a: { a: ["number>0", "=>", (data) => data + 1] },
175
- // b: { a: "1" },
176
- // c: "a&b"
177
- // }).export()
178
- // attest<Type<{ a: (In: 1) => Out<number> }>>(types.c)
179
- // attest(types.c.node).snap({
180
- // object: {
181
- // props: {
182
- // a: { number: { rules: { value: 1 }, morph: "(function)" } }
183
- // }
184
- // }
185
- // })
186
- })
187
- it("deep union", () => {
188
- const types = scope({
189
- a: { a: ["number>0", "=>", (data) => `${data}`] },
190
- b: { a: "Function" },
191
- c: "a|b"
192
- }).export()
193
- attest<
194
- Type<
195
- | {
196
- a: (In: number) => Out<string>
197
- }
198
- | {
199
- a: Function
200
- }
201
- >
202
- >(types.c)
203
-
204
- // attest(types.c.node).snap({
205
- // object: [
206
- // {
207
- // props: {
208
- // a: {
209
- // number: {
210
- // rules: {
211
- // range: {
212
- // min: { limit: 0, comparator: ">" }
213
- // }
214
- // },
215
- // morph: "(function)"
216
- // }
217
- // }
218
- // }
219
- // },
220
- // { props: { a: "Function" } }
221
- // ]
222
- // })
223
- })
224
- it("chained reference", () => {
225
- const $ = scope({
226
- a: type("string").morph((s) => s.length),
227
- b: () => $.type("a").morph((n) => n === 0)
228
- })
229
- const types = $.export()
230
- attest<Type<(In: string) => Out<boolean>>>(types.b)
231
- // attest(types.b.node).snap({
232
- // string: { rules: {}, morph: ["(function)", "(function)"] }
233
- // })
234
- })
235
- it("chained nested", () => {
236
- const $ = scope({
237
- a: type("string").morph((s) => s.length),
238
- b: () => $.type({ a: "a" }).morph(({ a }) => a === 0)
239
- })
240
-
241
- const types = $.export()
242
- attest<Type<(In: { a: string }) => Out<boolean>>>(types.b)
243
- // attest(types.b.node).snap({
244
- // object: { rules: { props: { a: "a" } }, morph: "(function)" }
245
- // })
246
- })
247
- it("directly nested", () => {
248
- const t = type([
249
- {
250
- a: ["string", "=>", (s: string) => s.length]
251
- },
252
- "=>",
253
- ({ a }) => a === 0
254
- ])
255
- attest<Type<(In: { a: string }) => Out<boolean>>>(t)
256
- // attest(t.node).snap({
257
- // object: {
258
- // rules: {
259
- // props: { a: { string: { rules: {}, morph: "(function)" } } }
260
- // },
261
- // morph: "(function)"
262
- // }
263
- // })
264
- })
265
- it("discriminable tuple union", () => {
266
- const $ = scope({
267
- a: () => $.type(["string"]).morph((s) => [...s, "!"]),
268
- b: ["boolean"],
269
- c: () => $.type("a|b")
270
- })
271
- const types = $.export()
272
- attest<Type<[boolean] | ((In: [string]) => Out<string[]>)>>(types.c)
273
- })
274
- it("double intersection", () => {
275
- // attest(() => {
276
- // const z = scope({
277
- // a: ["boolean", "=>", (data) => `${data}`],
278
- // b: ["boolean", "=>", (data) => `${data}!!!`],
279
- // c: "a&b"
280
- // }).compile()
281
- // }).throws("Intersection of morphs results in an unsatisfiable type")
282
- })
283
- it("undiscriminated union", () => {
284
- // TODO: fix
285
- // attest(() => {
286
- // scope({
287
- // a: ["/.*/", "=>", (s) => s.trim()],
288
- // b: "string",
289
- // c: "a|b"
290
- // }).export()
291
- // }).throws(writeUndiscriminableMorphUnionMessage("/"))
292
- })
293
- it("deep double intersection", () => {
294
- attest(() => {
295
- // const s = scope({
296
- // a: { a: ["boolean", "=>", (data) => `${data}`] },
297
- // b: { a: ["boolean", "=>", (data) => `${data}!!!`] },
298
- // c: "a&b"
299
- // }).compile()
300
- }).throws("At a: Intersection of morphs results in an unsatisfiable type")
301
- })
302
- it("deep undiscriminated union", () => {
303
- attest(() => {
304
- scope({
305
- a: { a: ["string", "=>", (s) => s.trim()] },
306
- b: { a: "'foo'" },
307
- c: "a|b"
308
- }).export()
309
- }).throws(writeUndiscriminableMorphUnionMessage("/"))
310
- })
311
- it("deep undiscriminated reference", () => {
312
- const $ = scope({
313
- a: { a: ["string", "=>", (s) => s.trim()] },
314
- b: { a: "boolean" },
315
- c: { b: "boolean" }
316
- })
317
- const t = $.type("a|b")
318
- attest<
319
- Type<
320
- | {
321
- a: (In: string) => Out<string>
322
- }
323
- | {
324
- a: boolean
325
- }
326
- >
327
- >(t)
328
-
329
- attest(() => {
330
- scope({
331
- a: { a: ["string", "=>", (s) => s.trim()] },
332
- b: { b: "boolean" },
333
- c: "a|b"
334
- }).export()
335
- }).throws(writeUndiscriminableMorphUnionMessage("/"))
336
- })
337
- it("array double intersection", () => {
338
- // attest(() => {
339
- // scope({
340
- // a: { a: ["number>0", "=>", (data) => data + 1] },
341
- // b: { a: ["number>0", "=>", (data) => data + 2] },
342
- // c: "a[]&b[]"
343
- // }).export()
344
- // }).throws(
345
- // "At [index]/a: Intersection of morphs results in an unsatisfiable type"
346
- // )
347
- })
348
- it("undiscriminated morph at path", () => {
349
- attest(() => {
350
- scope({
351
- a: { a: ["string", "=>", (s) => s.trim()] },
352
- b: { b: "boolean" },
353
- c: { key: "a|b" }
354
- }).export()
355
- }).throws(writeUndiscriminableMorphUnionMessage("key"))
356
- })
357
- it("helper morph intersection", () => {
358
- attest(() =>
359
- type("string")
360
- .morph((s) => s.length)
361
- .and(type("string").morph((s) => s.length))
362
- ).throws("Intersection of morphs results in an unsatisfiable type")
363
- })
364
- it("union helper undiscriminated", () => {
365
- attest(() =>
366
- type("string")
367
- .morph((s) => s.length)
368
- .or("'foo'")
369
- ).throws(writeUndiscriminableMorphUnionMessage("/"))
370
- })
371
- // it("problem not included in return", () => {
372
- // const parsedInt = type([
373
- // "string",
374
- // "=>",
375
- // (s, errors) => {
376
- // const result = parseInt(s)
377
- // if (Number.isNaN(result)) {
378
- // return errors.mustBe("an integer string", s, [])
379
- // }
380
- // return result
381
- // }
382
- // ])
383
- // attest<Type<(In: string) => Out<number>>>(parsedInt)
384
- // attest(parsedInt("5").out).snap(5)
385
- // attest(parsedInt("five").errors?.summary).snap(
386
- // "Must be an integer string (was 'five')"
387
- // )
388
- // })
389
- it("nullable return", () => {
390
- const toNullableNumber = type(["string", "=>", (s) => s.length || null])
391
- attest<Type<(In: string) => Out<number | null>>>(toNullableNumber)
392
- })
393
- it("undefinable return", () => {
394
- const toUndefinableNumber = type([
395
- "string",
396
- "=>",
397
- (s) => s.length || undefined
398
- ])
399
- attest<Type<(In: string) => Out<number | undefined>>>(toUndefinableNumber)
400
- })
401
- it("null or undefined return", () => {
402
- const toMaybeNumber = type([
403
- "string",
404
- "=>",
405
- (s) => (s.length === 0 ? undefined : s.length === 1 ? null : s.length)
406
- ])
407
- attest<Type<(In: string) => Out<number | null | undefined>>>(toMaybeNumber)
408
- })
409
- })