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,64 +1,133 @@
1
- import { attest } from "@arktype/attest"
2
- import { registry } from "@arktype/schema"
3
- import { printable } from "@arktype/util"
4
- import { scope, type } from "arktype"
1
+ import { attest, contextualize } from "@arktype/attest"
5
2
  import {
6
3
  writeInvalidPropertyKeyMessage,
7
- writeInvalidSpreadTypeMessage
8
- } from "../parser/objectLiteral.js"
9
- import { writeUnresolvableMessage } from "../parser/string/shift/operand/unenclosed.js"
10
- import { writeUnboundableMessage } from "../parser/string/shift/operator/bounds.js"
11
-
12
- describe("object literal", () => {
13
- it("empty", () => {
14
- const o = type({})
15
- attest(o.json).equals(type("object").json)
16
- })
17
- it("required", () => {
18
- const o = type({ a: "string", b: "number" })
19
- attest<{ a: string; b: number }>(o.infer)
20
- attest(o.json).snap({
21
- basis: "object",
22
- required: [
23
- { key: "a", value: "string" },
24
- { key: "b", value: "number" }
25
- ]
4
+ writeUnboundableMessage,
5
+ writeUnresolvableMessage
6
+ } from "@arktype/schema"
7
+ import { printable, registeredReference } from "@arktype/util"
8
+ import { scope, type } from "arktype"
9
+ import { writeInvalidSpreadTypeMessage } from "../parser/objectLiteral.js"
10
+
11
+ contextualize(
12
+ "named",
13
+ () => {
14
+ it("empty", () => {
15
+ const o = type({})
16
+ attest(o.json).equals(type("object").json)
26
17
  })
27
- })
28
- it("optional keys", () => {
29
- const o = type({ "a?": "string", b: "number" })
30
- attest<{ a?: string; b: number }>(o.infer)
31
- attest(o.json).snap({
32
- basis: "object",
33
- optional: [{ key: "a", value: "string" }],
34
- required: [{ key: "b", value: "number" }]
18
+
19
+ it("required", () => {
20
+ const o = type({ a: "string", b: "number" })
21
+ attest<{ a: string; b: number }>(o.infer)
22
+ attest(o.json).snap({
23
+ domain: "object",
24
+ prop: [
25
+ { key: "a", value: "string" },
26
+ { key: "b", value: "number" }
27
+ ]
28
+ })
35
29
  })
36
- })
37
- it("symbol key", () => {
38
- const s = Symbol()
39
- const name = registry.register(s)
40
- const t = type({
41
- [s]: "string"
30
+
31
+ it("optional keys", () => {
32
+ const o = type({ "a?": "string", b: "number" })
33
+ attest<{ a?: string; b: number }>(o.infer)
34
+ attest(o.json).snap({
35
+ domain: "object",
36
+ prop: [
37
+ { key: "a", optional: true, value: "string" },
38
+ { key: "b", value: "number" }
39
+ ]
40
+ })
42
41
  })
43
- attest<{ [s]: string }>(t.infer)
44
- attest(t.json).equals({
45
- basis: "object",
46
- required: [{ key: name, value: "string" }]
42
+
43
+ it("symbol key", () => {
44
+ const s = Symbol()
45
+ const name = registeredReference(s)
46
+ const t = type({
47
+ [s]: "string"
48
+ })
49
+ attest<{ [s]: string }>(t.infer)
50
+ attest(t.json).snap({
51
+ domain: "object",
52
+ prop: [{ key: name, value: "string" }]
53
+ })
54
+ })
55
+
56
+ it("error in obj that has tuple that writes error at proper path", () => {
57
+ // @ts-expect-error
58
+ attest(() => type({ "a?": ["string", ["stringx", "?"]] }))
59
+ .throws(writeUnresolvableMessage("stringx"))
60
+ .type.errors.snap(
61
+ "Type '\"stringx\"' is not assignable to type '\"'stringx' is unresolvable \"'."
62
+ )
47
63
  })
48
- })
49
- it("optional symbol", () => {
50
- const s = Symbol()
51
- const name = registry.register(s)
52
- const t = type({
53
- [s]: "number?"
64
+
65
+ it("nested", () => {
66
+ const t = type({ "a?": { b: "boolean" } })
67
+ attest<{ a?: { b: boolean } }>(t.infer)
54
68
  })
55
- attest<{ [s]?: number }>(t.infer)
56
- attest(t.json).equals({
57
- basis: "object",
58
- optional: [{ key: name, value: "number" }]
69
+
70
+ it("intersections", () => {
71
+ const a = { "a?": "string" } as const
72
+ const b = { b: "string" } as const
73
+ const c = { "c?": "string" } as const
74
+ const abc = type(a).and(b).and(c)
75
+ attest<{ a?: string; b: string; c?: string }>(abc.infer)
76
+ attest(abc.json).equals(type({ ...a, ...b, ...c }).json)
77
+ attest(abc.json).equals(type([[a, "&", b], "&", c]).json)
78
+ })
79
+
80
+ it("intersection", () => {
81
+ const t = type({ a: "number" }).and({ b: "boolean" })
82
+ // Should be simplified from {a: number} & {b: boolean} to {a: number, b: boolean}
83
+ attest(t.infer).type.toString.snap("{ a: number; b: boolean; }")
84
+ attest(t.json).equals(type({ a: "number", b: "boolean" }).json)
59
85
  })
60
- })
61
- describe("spread syntax", () => {
86
+
87
+ it("escaped optional token", () => {
88
+ const t = type({ "a\\?": "string" })
89
+ attest<{ "a?": string }>(t.infer)
90
+ })
91
+
92
+ it("traverse optional", () => {
93
+ const o = type({ "a?": "string" })
94
+ attest(o({ a: "a" })).snap({ a: "a" })
95
+ attest(o({})).snap({})
96
+ attest(o({ a: 1 }).toString()).snap("a must be a string (was number)")
97
+ })
98
+
99
+ // it("traverse strict optional", () => {
100
+ // // TODO: strict
101
+ // const o = type({ "a?": "string" })
102
+ // attest(o({ a: "a" })).snap({ a: "a" })
103
+ // attest(o({})).snap({})
104
+ // attest(o({ a: 1 }).toString()).snap("a must be a string (was number)")
105
+ // })
106
+
107
+ // it("multiple bad strict", () => {
108
+ // const t = type({ a: "string", b: "boolean" }).configure({
109
+ // keys: "strict"
110
+ // })
111
+ // attest(t({ a: 1, b: 2 }).toString()).snap(
112
+ // "a must be a string (was number)\nb must be boolean (was number)"
113
+ // )
114
+ // })
115
+
116
+ // it("optional symbol", () => {
117
+ // const s = Symbol()
118
+ // const name = reference(s)
119
+ // const t = type({
120
+ // [optional(s)]: "number"
121
+ // })
122
+ // attest<{ [s]?: number }>(t.infer)
123
+ // attest(t.json).equals({
124
+ // domain: "object",
125
+ // optional: [{ key: name, value: "number" }]
126
+ // })
127
+ // })
128
+ },
129
+ "spread syntax",
130
+ () => {
62
131
  it("within scope", () => {
63
132
  const s = scope({
64
133
  user: { isAdmin: "false", name: "string" },
@@ -67,8 +136,8 @@ describe("object literal", () => {
67
136
 
68
137
  attest<{ isAdmin: true; name: string }>(s.admin.infer)
69
138
  attest(s.admin.json).equals({
70
- basis: "object",
71
- required: [
139
+ domain: "object",
140
+ prop: [
72
141
  { key: "isAdmin", value: { unit: true } },
73
142
  { key: "name", value: "string" }
74
143
  ]
@@ -80,9 +149,9 @@ describe("object literal", () => {
80
149
  const admin = type({ "...": user, isAdmin: "true" })
81
150
 
82
151
  attest<{ isAdmin: true; name: string }>(admin.infer)
83
- attest(admin.json).equals({
84
- basis: "object",
85
- required: [
152
+ attest(admin.json).snap({
153
+ domain: "object",
154
+ prop: [
86
155
  { key: "isAdmin", value: { unit: true } },
87
156
  { key: "name", value: "string" }
88
157
  ]
@@ -105,9 +174,12 @@ describe("object literal", () => {
105
174
  }>(t.infer)
106
175
 
107
176
  attest(t.json).snap({
108
- basis: "object",
109
- required: [
110
- { key: "inherited", value: [{ unit: false }, { unit: true }] },
177
+ domain: "object",
178
+ prop: [
179
+ {
180
+ key: "inherited",
181
+ value: [{ unit: false }, { unit: true }]
182
+ },
111
183
  { key: "overridden", value: "number" }
112
184
  ]
113
185
  })
@@ -120,9 +192,9 @@ describe("object literal", () => {
120
192
 
121
193
  attest<{ "...": string }>(t.infer)
122
194
 
123
- attest(t.json).equals({
124
- basis: "object",
125
- required: [{ key: "...", value: "string" }]
195
+ attest(t.json).snap({
196
+ domain: "object",
197
+ prop: [{ key: "...", value: "string" }]
126
198
  })
127
199
  })
128
200
 
@@ -142,159 +214,229 @@ describe("object literal", () => {
142
214
 
143
215
  attest<{ isAdmin: true; name: string }>(adminUser.infer)
144
216
  attest(adminUser.json).snap({
145
- basis: "object",
146
- required: [
217
+ domain: "object",
218
+ prop: [
147
219
  { key: "isAdmin", value: { unit: true } },
148
220
  { key: "name", value: "string" }
149
221
  ]
150
222
  })
151
223
  })
152
- })
153
- describe("optional keys and definition reduction", () => {
154
- it("optional value", () => {
155
- const t = type({ a: "string?" })
156
- attest(t.json).equals(type({ "a?": "string" }).json)
157
- })
158
- it("optional key and value", () => {
159
- const t = type({ "a?": "string?" })
160
- attest(t.json).equals(type({ "a?": "string" }).json)
161
- })
162
- it("optional value as tuple", () => {
163
- const t = type({ a: ["string", "?"] })
164
- attest(t.json).equals(type({ "a?": "string" }).json)
224
+ },
225
+ "index",
226
+ () => {
227
+ it("string index", () => {
228
+ const o = type({ "[string]": "string" })
229
+ attest<{ [x: string]: string }>(o.infer)
230
+ attest(o.json).snap({
231
+ domain: "object",
232
+ index: [{ key: "string", value: "string" }]
233
+ })
234
+
235
+ attest(o({})).equals({})
236
+ attest(o({ a: "a", b: "b" })).equals({ a: "a", b: "b" })
237
+
238
+ const validWithSymbol = { a: "a", [Symbol()]: null }
239
+ attest(validWithSymbol).equals(validWithSymbol)
240
+
241
+ attest(o({ a: 1 }).toString()).snap("a must be a string (was number)")
242
+ attest(o({ a: true, b: false }).toString())
243
+ .snap(`a must be a string (was true)
244
+ b must be a string (was false)`)
165
245
  })
166
- })
167
- it("error in obj that has tuple that writes error at proper path", () => {
168
- // @ts-expect-error
169
- attest(() => type({ "a?": ["string", "string?", ["stringx", "?"]] }))
170
- .throws(writeUnresolvableMessage("stringx"))
171
- .type.errors.snap(
172
- "Type '\"stringx\"' is not assignable to type '\"'stringx' is unresolvable \"'."
246
+
247
+ it("symbol index", () => {
248
+ const o = type({ "[symbol]": "1" })
249
+ attest<{ [x: symbol]: 1 }>(o.infer)
250
+ attest(o.json).snap({
251
+ domain: "object",
252
+ index: [{ key: "symbol", value: { unit: 1 } }]
253
+ })
254
+
255
+ attest(o({})).equals({})
256
+
257
+ attest(o({ a: 999 })).unknown.snap({ a: 999 })
258
+
259
+ const zildjian = Symbol()
260
+ const zildjianName = printable(zildjian)
261
+
262
+ // I've been dope, suspenseful with a pencil
263
+ // Ever since...
264
+ const prince = Symbol()
265
+ const princeName = printable(prince)
266
+
267
+ attest(o({ [zildjian]: 1, [prince]: 1 })).equals({
268
+ [zildjian]: 1,
269
+ [prince]: 1
270
+ })
271
+
272
+ attest({ a: 0, [zildjian]: 1 }).equals({ a: 0, [zildjian]: 1 })
273
+
274
+ attest(o({ [zildjian]: 0 }).toString()).equals(
275
+ `value at [${zildjianName}] must be 1 (was 0)`
173
276
  )
174
- })
175
- it("index", () => {
176
- const o = type({ "[string]": "string" })
177
- attest<{ [x: string]: string }>(o.infer)
178
- })
179
- it("enumerable indexed union", () => {
180
- const o = type({ "['foo' | 'bar']": "string" })
181
- attest<{ foo: string; bar: string }>(o.infer)
182
- })
183
- it("non-enumerable indexed union", () => {
184
- const o = type({ "[string | symbol]": "string" })
185
- attest<{ [x: string]: string; [x: symbol]: string }>(o.infer)
186
- })
187
- it("multiple indexed", () => {
188
- const o = type({
189
- "[string]": "string",
190
- "[symbol]": "number"
277
+ attest(o({ [prince]: null, [zildjian]: undefined }).toString())
278
+ .snap(`value at [${princeName}] must be 1 (was null)
279
+ value at [${zildjianName}] must be 1 (was undefined)`)
191
280
  })
192
- attest<{ [x: string]: string; [x: symbol]: number }>(o.infer)
193
- })
194
- it("all key kinds", () => {
195
- const o = type({
196
- "[string]": "string",
197
- required: "'foo'",
198
- "optional?": "'bar'"
281
+
282
+ it("enumerable indexed union", () => {
283
+ const o = type({ "['foo' | 'bar']": "string" })
284
+ const expected = type({ foo: "string", bar: "string" })
285
+ attest<typeof expected>(o)
286
+ attest(o.json).equals(expected.json)
199
287
  })
200
- attest<{ [x: string]: string; required: "foo"; optional?: "bar" }>(o.infer)
201
- })
202
- it("index key from scope", () => {
203
- const types = scope({
204
- key: "symbol|'foo'|'bar'|'baz'",
205
- obj: {
206
- "[key]": "string"
207
- }
208
- }).export()
209
- type Key = symbol | "foo" | "bar" | "baz"
210
- attest<Key>(types.key.infer)
211
- attest<Record<Key, string>>(types.obj.infer)
212
- })
213
- it("syntax error in index definition", () => {
214
- attest(() =>
215
- type({
216
- // @ts-expect-error
217
- "[unresolvable]": "string"
288
+
289
+ it("non-enumerable indexed union", () => {
290
+ const o = type({ "[string | symbol]": "string" })
291
+ attest<{ [x: string]: string; [x: symbol]: string }>(o.infer)
292
+ attest(o.json).snap({
293
+ domain: "object",
294
+ index: [{ key: ["string", "symbol"], value: "string" }]
218
295
  })
219
- ).throwsAndHasTypeError(writeUnresolvableMessage("unresolvable"))
220
- })
221
-
222
- it("does not allow syntax error message as value", () => {
223
- attest(() =>
224
- type({
225
- // @ts-expect-error
226
- "[unresolvable]": "'unresolvable' is unresolvable"
296
+ })
297
+
298
+ it("multiple indexed", () => {
299
+ const o = type({
300
+ "[string]": "string",
301
+ "[symbol]": "number"
227
302
  })
228
- ).throwsAndHasTypeError(writeUnresolvableMessage("unresolvable"))
229
- })
230
-
231
- it("semantic error in index definition", () => {
232
- attest(() =>
233
- type({
234
- // @ts-expect-error
235
- "[symbol<5]": "string"
303
+ attest<{ [x: string]: string; [x: symbol]: number }>(o.infer)
304
+ attest(o.json).snap({
305
+ domain: "object",
306
+ index: [
307
+ { key: "string", value: "string" },
308
+ { key: "symbol", value: "number" }
309
+ ]
236
310
  })
237
- ).throwsAndHasTypeError(writeUnboundableMessage("symbol"))
238
- })
239
-
240
- it("invalid key type for index definition", () => {
241
- attest(() =>
242
- type({
243
- // @ts-expect-error
244
- "[object]": "string"
311
+
312
+ attest(o({})).equals({})
313
+ attest(o({ foo: "f" })).equals({ foo: "f" })
314
+
315
+ const sym = Symbol()
316
+
317
+ const symName = printable(sym)
318
+
319
+ const validWithStringsAndSymbols = {
320
+ str: "string",
321
+ [sym]: 8675309
322
+ }
323
+
324
+ attest(o(validWithStringsAndSymbols)).equals(validWithStringsAndSymbols)
325
+
326
+ attest(
327
+ o({
328
+ str: 100,
329
+ [sym]: "💯"
330
+ }).toString()
331
+ ).snap(`str must be a string (was number)
332
+ value at [${symName}] must be a number (was string)`)
333
+ })
334
+
335
+ it("all key kinds", () => {
336
+ const o = type({
337
+ "[string]": "string",
338
+ required: "'foo'",
339
+ "optional?": "'bar'"
340
+ })
341
+ attest<{ [x: string]: string; required: "foo"; optional?: "bar" }>(
342
+ o.infer
343
+ )
344
+ attest(o.json).snap({
345
+ domain: "object",
346
+ prop: [
347
+ { key: "optional", optional: true, value: { unit: "bar" } },
348
+ { key: "required", value: { unit: "foo" } }
349
+ ],
350
+ index: [{ key: "string", value: "string" }]
245
351
  })
246
- ).throwsAndHasTypeError(writeInvalidPropertyKeyMessage("object"))
247
- })
248
-
249
- it("does not allow invalid key type error as value", () => {
250
- attest(() =>
251
- type({
252
- // @ts-expect-error
253
- "[object]":
254
- "Indexed key definition 'object' must be a string, number or symbol"
352
+
353
+ const valid: typeof o.infer = { required: "foo", other: "bar" }
354
+ attest(o(valid)).equals(valid)
355
+ attest(
356
+ o({
357
+ optional: "wrongString",
358
+ other: 0n
359
+ }).toString()
360
+ ).snap(`optional must be "bar" (was "wrongString")
361
+ required must be "foo" (was missing)
362
+ other must be a string (was bigint)`)
363
+ })
364
+
365
+ it("index key from scope", () => {
366
+ const types = scope({
367
+ key: "symbol|'foo'|'bar'|'baz'",
368
+ obj: {
369
+ "[key]": "string"
370
+ }
371
+ }).export()
372
+ type Key = symbol | "foo" | "bar" | "baz"
373
+ attest<Key>(types.key.infer)
374
+ attest<Record<Key, string>>(types.obj.infer)
375
+
376
+ const expected = type({ "[symbol]": "string" }).and({
377
+ foo: "string",
378
+ bar: "string",
379
+ baz: "string"
380
+ })
381
+
382
+ attest(types.obj.json).snap(expected.json)
383
+ })
384
+
385
+ it("syntax error in index definition", () => {
386
+ attest(() =>
387
+ type({
388
+ // @ts-expect-error
389
+ "[unresolvable]": "string"
390
+ })
391
+ ).throwsAndHasTypeError(writeUnresolvableMessage("unresolvable"))
392
+ })
393
+
394
+ it("does not allow syntax error message as value", () => {
395
+ attest(() =>
396
+ type({
397
+ // @ts-expect-error
398
+ "[unresolvable]": "'unresolvable' is unresolvable"
399
+ })
400
+ ).throwsAndHasTypeError(writeUnresolvableMessage("unresolvable"))
401
+ })
402
+
403
+ it("semantic error in index definition", () => {
404
+ attest(() =>
405
+ type({
406
+ // @ts-expect-error
407
+ "[symbol<5]": "string"
408
+ })
409
+ ).throwsAndHasTypeError(writeUnboundableMessage("symbol"))
410
+ })
411
+
412
+ it("invalid key type for index definition", () => {
413
+ attest(() =>
414
+ type({
415
+ // @ts-expect-error
416
+ "[object]": "string"
417
+ })
418
+ ).throwsAndHasTypeError(writeInvalidPropertyKeyMessage("object"))
419
+ })
420
+
421
+ it("does not allow invalid key type error as value", () => {
422
+ attest(() =>
423
+ type({
424
+ // @ts-expect-error
425
+ "[object]":
426
+ "Indexed key definition 'object' must be a string, number or symbol"
427
+ })
428
+ )
429
+ .throws(writeUnresolvableMessage("Indexed"))
430
+ .type.errors(writeInvalidPropertyKeyMessage("object"))
431
+ })
432
+
433
+ it("escaped index", () => {
434
+ const o = type({ "\\[string]": "string" })
435
+ attest<{ "[string]": string }>(o.infer)
436
+ attest(o.json).snap({
437
+ domain: "object",
438
+ prop: [{ key: "[string]", value: "string" }]
255
439
  })
256
- ).throwsAndHasTypeError(writeInvalidPropertyKeyMessage("object"))
257
- })
258
-
259
- it("nested", () => {
260
- const t = type({ "a?": { b: "boolean" } })
261
- attest<{ a?: { b: boolean } }>(t.infer)
262
- })
263
- it("intersections", () => {
264
- const a = { "a?": "string" } as const
265
- const b = { b: "string" } as const
266
- const c = { "c?": "string" } as const
267
- const abc = type(a).and(b).and(c)
268
- attest<{ a?: string; b: string; c?: string }>(abc.infer)
269
- attest(abc.json).equals(type({ ...a, ...b, ...c }).json)
270
- attest(abc.json).equals(type([[a, "&", b], "&", c]).json)
271
- })
272
- it("traverse optional", () => {
273
- const o = type({ "a?": "string" }).configure({ keys: "strict" })
274
- attest(o({ a: "a" }).out).snap({ a: "a" })
275
- attest(o({}).out).snap({})
276
- attest(o({ a: 1 }).errors?.summary).snap("a must be a string (was number)")
277
- })
278
- it("intersection", () => {
279
- const t = type({ a: "number" }).and({ b: "boolean" })
280
- // Should be simplified from {a: number} & {b: boolean} to {a: number, b: boolean}
281
- attest(t.infer).type.toString.snap("{ a: number; b: boolean; }")
282
- attest(t.json).equals(type({ a: "number", b: "boolean" }).json)
283
- })
284
- it("escaped optional token", () => {
285
- const t = type({ "a\\?": "string" })
286
- attest<{ "a?": string }>(t.infer)
287
- })
288
- it("escaped index", () => {
289
- const o = type({ "\\[string]": "string" })
290
- attest<{ "[string]": string }>(o.infer)
291
- })
292
- it("multiple bad strict", () => {
293
- const t = type({ a: "string", b: "boolean" }).configure({
294
- keys: "strict"
295
440
  })
296
- attest(t({ a: 1, b: 2 }).errors?.summary).snap(
297
- "a must be a string (was number)\nb must be boolean (was number)"
298
- )
299
- })
300
- })
441
+ }
442
+ )
@@ -106,25 +106,25 @@ bench("divisor", () => {
106
106
  .types([1023, "instantiations"])
107
107
 
108
108
  bench("filter-tuple", () => {
109
- const _ = type(["boolean", ":", (b) => b])
109
+ const _ = type(["boolean", ":", b => b])
110
110
  })
111
111
  .median([8.04, "us"])
112
112
  .types([583, "instantiations"])
113
113
 
114
114
  bench("filter-chain", () => {
115
- const _ = type("boolean").narrow((b) => b)
115
+ const _ = type("boolean").narrow(b => b)
116
116
  })
117
117
  .median([12.19, "us"])
118
118
  .types([42, "instantiations"])
119
119
 
120
120
  bench("morph-tuple", () => {
121
- const _ = type(["boolean", "=>", (b) => b])
121
+ const _ = type(["boolean", "=>", b => b])
122
122
  })
123
123
  .median([8.96, "us"])
124
124
  .types([562, "instantiations"])
125
125
 
126
126
  bench("morph-chain", () => {
127
- const _ = type("boolean").morph((b) => b)
127
+ const _ = type("boolean").pipe(b => b)
128
128
  })
129
129
  .median([2.98, "us"])
130
130
  .types([26, "instantiations"])