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,102 +0,0 @@
1
- import { attest } from "@arktype/attest"
2
- import type { Out, is } from "@arktype/schema"
3
- import type { equals } from "@arktype/util"
4
- import { type, type Type } from "arktype"
5
-
6
- describe("narrow", () => {
7
- it("implicit problem", () => {
8
- const isOdd = (n: number) => n % 2 === 1
9
- const odd = type(["number", ":", isOdd])
10
- 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
- )
16
- })
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)")
20
- })
21
- it("explicit problem", () => {
22
- const even = type([
23
- "number",
24
- ":",
25
- (n, ctx) => n % 3 === 0 || ctx.invalid({ expected: "divisible by 3" })
26
- ])
27
- attest(even(1).errors?.summary).snap("Must be divisible by 3 (was 1)")
28
- })
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
- it("functional predicate", () => {
54
- const one = type(["number", ":", (n): n is 1 => n === 1])
55
- attest<1>(one.infer)
56
- })
57
- it("functional parameter inference", () => {
58
- type Expected = number | boolean[]
59
- const validateNumberOrBooleanList = <t>(
60
- t: equals<t, Expected> extends true ? t : Expected
61
- ) => true
62
- attest<number | boolean[]>(
63
- type([
64
- "number|boolean[]",
65
- ":",
66
- (data) => validateNumberOrBooleanList(data)
67
- ]).infer
68
- )
69
- attest(() => {
70
- // @ts-expect-error
71
- type(["number|boolean[]", ":", (data: number | string[]) => !!data])
72
- }).type.errors("Type 'boolean' is not assignable to type 'string'.")
73
- })
74
- it("narrow problem", () => {
75
- const palindrome = type([
76
- "string",
77
- ":",
78
- (s, ctx) =>
79
- s === [...s].reverse().join("") ? true : ctx.invalid("a palindrome")
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')"
85
- )
86
- })
87
- it("narrows the output type of a morph", () => {
88
- const t = type("string")
89
- .morph((s) => s.length)
90
- .narrow((n): n is 5 => n === 5)
91
- attest<Type<(In: string) => Out<is<5, { anonymousPredicate: true }>>>>(t)
92
- attest(t.json).snap({ domain: "string" })
93
- })
94
- it("expression", () => {
95
- const t = type("string", ":", (s): s is `f${string}` => s[0] === "f")
96
- attest<`f${string}`>(t.infer)
97
- attest(t.json).snap({
98
- basis: "string",
99
- predicate: ["$ark.anonymousFunction8"]
100
- })
101
- })
102
- })
@@ -1,190 +0,0 @@
1
- // describe("node definitions", () => {
2
- // describe("basis", () => {
3
- // it("domain", () => {
4
- // const t = node({
5
- // basis: "string"
6
- // })
7
- // attest<TypeNode<string>>(t)
8
- // })
9
- // it("class", () => {
10
- // const t = node({
11
- // basis: Date
12
- // })
13
- // attest<TypeNode<Date>>(t)
14
- // })
15
- // it("value", () => {
16
- // const t = node({
17
- // basis: ["===", 3.14159]
18
- // })
19
- // attest<TypeNode<3.14159>>(t)
20
- // })
21
- // })
22
- // it("optional props", () => {
23
- // const t = node({
24
- // basis: "object",
25
- // props: {
26
- // a: {
27
- // value: { basis: "string" }
28
- // },
29
- // b: {
30
- // optional: true,
31
- // value: { basis: "number" }
32
- // }
33
- // }
34
- // })
35
- // attest<TypeNode<{>(t)
36
- // a: string
37
- // b?: boolean
38
- // }>
39
- // })
40
- // it("arrays", () => {
41
- // const t = node({
42
- // basis: Array,
43
- // props: [
44
- // {},
45
- // {
46
- // key: arrayIndexInput(),
47
- // value: {
48
- // basis: "object",
49
- // props: {
50
- // name: {
51
- // value: { basis: "string" }
52
- // }
53
- // }
54
- // }
55
- // }
56
- // ]
57
- // })
58
- // attest<TypeNode<{ name: string }[]>>(t)
59
- // })
60
- // it("variadic tuple", () => {
61
- // const t = node({
62
- // basis: Array,
63
- // props: [
64
- // {
65
- // 0: {
66
- // value: { basis: "string" }
67
- // },
68
- // // works for numeric or string keys
69
- // "1": {
70
- // value: { basis: "number" }
71
- // }
72
- // },
73
- // {
74
- // key: arrayIndexInput(2),
75
- // value: {
76
- // basis: "symbol"
77
- // }
78
- // }
79
- // ]
80
- // })
81
- // attest<TypeNode<[string, number, ...symbol[]]>>(t)
82
- // })
83
- // it("non-variadic tuple", () => {
84
- // const t = node({
85
- // basis: Array,
86
- // props: {
87
- // 0: {
88
- // value: {
89
- // basis: "object",
90
- // props: {
91
- // a: { value: { basis: "string" } },
92
- // b: { value: { basis: "number" } }
93
- // }
94
- // }
95
- // },
96
- // 1: {
97
- // value: {
98
- // basis: ["===", "arktype"]
99
- // }
100
- // },
101
- // length: {
102
- // prerequisite: true,
103
- // value: { basis: ["===", 2] }
104
- // }
105
- // }
106
- // })
107
- // attest<TypeNode<>(t)
108
- // [
109
- // {
110
- // a: string
111
- // b: number
112
- // },
113
- // "arktype"
114
- // ]
115
- // >
116
- // })
117
- // it("branches", () => {
118
- // const t = node(
119
- // { basis: ["===", "foo"] },
120
- // { basis: ["===", "bar"] },
121
- // { basis: "number" },
122
- // {
123
- // basis: "object",
124
- // props: { a: { value: { basis: "bigint" } } }
125
- // }
126
- // )
127
- // attest<TypeNode<number | "foo" | "bar" | { a: bigint }>>(t)
128
- // })
129
- // it("narrow", () => {
130
- // const t = node({
131
- // basis: "string",
132
- // narrow: (s): s is "foo" => s === "foo"
133
- // })
134
- // attest<TypeNode<"foo">>(t)
135
- // })
136
- // it("narrow array", () => {
137
- // const t = node({
138
- // basis: "object",
139
- // narrow: [
140
- // (o): o is { a: string } => typeof o.a === "string",
141
- // (o): o is { b: boolean } => typeof o.b === "boolean"
142
- // ] as const
143
- // })
144
- // attest<TypeNode<{>(t)
145
- // a: string
146
- // b: boolean
147
- // }>
148
- // })
149
- // it("morph", () => {
150
- // const t = node({
151
- // basis: "string",
152
- // morph: (s: string) => s.length
153
- // })
154
- // attest<TypeNode<(In: string) => Out<number>>>(t)
155
- // })
156
- // it("morph list", () => {
157
- // const t = node({
158
- // basis: "string",
159
- // morph: [(s: string) => s.length, (n: number) => ({ n })] as const
160
- // })
161
- // attest<TypeNode<(In: string) => Out<{ n: number }>>>(t)
162
- // })
163
- // it("never", () => {
164
- // const t = node()
165
- // attest<TypeNode<never>>(t)
166
- // })
167
- // it("errors on rule in wrong domain", () => {
168
- // attest(() =>
169
- // node({
170
- // basis: "number",
171
- // divisor: 5,
172
- // // @ts-expect-error
173
- // regex: "/.*/"
174
- // })
175
- // ).throws.snap(
176
- // "Error: regex constraint may only be applied to a string (was number)"
177
- // )
178
- // })
179
- // it("errors on filter literal", () => {
180
- // attest(() =>
181
- // node({
182
- // basis: ["===", true],
183
- // // @ts-expect-error
184
- // narrow: (b: boolean) => b === true
185
- // })
186
- // ).throws(
187
- // "narrow constraint may only be applied to a non-literal type (was true)"
188
- // )
189
- // })
190
- // })
@@ -1,28 +0,0 @@
1
- import { bench } from "@arktype/attest"
2
- import { type } from "arktype"
3
-
4
- bench("dictionary", () => {
5
- const dict = type({
6
- a: "string[]",
7
- b: "number[]",
8
- c: { nested: "boolean[]" }
9
- })
10
- })
11
- .median([20.83, "us"])
12
- .types([1704, "instantiations"])
13
-
14
- bench("dictionary with optional keys", () => {
15
- const dict = type({
16
- "a?": "string[]",
17
- "b?": "number[]",
18
- "c?": { "nested?": "boolean[]" }
19
- })
20
- })
21
- .median([21.23, "us"])
22
- .types([1704, "instantiations"])
23
-
24
- bench("tuple", () => {
25
- const tuple = type(["string[]", "number[]", ["boolean[]"]])
26
- })
27
- .median([28.6, "us"])
28
- .types([2739, "instantiations"])
@@ -1,301 +0,0 @@
1
- import { attest } from "@arktype/attest"
2
- import { printable, reference } from "@arktype/util"
3
- import { scope, type } from "arktype"
4
- import {
5
- writeInvalidPropertyKeyMessage,
6
- writeInvalidSpreadTypeMessage
7
- } from "../parser/objectLiteral.js"
8
- import { writeUnresolvableMessage } from "../parser/string/shift/operand/unenclosed.js"
9
- import { writeUnboundableMessage } from "../parser/string/shift/operator/bounds.js"
10
-
11
- describe("object literal", () => {
12
- it("empty", () => {
13
- const o = type({})
14
- attest(o.json).equals(type("object").json)
15
- })
16
- it("required", () => {
17
- const o = type({ a: "string", b: "number" })
18
- attest<{ a: string; b: number }>(o.infer)
19
- attest(o.json).snap({
20
- basis: "object",
21
- required: [
22
- { key: "a", value: "string" },
23
- { key: "b", value: "number" }
24
- ]
25
- })
26
- })
27
- it("optional keys", () => {
28
- const o = type({ "a?": "string", b: "number" })
29
- attest<{ a?: string; b: number }>(o.infer)
30
- attest(o.json).snap({
31
- basis: "object",
32
- optional: [{ key: "a", value: "string" }],
33
- required: [{ key: "b", value: "number" }]
34
- })
35
- })
36
- it("symbol key", () => {
37
- const s = Symbol()
38
- const name = reference(s)
39
- const t = type({
40
- [s]: "string"
41
- })
42
- attest<{ [s]: string }>(t.infer)
43
- attest(t.json).equals({
44
- basis: "object",
45
- required: [{ key: name, value: "string" }]
46
- })
47
- })
48
- it("optional symbol", () => {
49
- const s = Symbol()
50
- const name = reference(s)
51
- const t = type({
52
- [s]: "number?"
53
- })
54
- attest<{ [s]?: number }>(t.infer)
55
- attest(t.json).equals({
56
- basis: "object",
57
- optional: [{ key: name, value: "number" }]
58
- })
59
- })
60
- describe("spread syntax", () => {
61
- it("within scope", () => {
62
- const s = scope({
63
- user: { isAdmin: "false", name: "string" },
64
- admin: { "...": "user", isAdmin: "true" }
65
- }).export()
66
-
67
- attest<{ isAdmin: true; name: string }>(s.admin.infer)
68
- attest(s.admin.json).equals({
69
- basis: "object",
70
- required: [
71
- { key: "isAdmin", value: { unit: true } },
72
- { key: "name", value: "string" }
73
- ]
74
- })
75
- })
76
-
77
- it("from another `type` call", () => {
78
- const user = type({ isAdmin: "false", name: "string" })
79
- const admin = type({ "...": user, isAdmin: "true" })
80
-
81
- attest<{ isAdmin: true; name: string }>(admin.infer)
82
- attest(admin.json).equals({
83
- basis: "object",
84
- required: [
85
- { key: "isAdmin", value: { unit: true } },
86
- { key: "name", value: "string" }
87
- ]
88
- })
89
- })
90
-
91
- it("from an object literal", () => {
92
- // no idea why you'd want to do this
93
- const t = type({
94
- "...": {
95
- inherited: "boolean",
96
- overridden: "string"
97
- },
98
- overridden: "number"
99
- })
100
-
101
- attest<{
102
- inherited: boolean
103
- overridden: number
104
- }>(t.infer)
105
-
106
- attest(t.json).snap({
107
- basis: "object",
108
- required: [
109
- { key: "inherited", value: [{ unit: false }, { unit: true }] },
110
- { key: "overridden", value: "number" }
111
- ]
112
- })
113
- })
114
-
115
- it("escaped key", () => {
116
- const t = type({
117
- "\\...": "string"
118
- })
119
-
120
- attest<{ "...": string }>(t.infer)
121
-
122
- attest(t.json).equals({
123
- basis: "object",
124
- required: [{ key: "...", value: "string" }]
125
- })
126
- })
127
-
128
- it("with non-object", () => {
129
- // @ts-expect-error
130
- attest(() => type({ "...": "string" })).throwsAndHasTypeError(
131
- writeInvalidSpreadTypeMessage(printable("string"))
132
- )
133
- })
134
-
135
- // this is a regression test to ensure nodes are handled even if they aren't just an object
136
- it("with complex type", () => {
137
- const adminUser = type({
138
- "...": [{ name: "string" }, "&", { isAdmin: "false" }],
139
- isAdmin: "true"
140
- })
141
-
142
- attest<{ isAdmin: true; name: string }>(adminUser.infer)
143
- attest(adminUser.json).snap({
144
- basis: "object",
145
- required: [
146
- { key: "isAdmin", value: { unit: true } },
147
- { key: "name", value: "string" }
148
- ]
149
- })
150
- })
151
- })
152
- describe("optional keys and definition reduction", () => {
153
- it("optional value", () => {
154
- const t = type({ a: "string?" })
155
- attest(t.json).equals(type({ "a?": "string" }).json)
156
- })
157
- it("optional key and value", () => {
158
- const t = type({ "a?": "string?" })
159
- attest(t.json).equals(type({ "a?": "string" }).json)
160
- })
161
- it("optional value as tuple", () => {
162
- const t = type({ a: ["string", "?"] })
163
- attest(t.json).equals(type({ "a?": "string" }).json)
164
- })
165
- })
166
- it("error in obj that has tuple that writes error at proper path", () => {
167
- // @ts-expect-error
168
- attest(() => type({ "a?": ["string", "string?", ["stringx", "?"]] }))
169
- .throws(writeUnresolvableMessage("stringx"))
170
- .type.errors.snap(
171
- "Type '\"stringx\"' is not assignable to type '\"'stringx' is unresolvable \"'."
172
- )
173
- })
174
- it("index", () => {
175
- const o = type({ "[string]": "string" })
176
- attest<{ [x: string]: string }>(o.infer)
177
- })
178
- it("enumerable indexed union", () => {
179
- const o = type({ "['foo' | 'bar']": "string" })
180
- attest<{ foo: string; bar: string }>(o.infer)
181
- })
182
- it("non-enumerable indexed union", () => {
183
- const o = type({ "[string | symbol]": "string" })
184
- attest<{ [x: string]: string; [x: symbol]: string }>(o.infer)
185
- })
186
- it("multiple indexed", () => {
187
- const o = type({
188
- "[string]": "string",
189
- "[symbol]": "number"
190
- })
191
- attest<{ [x: string]: string; [x: symbol]: number }>(o.infer)
192
- })
193
- it("all key kinds", () => {
194
- const o = type({
195
- "[string]": "string",
196
- required: "'foo'",
197
- "optional?": "'bar'"
198
- })
199
- attest<{ [x: string]: string; required: "foo"; optional?: "bar" }>(o.infer)
200
- })
201
- it("index key from scope", () => {
202
- const types = scope({
203
- key: "symbol|'foo'|'bar'|'baz'",
204
- obj: {
205
- "[key]": "string"
206
- }
207
- }).export()
208
- type Key = symbol | "foo" | "bar" | "baz"
209
- attest<Key>(types.key.infer)
210
- attest<Record<Key, string>>(types.obj.infer)
211
- })
212
- it("syntax error in index definition", () => {
213
- attest(() =>
214
- type({
215
- // @ts-expect-error
216
- "[unresolvable]": "string"
217
- })
218
- ).throwsAndHasTypeError(writeUnresolvableMessage("unresolvable"))
219
- })
220
-
221
- it("does not allow syntax error message as value", () => {
222
- attest(() =>
223
- type({
224
- // @ts-expect-error
225
- "[unresolvable]": "'unresolvable' is unresolvable"
226
- })
227
- ).throwsAndHasTypeError(writeUnresolvableMessage("unresolvable"))
228
- })
229
-
230
- it("semantic error in index definition", () => {
231
- attest(() =>
232
- type({
233
- // @ts-expect-error
234
- "[symbol<5]": "string"
235
- })
236
- ).throwsAndHasTypeError(writeUnboundableMessage("symbol"))
237
- })
238
-
239
- it("invalid key type for index definition", () => {
240
- attest(() =>
241
- type({
242
- // @ts-expect-error
243
- "[object]": "string"
244
- })
245
- ).throwsAndHasTypeError(writeInvalidPropertyKeyMessage("object"))
246
- })
247
-
248
- it("does not allow invalid key type error as value", () => {
249
- attest(() =>
250
- type({
251
- // @ts-expect-error
252
- "[object]":
253
- "Indexed key definition 'object' must be a string, number or symbol"
254
- })
255
- ).throwsAndHasTypeError(writeInvalidPropertyKeyMessage("object"))
256
- })
257
-
258
- it("nested", () => {
259
- const t = type({ "a?": { b: "boolean" } })
260
- attest<{ a?: { b: boolean } }>(t.infer)
261
- })
262
- it("intersections", () => {
263
- const a = { "a?": "string" } as const
264
- const b = { b: "string" } as const
265
- const c = { "c?": "string" } as const
266
- const abc = type(a).and(b).and(c)
267
- attest<{ a?: string; b: string; c?: string }>(abc.infer)
268
- attest(abc.json).equals(type({ ...a, ...b, ...c }).json)
269
- attest(abc.json).equals(type([[a, "&", b], "&", c]).json)
270
- })
271
- // TODO: reenable
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
- // TODO: reenable
293
- // it("multiple bad strict", () => {
294
- // const t = type({ a: "string", b: "boolean" }).configure({
295
- // keys: "strict"
296
- // })
297
- // attest(t({ a: 1, b: 2 }).errors?.summary).snap(
298
- // "a must be a string (was number)\nb must be boolean (was number)"
299
- // )
300
- // })
301
- })
@@ -1,45 +0,0 @@
1
- import { bench } from "@arktype/attest"
2
- import { scope, type } from "arktype"
3
-
4
- bench("single-quoted", () => {
5
- const _ = type("'nineteen characters'")
6
- })
7
- .median([3.05, "us"])
8
- .types([502, "instantiations"])
9
-
10
- bench("double-quoted", () => {
11
- const _ = type('"nineteen characters"')
12
- })
13
- .median([3.13, "us"])
14
- .types([502, "instantiations"])
15
-
16
- bench("regex literal", () => {
17
- const _ = type("/nineteen characters/")
18
- })
19
- .median([4.18, "us"])
20
- .types([502, "instantiations"])
21
-
22
- bench("keyword", () => {
23
- const _ = type("string")
24
- })
25
- .median([1.44, "us"])
26
- .types([84, "instantiations"])
27
-
28
- const $ = scope({ strung: "string" })
29
- bench("alias", () => {
30
- const _ = $.type("strung")
31
- })
32
- .median([1.54, "us"])
33
- .types([725, "instantiations"])
34
-
35
- bench("number", () => {
36
- const _ = type("-98765.4321")
37
- })
38
- .median([4.41, "us"])
39
- .types([415, "instantiations"])
40
-
41
- bench("bigint", () => {
42
- const _ = type("-987654321n")
43
- })
44
- .median()
45
- .types()