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,144 +0,0 @@
1
- import { attest } from "@arktype/attest"
2
- import { scope } from "arktype"
3
-
4
- const getCyclicScope = () =>
5
- scope({
6
- package: {
7
- name: "string",
8
- "dependencies?": "package[]",
9
- "contributors?": "contributor[]"
10
- },
11
- contributor: {
12
- email: "email",
13
- "packages?": "package[]"
14
- }
15
- })
16
-
17
- type Package = ReturnType<typeof getCyclicScope>["infer"]["package"]
18
-
19
- const getCyclicData = () => {
20
- const packageData = {
21
- name: "arktype",
22
- dependencies: [{ name: "typescript" }],
23
- contributors: [{ email: "david@arktype.io" }]
24
- } satisfies Package
25
- packageData.dependencies.push(packageData)
26
- return packageData
27
- }
28
-
29
- describe("cyclic", () => {
30
- it("cyclic union", () => {
31
- const $ = scope({
32
- a: { b: "b|false" },
33
- b: { a: "a|true" }
34
- })
35
- attest($.infer).type.toString.snap(
36
- "{ a: { b: false | { a: true | any; }; }; b: { a: true | { b: false | any; }; }; }"
37
- )
38
- })
39
- it("cyclic intersection", () => {
40
- const $ = scope({
41
- a: { b: "b&a" },
42
- b: { a: "a&b" }
43
- })
44
- attest($.infer).type.toString.snap(
45
- "{ a: { b: { a: { b: any; a: any; }; b: any; }; }; b: { a: { b: { a: any; b: any; }; a: any; }; }; }"
46
- )
47
- })
48
- // TODO: reenable
49
- it("cyclic", () => {
50
- const types = scope({ a: { b: "b" }, b: { a: "a" } }).export()
51
- // attest(types.a.node).snap({
52
- // object: { props: { b: "b" } }
53
- // })
54
- // Type hint displays as "..." on hitting cycle (or any if "noErrorTruncation" is true)
55
- attest<{
56
- b: {
57
- a: {
58
- b: {
59
- a: any
60
- }
61
- }
62
- }
63
- }>(types.a.infer)
64
- attest<{
65
- b: {
66
- a: any
67
- }
68
- }>(types.b.infer.a.b.a.b.a.b.a)
69
-
70
- // @ts-expect-error
71
- attest(types.a.infer.b.a.b.c).type.errors.snap(
72
- `Property 'c' does not exist on type '{ a: { b: ...; }; }'.`
73
- )
74
- })
75
- it("allows valid", () => {
76
- const types = getCyclicScope().export()
77
- const data = getCyclicData()
78
- attest(types.package(data).out).snap({
79
- name: "arktype",
80
- dependencies: [{ name: "typescript" }, "(cycle)" as any as Package],
81
- contributors: [{ email: "david@arktype.io" }]
82
- })
83
- })
84
- it("adds errors on invalid", () => {
85
- const types = getCyclicScope().export()
86
- const data = getCyclicData()
87
- data.contributors[0].email = "ssalbdivad"
88
- attest(types.package(data).errors?.summary).snap(
89
- "dependencies/1/contributors/0/email must be a valid email (was 'ssalbdivad')\ncontributors/0/email must be a valid email (was 'ssalbdivad')"
90
- )
91
- })
92
- it("can include cyclic data in message", () => {
93
- const data = getCyclicData()
94
- const nonSelfDependent = getCyclicScope().type([
95
- "package",
96
- ":",
97
- (p) => !p.dependencies?.some((d) => d.name === p.name)
98
- ])
99
- attest(nonSelfDependent(data).errors?.summary).snap(
100
- 'Must be valid (was {"name":"arktype","dependencies":[{"name":"typescript"},"(cycle)"],"contributors":[{"email":"david@arktype.io"}]})'
101
- )
102
- })
103
-
104
- it("equivalent cycles reduce", () => {
105
- // // TODO: reduce this case or create an issue
106
- // const $ = scope({
107
- // user: {
108
- // friends: "user[]",
109
- // name: "string"
110
- // },
111
- // admin: {
112
- // friends: "user[]",
113
- // name: "string"
114
- // }
115
- // })
116
- })
117
-
118
- it("union cyclic reference", () => {
119
- const types = scope({
120
- a: {
121
- b: "b"
122
- },
123
- b: {
124
- a: "a|3"
125
- }
126
- })
127
- attest(types.infer).type.toString.snap(
128
- "{ a: { b: { a: 3 | any; }; }; b: { a: 3 | { b: any; }; }; }"
129
- )
130
- })
131
- it("intersect cyclic reference", () => {
132
- const types = scope({
133
- a: {
134
- b: "b"
135
- },
136
- b: {
137
- c: "a&b"
138
- }
139
- })
140
- attest(types.infer).type.toString.snap(
141
- "{ a: { b: { c: { b: any; c: any; }; }; }; b: { c: { b: any; c: any; }; }; }"
142
- )
143
- })
144
- })
@@ -1,64 +0,0 @@
1
- import { scope, type } from "arktype"
2
-
3
- const ab = { a: "1", b: 1 }
4
-
5
- describe("JSDoc", () => {
6
- // Note: `<#` are not checked, they are for understandability
7
- it("objects keep JSDoc", () => {
8
- type({
9
- /** JSDoc */
10
- a: "string"
11
- }).assert(ab).a // <# JSDoc
12
- })
13
- it("intersections keep JSDoc", () => {
14
- type([
15
- {
16
- /** JSDoc */
17
- a: "string"
18
- },
19
- "&",
20
- {
21
- b: "number"
22
- }
23
- ]).assert(ab).a // <# JSDoc
24
-
25
- type([
26
- {
27
- /** First */
28
- a: "string"
29
- },
30
- "&",
31
- {
32
- /** Second */
33
- a: "string"
34
- }
35
- ]).assert(ab).a // <# First
36
-
37
- scope({
38
- a: {
39
- /** JSDoc */
40
- a: "string"
41
- },
42
- b: {
43
- b: "number"
44
- },
45
- ab: "a & b"
46
- })
47
- .export()
48
- .ab.assert(ab).a // <# JSDoc
49
- })
50
-
51
- it("works with module scopes", () => {
52
- scope({
53
- /** JSDoc */
54
- a: "string"
55
- }).export().a // <# JSDoc
56
- })
57
-
58
- it("doesn't work with optional keys", () => {
59
- type({
60
- /** JSDoc */
61
- "a?": "string"
62
- }).assert(ab).a // <# ---
63
- })
64
- })
@@ -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
- })