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,104 +0,0 @@
1
- import { attest } from "@arktype/attest"
2
- import { writeIndivisibleMessage } from "@arktype/schema"
3
- import { type } from "arktype"
4
- import {
5
- writeMissingRightOperandMessage,
6
- writeUnresolvableMessage
7
- } from "../parser/string/shift/operand/unenclosed.js"
8
-
9
- describe("intersection", () => {
10
- it("two types", () => {
11
- const t = type("boolean&true")
12
- attest<true>(t.infer)
13
- attest(t.json).is(type("true").json)
14
- })
15
- it("intersection parsed before union", () => {
16
- // Should be parsed as:
17
- // 1. "0" | ("1"&"string") | "2"
18
- // 2. "0" | "1" | "2"
19
- const t = type("'0'|'1'&string|'2'")
20
- attest<"0" | "1" | "2">(t.infer)
21
- attest(t.json).equals(type("===", "0", "1", "2").json)
22
- })
23
- it("tuple expression", () => {
24
- const t = type([{ a: "string" }, "&", { b: "number" }])
25
- attest<{ a: string; b: number }>(t.infer)
26
- attest(t.json).equals(type({ a: "string", b: "number" }).json)
27
- })
28
- it("several types", () => {
29
- const t = type("unknown&boolean&false")
30
- attest<false>(t.infer)
31
- attest(t.json).equals(type("false").json)
32
- })
33
- it("method", () => {
34
- const t = type({ a: "string" }).and({ b: "boolean" })
35
- attest<{ a: string; b: boolean }>(t.infer)
36
- attest(t.json).equals(type({ a: "string", b: "boolean" }).json)
37
- })
38
- it("chained deep intersections", () => {
39
- const b = type({ b: "boolean" }, "=>", (o) => [o.b])
40
- const t = type({
41
- a: ["string", "=>", (s) => s.length]
42
- })
43
- .and({
44
- // unable to inline this due to:
45
- // https://github.com/arktypeio/arktype/issues/806
46
- b
47
- })
48
- .and({
49
- b: { b: "true" },
50
- c: "'hello'"
51
- })
52
- attest<{
53
- a: string
54
- b: {
55
- b: true
56
- }
57
- c: "hello"
58
- }>(t.in.infer)
59
-
60
- attest<{ a: number; b: boolean[]; c: "hello" }>(t.infer)
61
- })
62
- describe("errors", () => {
63
- it("bad reference", () => {
64
- // @ts-expect-error
65
- attest(() => type("boolean&tru"))
66
- .throws(writeUnresolvableMessage("tru"))
67
- .type.errors("boolean&true")
68
- })
69
- it("double and", () => {
70
- // @ts-expect-error
71
- attest(() => type("boolean&&true")).throws(
72
- writeMissingRightOperandMessage("&", "&true")
73
- )
74
- })
75
- it("implicit never", () => {
76
- attest(() => type("string&number")).throws(
77
- "Intersection of a string and a number results in an unsatisfiable type"
78
- )
79
- })
80
- it("left semantic error", () => {
81
- // @ts-expect-error
82
- attest(() => type("string%2&'foo'")).throwsAndHasTypeError(
83
- writeIndivisibleMessage("string")
84
- )
85
- })
86
- it("right semantic error", () => {
87
- // @ts-expect-error
88
- attest(() => type("'foo'&string%2")).throwsAndHasTypeError(
89
- writeIndivisibleMessage("string")
90
- )
91
- })
92
- it("chained validation", () => {
93
- attest(() =>
94
- // @ts-expect-error
95
- type({ a: "string" }).and({ b: "what" })
96
- ).throwsAndHasTypeError(writeUnresolvableMessage("what"))
97
- })
98
- it("at path", () => {
99
- attest(() => type({ a: "string" }).and({ a: "number" })).throws(
100
- "Intersection at a of a string and a number results in an unsatisfiable type"
101
- )
102
- })
103
- })
104
- })
@@ -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,74 +0,0 @@
1
- import { attest } from "@arktype/attest"
2
- import { type } from "arktype"
3
-
4
- // TODO: reenable
5
- // describe("key traversal", () => {
6
- // const getExtraneousB = () => ({ a: "ok", b: "why?" })
7
- // it("loose by default", () => {
8
- // const t = type({
9
- // a: "string"
10
- // })
11
- // const dataWithExtraneousB = getExtraneousB()
12
- // attest(t(dataWithExtraneousB).out).equals(dataWithExtraneousB)
13
- // })
14
- // it("invalid union", () => {
15
- // const o = type([{ a: "string" }, "|", { b: "boolean" }]).configure({
16
- // keys: "strict"
17
- // })
18
- // attest(o({ a: 2 }).errors?.summary).snap(
19
- // 'a must be a string or removed (was {"a":2})'
20
- // )
21
- // })
22
- // it("distilled type", () => {
23
- // const t = type({
24
- // a: "string"
25
- // }).configure({ keys: "distilled" })
26
- // attest(t({ a: "ok" }).out).equals({ a: "ok" })
27
- // attest(t(getExtraneousB()).out).snap({ a: "ok" })
28
- // })
29
- // it("distilled array", () => {
30
- // const o = type({ a: "email[]" }).configure({
31
- // keys: "distilled"
32
- // })
33
- // attest(o({ a: ["shawn@arktype.io"] }).out).snap({
34
- // a: ["shawn@arktype.io"]
35
- // })
36
- // attest(o({ a: ["notAnEmail"] }).errors?.summary).snap(
37
- // "a/0 must be a valid email (was 'notAnEmail')"
38
- // )
39
- // // can handle missing keys
40
- // attest(o({ b: ["shawn"] }).errors?.summary).snap("a must be defined")
41
- // })
42
- // it("distilled union", () => {
43
- // const o = type([{ a: "string" }, "|", { b: "boolean" }]).configure({
44
- // keys: "distilled"
45
- // })
46
- // // can distill to first branch
47
- // attest(o({ a: "to", z: "bra" }).out).snap({ a: "to" })
48
- // // can distill to second branch
49
- // attest(o({ b: true, c: false }).out).snap({ b: true })
50
- // // can handle missing keys
51
- // attest(o({ a: 2 }).errors?.summary).snap(
52
- // 'a must be a string or b must be defined (was {"a":2})'
53
- // )
54
- // })
55
- // it("strict type", () => {
56
- // const t = type({
57
- // a: "string"
58
- // }).configure({ keys: "strict" })
59
- // attest(t({ a: "ok" }).out).equals({ a: "ok" })
60
- // attest(t(getExtraneousB()).errors?.summary).snap("b must be removed")
61
- // })
62
- // it("strict array", () => {
63
- // const o = type({ a: "string[]" }).configure({
64
- // keys: "strict"
65
- // })
66
- // attest(o({ a: ["shawn"] }).out).snap({ a: ["shawn"] })
67
- // attest(o({ a: [2] }).errors?.summary).snap(
68
- // "a/0 must be a string (was number)"
69
- // )
70
- // attest(o({ b: ["shawn"] }).errors?.summary).snap(
71
- // "b must be removed\na must be defined"
72
- // )
73
- // })
74
- // })
@@ -1,134 +0,0 @@
1
- import { attest } from "@arktype/attest"
2
- import { schema } from "@arktype/schema"
3
- import { type } from "arktype"
4
- import {
5
- writeMissingRightOperandMessage,
6
- writeUnresolvableMessage
7
- } from "../parser/string/shift/operand/unenclosed.js"
8
-
9
- describe("keyof", () => {
10
- it("autocompletion", () => {
11
- // @ts-expect-error
12
- attest(() => type("k")).type.errors("keyof ")
13
- })
14
- it("root expression", () => {
15
- const t = type("keyof", "Date")
16
- attest<keyof Date>(t.infer)
17
- attest(t.json).equals(schema(Date).keyof().json)
18
- })
19
- it("primitive", () => {
20
- const t = type("keyof bigint")
21
- attest<keyof bigint>(t.infer)
22
- const expected = schema.units(
23
- "toLocaleString",
24
- "toString",
25
- "valueOf",
26
- Symbol.toStringTag
27
- )
28
- attest(t.json).equals(expected.json)
29
- })
30
- it("object literal", () => {
31
- const t = type({ a: "123", b: "123" }).keyof()
32
- attest<"a" | "b">(t.infer)
33
- attest(t.json).equals(type("'a'|'b'").json)
34
- })
35
- it("overlapping union", () => {
36
- const t = type({ a: "number", b: "boolean" })
37
- .or({ b: "number", c: "string" })
38
- .keyof()
39
- attest<"b">(t.infer)
40
- attest(t.json).equals(type("'b'").json)
41
- })
42
- it("non-overlapping union", () => {
43
- attest(() => type({ a: "number" }).or({ b: "number" }).keyof()).throws(
44
- 'Intersection of "a" and "b" results in an unsatisfiable type'
45
- )
46
- })
47
- it("tuple expression", () => {
48
- const t = type(["keyof", { a: "string" }])
49
- attest<"a">(t.infer)
50
- attest(t.json).equals(schema.units("a").json)
51
- })
52
- it("union including non-object", () => {
53
- attest(() => type({ a: "number" }).or("boolean").keyof()).throws(
54
- 'Intersection of "toString" or "valueOf" and "a" results in an unsatisfiable type'
55
- )
56
- })
57
- it("unsatisfiable", () => {
58
- attest(() => type("keyof undefined")).throws(
59
- "Intersection of unknown and never results in an unsatisfiable type"
60
- )
61
- })
62
- it("multiple keyofs", () => {
63
- const t = type("keyof keyof string")
64
- attest<"toString" | "valueOf">(t.infer)
65
- attest(t.json).equals(schema.units("toString", "valueOf").json)
66
- })
67
- it("groupable", () => {
68
- const t = type("(keyof symbol & string)[]")
69
- attest<("toString" | "valueOf" | "description")[]>(t.infer)
70
- attest(t.json).equals(
71
- schema.units("toString", "valueOf", "description").array().json
72
- )
73
- })
74
- it("intersection precedence", () => {
75
- const t = type("keyof symbol & symbol")
76
- attest<typeof Symbol.toStringTag | typeof Symbol.toPrimitive>(t.infer)
77
- attest(t.json).is(schema.units(Symbol.toStringTag, Symbol.toPrimitive).json)
78
- })
79
- it("union precedence", () => {
80
- const t = type("keyof boolean | number")
81
- attest<"valueOf" | number>(t.infer)
82
- // for some reason TS doesn't include toString as a keyof boolean?
83
- // given it is included in keyof string, it seems like an anomaly, but we include it
84
- attest(t.json).equals(type("number | 'valueOf' | 'toString'").json)
85
- })
86
- it("missing operand", () => {
87
- // @ts-expect-error
88
- attest(() => type("keyof "))
89
- .throws(writeMissingRightOperandMessage("keyof", ""))
90
- // it tries to autocomplete, so this is just a possible completion that would be included
91
- .type.errors("keyof bigint")
92
- })
93
- it("invalid operand", () => {
94
- // @ts-expect-error
95
- attest(() => type("keyof nope")).throwsAndHasTypeError(
96
- writeUnresolvableMessage("nope")
97
- )
98
- })
99
- // TODO: numeric
100
- it("array", () => {
101
- const t = type("keyof string[]")
102
- attest<keyof string[]>(t.infer)
103
- // the array prototype has many items and they vary based on the JS
104
- // flavor we're running in, so just check that the indices from the type
105
- // and one prototype key are present as a heuristic
106
- t.assert("0")
107
- t.assert("354")
108
- t.assert("map")
109
- t.assert(Symbol.iterator)
110
- attest(() => t.assert("0.1")).throws.snap(
111
- 'TypeError: / must be a string matching /^(?:0|(?:[1-9]\\d*))$/, "length", "at", "concat", "copyWithin", "fill", "find", "findIndex", "lastIndexOf", "pop", "push", "reverse", "shift", "unshift", "slice", "sort", "splice", "includes", "indexOf", "join", "keys", "entries", "values", "forEach", "filter", "flat", "flatMap", "map", "every", "some", "reduce", "reduceRight", "toLocaleString", "toString", "findLast", "findLastIndex", (symbol Symbol.iterator) or (symbol Symbol.unscopables) (was "0.1")'
112
- )
113
- attest(() => t.assert("-1")).throws.snap(
114
- 'TypeError: / must be a string matching /^(?:0|(?:[1-9]\\d*))$/, "length", "at", "concat", "copyWithin", "fill", "find", "findIndex", "lastIndexOf", "pop", "push", "reverse", "shift", "unshift", "slice", "sort", "splice", "includes", "indexOf", "join", "keys", "entries", "values", "forEach", "filter", "flat", "flatMap", "map", "every", "some", "reduce", "reduceRight", "toLocaleString", "toString", "findLast", "findLastIndex", (symbol Symbol.iterator) or (symbol Symbol.unscopables) (was "-1")'
115
- )
116
- })
117
- it("tuple", () => {
118
- const t = type(["keyof", ["string", "number"]])
119
- attest<keyof [string, number]>(t.infer)
120
- t.assert("1")
121
- t.assert("map")
122
- attest(() => t.assert("2")).throws.snap(
123
- 'TypeError: / must be "length", "0", "1", "at", "concat", "copyWithin", "fill", "find", "findIndex", "lastIndexOf", "pop", "push", "reverse", "shift", "unshift", "slice", "sort", "splice", "includes", "indexOf", "join", "keys", "entries", "values", "forEach", "filter", "flat", "flatMap", "map", "every", "some", "reduce", "reduceRight", "toLocaleString", "toString", "findLast", "findLastIndex", (symbol Symbol.iterator) or (symbol Symbol.unscopables) (was "2")'
124
- )
125
- })
126
- it("wellFormedNonNegativeInteger intersection", () => {
127
- const a = type([{ "1": "'foo'" }, "&", "string[]"])
128
- const t = type("keyof", a)
129
- // TODO should still include wellFormed
130
- attest(t.root.toString()).snap(
131
- '"1" or "at" or "concat" or "copyWithin" or "entries" or "every" or "fill" or "filter" or "find" or "findIndex" or "findLast" or "findLastIndex" or "flat" or "flatMap" or "forEach" or "includes" or "indexOf" or "join" or "keys" or "lastIndexOf" or "length" or "map" or "pop" or "push" or "reduce" or "reduceRight" or "reverse" or "shift" or "slice" or "some" or "sort" or "splice" or "toLocaleString" or "toString" or "unshift" or "values" or (symbol Symbol.iterator) or (symbol Symbol.unscopables) or /^(?:0|(?:[1-9]\\d*))$/'
132
- )
133
- })
134
- })
@@ -1,188 +0,0 @@
1
- import { attest } from "@arktype/attest"
2
- import { schema } from "@arktype/schema"
3
- import { type } from "arktype"
4
-
5
- describe("keywords", () => {
6
- describe("jsObjects", () => {
7
- it("Function", () => {
8
- // should not be treated as a morph
9
- attest<Function>(type("Function").infer)
10
- })
11
- it("Date", () => {
12
- // should not expand built-in classes
13
- attest(type("Date").infer).type.toString.snap("Date")
14
- })
15
- })
16
- describe("tsKeywords", () => {
17
- it("string strings", () => {
18
- /**
19
- * In honor of @ark-expect-beta aka log(n):
20
- * - Zirco author https://github.com/zirco-lang/zrc
21
- * - Shameless Rust stan
22
- * - Occasional user of ArkType libraries
23
- * - Frequent user of ArkType Discord
24
- * - Universally renowned two-finger speed typist
25
- */
26
- const string = type("string")
27
- attest<string>(string.infer)
28
- attest(string("string").out).snap("string")
29
- })
30
- it("any", () => {
31
- const any = type("any")
32
- // equivalent to unknown at runtime
33
- attest(any.json).equals(type("unknown").json)
34
- // inferred as any
35
- attest<any>(any.infer)
36
- })
37
- it("boolean", () => {
38
- const boolean = type("boolean")
39
- attest<boolean>(boolean.infer)
40
- // should be simplified to simple checks for true and false literals
41
- attest(boolean.json).equals(schema({ unit: false }, { unit: true }).json)
42
- // TODO:
43
- // attest(boolean.json).snap(`if( $arkRoot !== false && $arkRoot !== true) {
44
- // return false
45
- // }`)
46
- })
47
- it("never", () => {
48
- const never = type("never")
49
- attest<never>(never.infer)
50
- // should be equivalent to a zero-branch union
51
- attest(never.json).equals(schema().json)
52
- })
53
- it("unknown", () => {
54
- // should be equivalent to an unconstrained predicate
55
- attest(type("unknown").json).equals(schema({}).json)
56
- })
57
- it("void", () => {
58
- const t = type("void")
59
- attest<void>(t.infer)
60
- //should be treated as undefined at runtime
61
- attest(t.json).equals(type("undefined").json)
62
- })
63
- })
64
- // describe("validation", () => {
65
- // it("integer", () => {
66
- // const integer = type("integer")
67
- // attest(integer(123).out).equals(123)
68
- // attest(integer("123").errors?.summary).equals(
69
- // "Must be a number (was string)"
70
- // )
71
- // attest(integer(12.12).errors?.summary).equals(
72
- // "Must be an integer (was 12.12)"
73
- // )
74
- // })
75
- // it("alpha", () => {
76
- // const alpha = type("alpha")
77
- // attest(alpha("user").out).equals("user")
78
- // attest(alpha("user123").errors?.summary).equals(
79
- // "Must be only letters (was 'user123')"
80
- // )
81
- // })
82
- // it("alphanumeric", () => {
83
- // const alphanumeric = type("alphanumeric")
84
- // attest(alphanumeric("user123").out).equals("user123")
85
- // attest(alphanumeric("user").out).equals("user")
86
- // attest(alphanumeric("123").out).equals("123")
87
- // attest(alphanumeric("abc@123").errors?.summary).equals(
88
- // "Must be only letters and digits (was 'abc@123')"
89
- // )
90
- // })
91
- // it("lowercase", () => {
92
- // const lowercase = type("lowercase")
93
- // attest(lowercase("var").out).equals("var")
94
- // attest(lowercase("newVar").errors?.summary).equals(
95
- // "Must be only lowercase letters (was 'newVar')"
96
- // )
97
- // })
98
- // it("uppercase", () => {
99
- // const uppercase = type("uppercase")
100
- // attest(uppercase("VAR").out).equals("VAR")
101
- // attest(uppercase("CONST_VAR").errors?.summary).equals(
102
- // "Must be only uppercase letters (was 'CONST_VAR')"
103
- // )
104
- // attest(uppercase("myVar").errors?.summary).equals(
105
- // "Must be only uppercase letters (was 'myVar')"
106
- // )
107
- // })
108
- // it("email", () => {
109
- // const email = type("email")
110
- // attest(email("shawn@mail.com").out).equals("shawn@mail.com")
111
- // attest(email("shawn@email").errors?.summary).equals(
112
- // "Must be a valid email (was 'shawn@email')"
113
- // )
114
- // })
115
- // it("uuid", () => {
116
- // const uuid = type("uuid")
117
- // attest(uuid("f70b8242-dd57-4e6b-b0b7-649d997140a0").out).equals(
118
- // "f70b8242-dd57-4e6b-b0b7-649d997140a0"
119
- // )
120
- // attest(uuid("1234").errors?.summary).equals(
121
- // "Must be a valid UUID (was '1234')"
122
- // )
123
- // })
124
- // it("parsedNumber", () => {
125
- // const parsedNumber = type("parsedNumber")
126
- // attest(parsedNumber("5").out).equals(5)
127
- // attest(parsedNumber("5.5").out).equals(5.5)
128
- // attest(parsedNumber("five").errors?.summary).equals(
129
- // "Must be a well-formed numeric string (was 'five')"
130
- // )
131
- // })
132
- // it("parsedInteger", () => {
133
- // const parsedInteger = type("parsedInteger")
134
- // attest(parsedInteger("5").out).equals(5)
135
- // attest(parsedInteger("5.5").errors?.summary).equals(
136
- // "Must be a well-formed integer string (was '5.5')"
137
- // )
138
- // attest(parsedInteger("five").errors?.summary).equals(
139
- // "Must be a well-formed integer string (was 'five')"
140
- // )
141
- // attest(parsedInteger(5).errors?.summary).equals(
142
- // "Must be a string (was number)"
143
- // )
144
- // attest(parsedInteger("9007199254740992").errors?.summary).equals(
145
- // "Must be an integer in the range Number.MIN_SAFE_INTEGER to Number.MAX_SAFE_INTEGER (was '9007199254740992')"
146
- // )
147
- // })
148
- // it("parsedDate", () => {
149
- // const parsedDate = type("parsedDate")
150
- // attest(parsedDate("5/21/1993").out?.toDateString()).equals(
151
- // "Fri May 21 1993"
152
- // )
153
- // attest(parsedDate("foo").errors?.summary).equals(
154
- // "Must be a valid date (was 'foo')"
155
- // )
156
- // attest(parsedDate(5).errors?.summary).equals(
157
- // "Must be a string (was number)"
158
- // )
159
- // })
160
- // it("json", () => {
161
- // const json = type("json")
162
- // attest(json('{"a": "hello"}').out).equals({ a: "hello" })
163
- // attest(json(123).errors?.summary).equals(
164
- // "Must be a JSON-parsable string (was number)"
165
- // )
166
- // })
167
- // it("credit card", () => {
168
- // const validCC = "5489582921773376"
169
- // attest(ark.creditCard(validCC).out).equals(validCC)
170
- // // Regex validation
171
- // attest(ark.creditCard("0".repeat(16)).errors?.summary).equals(
172
- // "Must be a valid credit card number (was '0000000000000000')"
173
- // )
174
- // // Luhn validation
175
- // attest(
176
- // ark.creditCard(validCC.slice(0, -1) + "0").errors?.summary
177
- // ).equals(
178
- // "Must be a valid credit card number (was '5489582921773370')"
179
- // )
180
- // })
181
- // it("semver", () => {
182
- // attest(ark.semver("1.0.0").out).equals("1.0.0")
183
- // attest(ark.semver("-1.0.0").errors?.summary).equals(
184
- // "Must be a valid semantic version (see https://semver.org/) (was '-1.0.0')"
185
- // )
186
- // })
187
- // })
188
- })
@@ -1,51 +0,0 @@
1
- import { attest } from "@arktype/attest"
2
- import { schema } from "@arktype/schema"
3
- import { type } from "arktype"
4
-
5
- describe("literal", () => {
6
- describe("tuple expression", () => {
7
- it("literal", () => {
8
- const t = type(["===", 5])
9
- attest<5>(t.infer)
10
- attest(t.json).equals(type("5").json)
11
- })
12
- it("non-serializable", () => {
13
- const s = Symbol()
14
- const t = type(["===", s])
15
- attest<symbol>(t.infer)
16
- attest(t(s).out).equals(s)
17
- attest(t("test").errors?.summary).snap(
18
- 'Must be (symbol anonymous) (was "test")'
19
- )
20
- })
21
- it("branches", () => {
22
- const o = { ark: true }
23
- const s = Symbol()
24
- const t = type(["===", true, "foo", 5, 1n, null, undefined, o, s])
25
- attest<
26
- true | "foo" | 5 | 1n | null | undefined | { ark: boolean } | typeof s
27
- >(t.infer)
28
- attest(t.json).equals(
29
- schema.units(true, "foo", 5, 1n, null, undefined, o, s).json
30
- )
31
- })
32
- })
33
- describe("root expression", () => {
34
- it("single", () => {
35
- const t = type("===", true)
36
- attest<true>(t.infer)
37
- attest(t.json).equals(type("true").json)
38
- })
39
- it("branches", () => {
40
- const o = { ark: true }
41
- const s = Symbol()
42
- const t = type("===", "foo", 5, true, null, 1n, undefined, o, s)
43
- attest<
44
- true | "foo" | 5 | 1n | null | undefined | { ark: boolean } | typeof s
45
- >(t.infer)
46
- attest(t.json).equals(
47
- schema.units(true, "foo", 5, 1n, null, undefined, o, s).json
48
- )
49
- })
50
- })
51
- })
@@ -1,31 +0,0 @@
1
- import { bench } from "@arktype/attest"
2
- import { match } from "arktype"
3
-
4
- bench("general matchers", () => {
5
- const matcher = match()
6
- .when("string", (s) => s)
7
- .when("number", (n) => n)
8
- .when("boolean", (b) => b)
9
- .orThrow()
10
-
11
- const a = matcher("abc")
12
- const b = matcher(4)
13
- const c = matcher(true)
14
- })
15
- .median()
16
- .types()
17
-
18
- bench("match.only<T>", () => {
19
- const matcher = match
20
- .only<string | number | boolean>()
21
- .when("string", (s) => s)
22
- .when("number", (n) => n)
23
- .when("boolean", (b) => b)
24
- .finalize()
25
-
26
- const a = matcher("abc")
27
- const b = matcher(4)
28
- const c = matcher(true)
29
- })
30
- .median()
31
- .types()