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,56 +1,56 @@
1
- describe("snippets", () => {
2
- // it("demo", async () => {
3
- // const typeSnippet = await import("../../docs/examples/demo.js")
4
- // attest<{>(typeSnippet.pkg.infer)
5
- // name: string
6
- // version: string
7
- // contributors?: string[]
8
- // }
9
- // attest(typeSnippet.errors?.summary).snap(
10
- // "contributors must be more than 1 items long (was 1)"
11
- // )
12
- // })
13
- // it("type", async () => {
14
- // const typeSnippet = await import("./benches/type.js")
15
- // attest<{>(typeSnippet.user.infer)
16
- // name: string
17
- // device: {
18
- // platform: "android" | "ios"
19
- // version?: number
20
- // }
21
- // }
22
- // attest(typeSnippet.errors?.summary).snap(
23
- // "device/platform must be 'android' or 'ios' (was 'enigma')"
24
- // )
25
- // })
26
- // // it("scope", async () => {
27
- // // const scopeSnippet = await import("../examples/scope.js")
28
- // // attest<{>(scopeSnippet.types.package.infer)
29
- // // name: string
30
- // // dependencies?: any[]
31
- // // devDependencies?: any[]
32
- // // contributors?: {
33
- // // email: string
34
- // // packages?: any[]
35
- // // }[]
36
- // // }
37
- // // attest(scopeSnippet.errors?.summary).snap(
38
- // // "dependencies/0/dependencies/0/contributors/0/email must be a valid email (was 'david@sharktypeio')\ncontributors/0/email must be a valid email (was 'david@sharktypeio')"
39
- // // )
40
- // // })
41
- // it("optimized", async () => {
42
- // const example = await import("../../docs/examples/optimized.js")
43
- // attest(example.deepLeftOrRight.infer).typed as
44
- // | {
45
- // auto: {
46
- // discriminated: "left"
47
- // }
48
- // }
49
- // | {
50
- // auto: {
51
- // discriminated: "right"
52
- // }
53
- // }
54
- // attest<number>(example.numericIntersection.infer)
55
- // })
56
- })
1
+ // describe("snippets", () => {
2
+ // it("demo", async () => {
3
+ // const typeSnippet = await import("../../docs/examples/demo.js")
4
+ // attest<{>(typeSnippet.pkg.infer)
5
+ // name: string
6
+ // version: string
7
+ // contributors?: string[]
8
+ // }
9
+ // attest(typeSnippet.toString()).snap(
10
+ // "contributors must be more than 1 items long (was 1)"
11
+ // )
12
+ // })
13
+ // it("type", async () => {
14
+ // const typeSnippet = await import("./benches/type.js")
15
+ // attest<{>(typeSnippet.user.infer)
16
+ // name: string
17
+ // device: {
18
+ // platform: "android" | "ios"
19
+ // version?: number
20
+ // }
21
+ // }
22
+ // attest(typeSnippet.toString()).snap(
23
+ // "device/platform must be 'android' or 'ios' (was 'enigma')"
24
+ // )
25
+ // })
26
+ // // it("scope", async () => {
27
+ // // const scopeSnippet = await import("../examples/scope.js")
28
+ // // attest<{>(scopeSnippet.types.package.infer)
29
+ // // name: string
30
+ // // dependencies?: any[]
31
+ // // devDependencies?: any[]
32
+ // // contributors?: {
33
+ // // email: string
34
+ // // packages?: any[]
35
+ // // }[]
36
+ // // }
37
+ // // attest(scopeSnippet.toString()).snap(
38
+ // // "dependencies/0/dependencies/0/contributors/0/email must be a valid email (was 'david@sharktypeio')\ncontributors/0/email must be a valid email (was 'david@sharktypeio')"
39
+ // // )
40
+ // // })
41
+ // it("optimized", async () => {
42
+ // const example = await import("../../docs/examples/optimized.js")
43
+ // attest(example.deepLeftOrRight.infer).typed as
44
+ // | {
45
+ // auto: {
46
+ // discriminated: "left"
47
+ // }
48
+ // }
49
+ // | {
50
+ // auto: {
51
+ // discriminated: "right"
52
+ // }
53
+ // }
54
+ // attest<number>(example.numericIntersection.infer)
55
+ // })
56
+ // })
@@ -1,29 +1,116 @@
1
- import { attest } from "@arktype/attest"
2
- import { schema } from "@arktype/schema"
3
- import { type, type Out } from "arktype"
4
- import { scope, type Ark } from "../ark.js"
5
- import {
6
- writeMissingRightOperandMessage,
7
- writeUnresolvableMessage
8
- } from "../parser/string/shift/operand/unenclosed.js"
9
- import type { Type } from "../type.js"
1
+ import { attest, contextualize } from "@arktype/attest"
2
+ import { rawSchema, writeUnresolvableMessage } from "@arktype/schema"
3
+ import { type } from "arktype"
4
+ import { writeMissingRightOperandMessage } from "../parser/string/shift/operand/unenclosed.js"
5
+
6
+ contextualize(
7
+ "tuple expressions",
8
+ () => {
9
+ it("nested", () => {
10
+ const t = type(["string|bigint", "|", ["number", "|", "boolean"]])
11
+ attest<string | number | bigint | boolean>(t.infer)
12
+ })
13
+
14
+ it("autocompletion", () => {
15
+ // @ts-expect-error
16
+ attest(() => type([""])).completions({
17
+ "": [
18
+ "string",
19
+ "number",
20
+ "bigint",
21
+ "boolean",
22
+ "symbol",
23
+ "undefined",
24
+ "object",
25
+ "null",
26
+ "integer",
27
+ "Array",
28
+ "Date",
29
+ "Error",
30
+ "Function",
31
+ "Map",
32
+ "RegExp",
33
+ "Set",
34
+ "WeakMap",
35
+ "WeakSet",
36
+ "Promise",
37
+ "true",
38
+ "false",
39
+ "any",
40
+ "never",
41
+ "unknown",
42
+ "parse",
43
+ "keyof",
44
+ "void",
45
+ "url",
46
+ "alpha",
47
+ "alphanumeric",
48
+ "lowercase",
49
+ "uppercase",
50
+ "creditCard",
51
+ "email",
52
+ "uuid",
53
+ "semver",
54
+ "Record",
55
+ "instanceof",
56
+ "===",
57
+ "...",
58
+ "this"
59
+ ]
60
+ })
61
+ // @ts-expect-error
62
+ attest(() => type(["string", ""])).completions({
63
+ "": [
64
+ "string",
65
+ "number",
66
+ "bigint",
67
+ "boolean",
68
+ "symbol",
69
+ "undefined",
70
+ "object",
71
+ "null",
72
+ "integer",
73
+ "Array",
74
+ "Date",
75
+ "Error",
76
+ "Function",
77
+ "Map",
78
+ "RegExp",
79
+ "Set",
80
+ "WeakMap",
81
+ "WeakSet",
82
+ "Promise",
83
+ "true",
84
+ "false",
85
+ "?",
86
+ "any",
87
+ "never",
88
+ "unknown",
89
+ "parse",
90
+ "keyof",
91
+ "void",
92
+ "[]",
93
+ "url",
94
+ "alpha",
95
+ "alphanumeric",
96
+ "lowercase",
97
+ "uppercase",
98
+ "creditCard",
99
+ "email",
100
+ "uuid",
101
+ "semver",
102
+ "Record",
103
+ "|",
104
+ "&",
105
+ ":",
106
+ "=>",
107
+ "@",
108
+ "...",
109
+ "this"
110
+ ]
111
+ })
112
+ })
10
113
 
11
- describe("tuple expressions", () => {
12
- it("nested", () => {
13
- const t = type(["string|bigint", "|", ["number", "|", "boolean"]])
14
- attest<string | number | bigint | boolean>(t.infer)
15
- })
16
- it("autocompletion", () => {
17
- // @ts-expect-error
18
- attest(() => type([""])).type.errors(
19
- `IndexZeroOperator | keyof Ark | "this"`
20
- )
21
- // @ts-expect-error
22
- attest(() => type(["string", ""])).type.errors(
23
- `"keyof" | keyof Ark | "this" | IndexOneOperator'`
24
- )
25
- })
26
- describe("errors", () => {
27
114
  it("missing right operand", () => {
28
115
  // @ts-expect-error
29
116
  attest(() => type(["string", "|"])).throwsAndHasTypeError(
@@ -34,105 +121,90 @@ describe("tuple expressions", () => {
34
121
  writeMissingRightOperandMessage("&", "")
35
122
  )
36
123
  })
124
+
37
125
  it("nested parse error", () => {
38
126
  attest(() => {
39
127
  // @ts-expect-error
40
128
  type(["string", "|", "numbr"])
41
129
  }).throwsAndHasTypeError(writeUnresolvableMessage("numbr"))
42
130
  })
131
+
43
132
  it("nested object parse error", () => {
44
133
  attest(() => {
45
134
  // @ts-expect-error
46
135
  type([{ s: "strng" }, "|", "number"])
47
136
  }).throwsAndHasTypeError(writeUnresolvableMessage("strng"))
48
137
  })
49
- // TODO: reenable
50
- it("this", () => {
51
- const t = type([{ a: "string" }, "|", { b: "this" }])
52
- attest(t.infer).type.toString.snap(
53
- "{ a: string; } | { b: { a: string; } | any; }"
54
- )
55
- const types = scope({
56
- a: {
57
- a: "string"
58
- },
59
- b: {
60
- b: "expected"
61
- },
62
- expected: "a|b"
63
- }).export()
64
- attest(t.json).equals(types.expected.json)
138
+ },
139
+ "root expression",
140
+ () => {
141
+ it("=== single", () => {
142
+ const t = type("===", 5)
143
+ attest<5>(t.infer)
144
+ attest(t.json).equals(type("5").json)
65
145
  })
66
- })
67
- })
68
146
 
69
- describe("root expression", () => {
70
- it("=== single", () => {
71
- const t = type("===", 5)
72
- attest<5>(t.infer)
73
- attest(t.json).equals(type("5").json)
74
- })
75
- it("=== branches", () => {
76
- const t = type("===", "foo", "bar", "baz")
77
- attest<"foo" | "bar" | "baz">(t.infer)
78
- attest(t.json).equals(schema.units("foo", "bar", "baz").json)
79
- })
80
- it("instanceof single", () => {
81
- const t = type("instanceof", RegExp)
82
- attest<RegExp>(t.infer)
83
- attest(t.json).equals(schema(RegExp).json)
84
- })
85
- it("instanceof branches", () => {
86
- const t = type("instanceof", Array, Date)
87
- attest<unknown[] | Date>(t.infer)
88
- attest(t.json).equals(schema(Array, Date).json)
89
- })
90
- it("postfix", () => {
91
- const t = type({ a: "string" }, "[]")
92
- attest<{ a: string }[]>(t.infer)
93
- attest(t.json).equals(type({ a: "string" }).array().json)
94
- })
95
- it("infix", () => {
96
- const t = type({ a: "string" }, "|", { b: "boolean" })
97
- attest<
98
- | {
99
- a: string
100
- }
101
- | {
102
- b: boolean
103
- }
104
- >(t.infer)
147
+ it("=== branches", () => {
148
+ const t = type("===", "foo", "bar", "baz")
149
+ attest<"foo" | "bar" | "baz">(t.infer)
150
+ attest(t.json).snap([{ unit: "bar" }, { unit: "baz" }, { unit: "foo" }])
151
+ })
152
+
153
+ it("instanceof single", () => {
154
+ const t = type("instanceof", RegExp)
155
+ attest<RegExp>(t.infer)
156
+ const expected = rawSchema(RegExp)
157
+ attest(t.json).equals(expected.json)
158
+ })
105
159
 
106
- attest(t.json).equals(type({ a: "string" }).or({ b: "boolean" }).json)
107
- })
108
- it("morph", () => {
109
- const t = type({ a: "string" }, "=>", (In) => ({ b: In.a }))
110
- attest<Type<(In: { a: string }) => Out<{ b: string }>, Ark>>(t)
111
- })
112
- it("narrow", () => {
113
- const t = type(
114
- { a: "string" },
115
- ":",
116
- (In): In is { a: "foo" } => In.a === "foo"
117
- )
118
- attest<{ a: "foo" }>(t.infer)
119
- })
120
- it("this", () => {
121
- const t = type({ a: "string" }, "|", { b: "this" })
122
- attest(t.infer).type.toString.snap(
123
- "{ a: string; } | { b: { a: string; } | any; }"
124
- )
125
- attest(t.json).equals(type([{ a: "string" }, "|", { b: "this" }]).json)
126
- })
127
- it("tuple as second arg", () => {
128
- // this case is not fundamentally unique but TS has a hard time
129
- // narrowing tuples in contexts like this
130
- const t = type("keyof", [
131
- { a: "string" },
132
- "&",
133
- { b: "boolean" }
134
- // as const is required for TS <=5.0
135
- ] as const)
136
- attest<"a" | "b">(t.infer)
137
- })
138
- })
160
+ it("instanceof branches", () => {
161
+ const t = type("instanceof", Array, Date)
162
+ attest<unknown[] | Date>(t.infer)
163
+ const expected = rawSchema([Array, Date])
164
+ attest(t.json).equals(expected.json)
165
+ })
166
+
167
+ it("postfix", () => {
168
+ const t = type({ a: "string" }, "[]")
169
+ attest<{ a: string }[]>(t.infer)
170
+ attest(t.json).equals(type({ a: "string" }).array().json)
171
+ })
172
+
173
+ it("infix", () => {
174
+ const t = type({ a: "string" }, "|", { b: "boolean" })
175
+ attest<
176
+ | {
177
+ a: string
178
+ }
179
+ | {
180
+ b: boolean
181
+ }
182
+ >(t.infer)
183
+
184
+ attest(t.json).equals(type({ a: "string" }).or({ b: "boolean" }).json)
185
+ })
186
+ // TODO: infinitely deep why?
187
+ // it("morph", () => {
188
+ // const t = type({ a: "string" }, "=>", (In) => ({ b: In.a }))
189
+ // attest<Type<(In: { a: string }) => Out<{ b: string }>, Ark>>(t)
190
+ // })
191
+
192
+ it("narrow", () => {
193
+ const t = type(
194
+ { a: "string" },
195
+ ":",
196
+ (In): In is { a: "foo" } => In.a === "foo"
197
+ )
198
+ attest<{ a: "foo" }>(t.infer)
199
+ })
200
+
201
+ it("tuple as second arg", () => {
202
+ // this case is not fundamentally unique but TS has a hard time
203
+ // narrowing tuples in contexts like this
204
+ const t = type("keyof", [{ a: "string" }, "&", { b: "boolean" }])
205
+ const expected = type("'a' | 'b'")
206
+ attest<typeof expected.infer>(t.infer)
207
+ attest(t.json).equals(expected.json)
208
+ })
209
+ }
210
+ )