arktype 2.0.0-dev.9 → 2.0.0-rc.1

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 (263) hide show
  1. package/LICENSE +7 -0
  2. package/out/config.d.ts +25 -0
  3. package/out/config.js +2 -0
  4. package/out/generic.d.ts +157 -18
  5. package/out/generic.js +37 -23
  6. package/out/index.d.ts +7 -0
  7. package/out/index.js +7 -0
  8. package/out/intersect.d.ts +28 -0
  9. package/out/intersect.js +1 -0
  10. package/out/keys.d.ts +16 -0
  11. package/out/keys.js +1 -0
  12. package/out/keywords/ark.d.ts +39 -0
  13. package/out/keywords/ark.js +24 -0
  14. package/out/keywords/ast.d.ts +193 -0
  15. package/out/keywords/ast.js +2 -0
  16. package/out/keywords/builtins.d.ts +17 -0
  17. package/out/keywords/builtins.js +10 -0
  18. package/out/keywords/constructors/Array.d.ts +20 -0
  19. package/out/keywords/constructors/Array.js +19 -0
  20. package/out/keywords/constructors/FormData.d.ts +15 -0
  21. package/out/keywords/constructors/FormData.js +38 -0
  22. package/out/keywords/constructors/TypedArray.d.ts +19 -0
  23. package/out/keywords/constructors/TypedArray.js +14 -0
  24. package/out/keywords/constructors/constructors.d.ts +31 -0
  25. package/out/keywords/constructors/constructors.js +16 -0
  26. package/out/keywords/number/epoch.d.ts +10 -0
  27. package/out/keywords/number/epoch.js +26 -0
  28. package/out/keywords/number/integer.d.ts +3 -0
  29. package/out/keywords/number/integer.js +5 -0
  30. package/out/keywords/number/number.d.ts +28 -0
  31. package/out/keywords/number/number.js +9 -0
  32. package/out/keywords/string/alpha.d.ts +7 -0
  33. package/out/keywords/string/alpha.js +2 -0
  34. package/out/keywords/string/alphanumeric.d.ts +7 -0
  35. package/out/keywords/string/alphanumeric.js +2 -0
  36. package/out/keywords/string/capitalize.d.ts +15 -0
  37. package/out/keywords/string/capitalize.js +12 -0
  38. package/out/keywords/string/creditCard.d.ts +8 -0
  39. package/out/keywords/string/creditCard.js +37 -0
  40. package/out/keywords/string/date.d.ts +59 -0
  41. package/out/keywords/string/date.js +98 -0
  42. package/out/keywords/string/digits.d.ts +7 -0
  43. package/out/keywords/string/digits.js +2 -0
  44. package/out/keywords/string/email.d.ts +7 -0
  45. package/out/keywords/string/email.js +4 -0
  46. package/out/keywords/string/integer.d.ts +16 -0
  47. package/out/keywords/string/integer.js +16 -0
  48. package/out/keywords/string/ip.d.ts +20 -0
  49. package/out/keywords/string/ip.js +23 -0
  50. package/out/keywords/string/json.d.ts +15 -0
  51. package/out/keywords/string/json.js +45 -0
  52. package/out/keywords/string/lower.d.ts +15 -0
  53. package/out/keywords/string/lower.js +12 -0
  54. package/out/keywords/string/normalize.d.ts +92 -0
  55. package/out/keywords/string/normalize.js +43 -0
  56. package/out/keywords/string/numeric.d.ts +15 -0
  57. package/out/keywords/string/numeric.js +13 -0
  58. package/out/keywords/string/semver.d.ts +7 -0
  59. package/out/keywords/string/semver.js +4 -0
  60. package/out/keywords/string/string.d.ts +112 -0
  61. package/out/keywords/string/string.js +41 -0
  62. package/out/keywords/string/trim.d.ts +15 -0
  63. package/out/keywords/string/trim.js +14 -0
  64. package/out/keywords/string/upper.d.ts +15 -0
  65. package/out/keywords/string/upper.js +12 -0
  66. package/out/keywords/string/url.d.ts +15 -0
  67. package/out/keywords/string/url.js +37 -0
  68. package/out/keywords/string/utils.d.ts +2 -0
  69. package/out/keywords/string/utils.js +10 -0
  70. package/out/keywords/string/uuid.d.ts +42 -0
  71. package/out/keywords/string/uuid.js +19 -0
  72. package/out/keywords/ts.d.ts +64 -0
  73. package/out/keywords/ts.js +53 -0
  74. package/out/keywords/utils.d.ts +1 -0
  75. package/out/keywords/utils.js +2 -0
  76. package/out/module.d.ts +21 -9
  77. package/out/module.js +3 -3
  78. package/out/parser/definition.d.ts +18 -15
  79. package/out/parser/definition.js +5 -5
  80. package/out/parser/objectLiteral.d.ts +65 -35
  81. package/out/parser/objectLiteral.js +75 -50
  82. package/out/parser/semantic/bounds.d.ts +12 -9
  83. package/out/parser/semantic/default.d.ts +9 -0
  84. package/out/parser/semantic/default.js +1 -0
  85. package/out/parser/semantic/divisor.d.ts +5 -5
  86. package/out/parser/semantic/divisor.js +1 -0
  87. package/out/parser/semantic/infer.d.ts +29 -14
  88. package/out/parser/semantic/infer.js +1 -0
  89. package/out/parser/semantic/keyof.d.ts +7 -0
  90. package/out/parser/semantic/keyof.js +1 -0
  91. package/out/parser/semantic/utils.d.ts +4 -4
  92. package/out/parser/semantic/utils.js +1 -0
  93. package/out/parser/semantic/validate.d.ts +19 -18
  94. package/out/parser/semantic/validate.js +0 -1
  95. package/out/parser/string/reduce/dynamic.d.ts +16 -14
  96. package/out/parser/string/reduce/dynamic.js +8 -7
  97. package/out/parser/string/reduce/shared.d.ts +15 -15
  98. package/out/parser/string/reduce/shared.js +4 -2
  99. package/out/parser/string/reduce/static.d.ts +22 -23
  100. package/out/parser/string/reduce/static.js +1 -0
  101. package/out/parser/string/shift/operand/date.d.ts +2 -4
  102. package/out/parser/string/shift/operand/date.js +1 -1
  103. package/out/parser/string/shift/operand/enclosed.d.ts +13 -7
  104. package/out/parser/string/shift/operand/enclosed.js +5 -4
  105. package/out/parser/string/shift/operand/genericArgs.d.ts +13 -13
  106. package/out/parser/string/shift/operand/genericArgs.js +7 -12
  107. package/out/parser/string/shift/operand/operand.d.ts +8 -7
  108. package/out/parser/string/shift/operand/operand.js +5 -6
  109. package/out/parser/string/shift/operand/unenclosed.d.ts +28 -16
  110. package/out/parser/string/shift/operand/unenclosed.js +9 -12
  111. package/out/parser/string/shift/operator/bounds.d.ts +18 -22
  112. package/out/parser/string/shift/operator/bounds.js +11 -16
  113. package/out/parser/string/shift/operator/default.d.ts +19 -0
  114. package/out/parser/string/shift/operator/default.js +17 -0
  115. package/out/parser/string/shift/operator/divisor.d.ts +3 -3
  116. package/out/parser/string/shift/operator/divisor.js +1 -1
  117. package/out/parser/string/shift/operator/operator.d.ts +7 -6
  118. package/out/parser/string/shift/operator/operator.js +2 -2
  119. package/out/parser/string/shift/scanner.d.ts +29 -31
  120. package/out/parser/string/shift/scanner.js +24 -21
  121. package/out/parser/string/string.d.ts +17 -13
  122. package/out/parser/string/string.js +5 -2
  123. package/out/parser/tuple.d.ts +21 -19
  124. package/out/parser/tuple.js +12 -23
  125. package/out/scope.d.ts +116 -55
  126. package/out/scope.js +73 -59
  127. package/out/subtypes/array.d.ts +13 -0
  128. package/out/subtypes/array.js +1 -0
  129. package/out/subtypes/base.d.ts +85 -0
  130. package/out/subtypes/base.js +1 -0
  131. package/out/subtypes/date.d.ts +11 -0
  132. package/out/subtypes/date.js +1 -0
  133. package/out/subtypes/instantiate.d.ts +16 -0
  134. package/out/subtypes/instantiate.js +1 -0
  135. package/out/subtypes/morph.d.ts +13 -0
  136. package/out/subtypes/morph.js +1 -0
  137. package/out/subtypes/number.d.ts +12 -0
  138. package/out/subtypes/number.js +1 -0
  139. package/out/subtypes/object.d.ts +33 -0
  140. package/out/subtypes/object.js +1 -0
  141. package/out/subtypes/string.d.ts +13 -0
  142. package/out/subtypes/string.js +1 -0
  143. package/out/subtypes/validator.d.ts +11 -0
  144. package/out/subtypes/validator.js +1 -0
  145. package/out/type.d.ts +32 -49
  146. package/out/type.js +24 -14
  147. package/package.json +46 -45
  148. package/__tests__/array.test.ts +0 -391
  149. package/__tests__/badDefinitionType.test.ts +0 -58
  150. package/__tests__/basis.test.ts +0 -50
  151. package/__tests__/bounds.test.ts +0 -318
  152. package/__tests__/cast.test.ts +0 -37
  153. package/__tests__/completions.test.ts +0 -51
  154. package/__tests__/config.test.ts +0 -96
  155. package/__tests__/dateLiteral.test.ts +0 -36
  156. package/__tests__/declared.test.ts +0 -124
  157. package/__tests__/discrimination.test.ts +0 -74
  158. package/__tests__/divisor.test.ts +0 -114
  159. package/__tests__/enclosed.test.ts +0 -107
  160. package/__tests__/examples.test.ts +0 -56
  161. package/__tests__/expressions.test.ts +0 -210
  162. package/__tests__/generateBenchData.ts +0 -89
  163. package/__tests__/generated/cyclic.ts +0 -3058
  164. package/__tests__/generic.test.ts +0 -313
  165. package/__tests__/group.test.ts +0 -85
  166. package/__tests__/hkt.test.ts +0 -22
  167. package/__tests__/imports.test.ts +0 -126
  168. package/__tests__/instanceof.test.ts +0 -104
  169. package/__tests__/intersection.test.ts +0 -108
  170. package/__tests__/keyTraversal.test.ts +0 -77
  171. package/__tests__/keyof.test.ts +0 -150
  172. package/__tests__/keywords.test.ts +0 -214
  173. package/__tests__/literal.test.ts +0 -90
  174. package/__tests__/match.bench.ts +0 -31
  175. package/__tests__/match.test.ts +0 -234
  176. package/__tests__/methods.bench.ts +0 -22
  177. package/__tests__/narrow.test.ts +0 -100
  178. package/__tests__/object.bench.ts +0 -28
  179. package/__tests__/objectLiteral.test.ts +0 -442
  180. package/__tests__/operand.bench.ts +0 -45
  181. package/__tests__/operator.bench.ts +0 -130
  182. package/__tests__/pipe.test.ts +0 -501
  183. package/__tests__/realWorld.test.ts +0 -63
  184. package/__tests__/regex.test.ts +0 -68
  185. package/__tests__/scope.bench.ts +0 -39
  186. package/__tests__/scope.test.ts +0 -374
  187. package/__tests__/serialization.test.ts +0 -19
  188. package/__tests__/string.test.ts +0 -52
  189. package/__tests__/submodule.test.ts +0 -86
  190. package/__tests__/this.test.ts +0 -83
  191. package/__tests__/thunk.test.ts +0 -86
  192. package/__tests__/traverse.test.ts +0 -141
  193. package/__tests__/tsGenerics.test.ts +0 -12
  194. package/__tests__/type.test.ts +0 -36
  195. package/__tests__/typeReference.test.ts +0 -39
  196. package/__tests__/unenclosed.test.ts +0 -136
  197. package/__tests__/union.test.ts +0 -214
  198. package/ark.ts +0 -47
  199. package/generic.ts +0 -69
  200. package/main.ts +0 -6
  201. package/match.ts +0 -218
  202. package/module.ts +0 -15
  203. package/out/ark.d.ts +0 -28
  204. package/out/ark.js +0 -10
  205. package/out/ark.js.map +0 -1
  206. package/out/generic.js.map +0 -1
  207. package/out/main.d.ts +0 -6
  208. package/out/main.js +0 -5
  209. package/out/main.js.map +0 -1
  210. package/out/match.d.ts +0 -71
  211. package/out/match.js +0 -52
  212. package/out/match.js.map +0 -1
  213. package/out/module.js.map +0 -1
  214. package/out/parser/definition.js.map +0 -1
  215. package/out/parser/generic.d.ts +0 -16
  216. package/out/parser/generic.js +0 -15
  217. package/out/parser/generic.js.map +0 -1
  218. package/out/parser/objectLiteral.js.map +0 -1
  219. package/out/parser/semantic/bounds.js.map +0 -1
  220. package/out/parser/semantic/divisor.js.map +0 -1
  221. package/out/parser/semantic/infer.js.map +0 -1
  222. package/out/parser/semantic/utils.js.map +0 -1
  223. package/out/parser/semantic/validate.js.map +0 -1
  224. package/out/parser/string/reduce/dynamic.js.map +0 -1
  225. package/out/parser/string/reduce/shared.js.map +0 -1
  226. package/out/parser/string/reduce/static.js.map +0 -1
  227. package/out/parser/string/shift/operand/date.js.map +0 -1
  228. package/out/parser/string/shift/operand/enclosed.js.map +0 -1
  229. package/out/parser/string/shift/operand/genericArgs.js.map +0 -1
  230. package/out/parser/string/shift/operand/operand.js.map +0 -1
  231. package/out/parser/string/shift/operand/unenclosed.js.map +0 -1
  232. package/out/parser/string/shift/operator/bounds.js.map +0 -1
  233. package/out/parser/string/shift/operator/divisor.js.map +0 -1
  234. package/out/parser/string/shift/operator/operator.js.map +0 -1
  235. package/out/parser/string/shift/scanner.js.map +0 -1
  236. package/out/parser/string/string.js.map +0 -1
  237. package/out/parser/tuple.js.map +0 -1
  238. package/out/scope.js.map +0 -1
  239. package/out/type.js.map +0 -1
  240. package/parser/definition.ts +0 -143
  241. package/parser/generic.ts +0 -66
  242. package/parser/objectLiteral.ts +0 -233
  243. package/parser/semantic/bounds.ts +0 -62
  244. package/parser/semantic/divisor.ts +0 -11
  245. package/parser/semantic/infer.ts +0 -141
  246. package/parser/semantic/utils.ts +0 -15
  247. package/parser/semantic/validate.ts +0 -117
  248. package/parser/string/reduce/dynamic.ts +0 -208
  249. package/parser/string/reduce/shared.ts +0 -99
  250. package/parser/string/reduce/static.ts +0 -296
  251. package/parser/string/shift/operand/date.ts +0 -55
  252. package/parser/string/shift/operand/enclosed.ts +0 -123
  253. package/parser/string/shift/operand/genericArgs.ts +0 -128
  254. package/parser/string/shift/operand/operand.ts +0 -42
  255. package/parser/string/shift/operand/unenclosed.ts +0 -217
  256. package/parser/string/shift/operator/bounds.ts +0 -270
  257. package/parser/string/shift/operator/divisor.ts +0 -33
  258. package/parser/string/shift/operator/operator.ts +0 -71
  259. package/parser/string/shift/scanner.ts +0 -210
  260. package/parser/string/string.ts +0 -85
  261. package/parser/tuple.ts +0 -542
  262. package/scope.ts +0 -291
  263. package/type.ts +0 -266
package/parser/tuple.ts DELETED
@@ -1,542 +0,0 @@
1
- import {
2
- type BaseMeta,
3
- type Morph,
4
- type MutableInner,
5
- type Node,
6
- type Out,
7
- type Predicate,
8
- type RawSchema,
9
- type UnionChildKind,
10
- type UnknownSchema,
11
- type distillConstrainableIn,
12
- type distillConstrainableOut,
13
- type inferIntersection,
14
- type inferMorphOut,
15
- type inferNarrow,
16
- jsObjects,
17
- makeRootAndArrayPropertiesMutable,
18
- tsKeywords
19
- } from "@arktype/schema"
20
- import {
21
- type BuiltinObjectKind,
22
- type Constructor,
23
- type Domain,
24
- type ErrorMessage,
25
- append,
26
- type array,
27
- type conform,
28
- objectKindOrDomainOf,
29
- type show,
30
- throwParseError
31
- } from "@arktype/util"
32
- import type { ParseContext } from "../scope.js"
33
- import type { inferDefinition, validateDefinition } from "./definition.js"
34
- import type { InfixOperator, PostfixExpression } from "./semantic/infer.js"
35
- import { writeMissingRightOperandMessage } from "./string/shift/operand/unenclosed.js"
36
- import type { BaseCompletions } from "./string/string.js"
37
-
38
- export const parseTuple = (def: array, ctx: ParseContext): RawSchema =>
39
- maybeParseTupleExpression(def, ctx) ?? parseTupleLiteral(def, ctx)
40
-
41
- export const parseTupleLiteral = (def: array, ctx: ParseContext): RawSchema => {
42
- let sequences: MutableInner<"sequence">[] = [{}]
43
- let i = 0
44
- while (i < def.length) {
45
- let spread = false
46
- let optional = false
47
- if (def[i] === "..." && i < def.length - 1) {
48
- spread = true
49
- i++
50
- }
51
-
52
- const element = ctx.$.parse(def[i], ctx)
53
-
54
- i++
55
- if (def[i] === "?") {
56
- if (spread) return throwParseError(spreadOptionalMessage)
57
-
58
- optional = true
59
- i++
60
- }
61
- if (spread) {
62
- if (!element.extends(jsObjects.Array))
63
- return throwParseError(writeNonArraySpreadMessage(element.expression))
64
-
65
- // a spread must be distributed over branches e.g.:
66
- // def: [string, ...(number[] | [true, false])]
67
- // nodes: [string, ...number[]] | [string, true, false]
68
- sequences = sequences.flatMap(base =>
69
- // since appendElement mutates base, we have to shallow-ish clone it for each branch
70
- element.branches.map(branch =>
71
- appendSpreadBranch(makeRootAndArrayPropertiesMutable(base), branch)
72
- )
73
- )
74
- } else {
75
- sequences = sequences.map(base =>
76
- appendElement(base, optional ? "optional" : "required", element)
77
- )
78
- }
79
- }
80
- return ctx.$.raw.schema(
81
- sequences.map(
82
- sequence =>
83
- ({
84
- proto: Array,
85
- sequence
86
- }) as const
87
- )
88
- )
89
- }
90
-
91
- type ElementKind = "optional" | "required" | "variadic"
92
-
93
- const appendElement = (
94
- base: MutableInner<"sequence">,
95
- kind: ElementKind,
96
- element: UnknownSchema
97
- ): MutableInner<"sequence"> => {
98
- switch (kind) {
99
- case "required":
100
- if (base.optional)
101
- // e.g. [string?, number]
102
- return throwParseError(requiredPostOptionalMessage)
103
- if (base.variadic) {
104
- // e.g. [...string[], number]
105
- base.postfix = append(base.postfix, element)
106
- } else {
107
- // e.g. [string, number]
108
- base.prefix = append(base.prefix, element)
109
- }
110
- return base
111
- case "optional":
112
- if (base.variadic)
113
- // e.g. [...string[], number?]
114
- return throwParseError(optionalPostVariadicMessage)
115
- // e.g. [string, number?]
116
- base.optional = append(base.optional, element)
117
- return base
118
- case "variadic":
119
- // e.g. [...string[], number, ...string[]]
120
- if (base.postfix) throwParseError(multipleVariadicMesage)
121
- if (base.variadic) {
122
- if (!base.variadic.equals(element)) {
123
- // e.g. [...string[], ...number[]]
124
- throwParseError(multipleVariadicMesage)
125
- }
126
- // e.g. [...string[], ...string[]]
127
- // do nothing, second spread doesn't change the type
128
- } else {
129
- // e.g. [string, ...number[]]
130
- base.variadic = element.raw
131
- }
132
- return base
133
- }
134
- }
135
-
136
- const appendSpreadBranch = (
137
- base: MutableInner<"sequence">,
138
- branch: Node<UnionChildKind>
139
- ): MutableInner<"sequence"> => {
140
- const spread = branch.firstReferenceOfKind("sequence")
141
- if (!spread) {
142
- // the only array with no sequence reference is unknown[]
143
- return appendElement(base, "variadic", tsKeywords.unknown)
144
- }
145
- spread.prefix.forEach(node => appendElement(base, "required", node))
146
- spread.optional.forEach(node => appendElement(base, "optional", node))
147
- spread.variadic && appendElement(base, "variadic", spread.variadic)
148
- spread.postfix.forEach(node => appendElement(base, "required", node))
149
- return base
150
- }
151
-
152
- const maybeParseTupleExpression = (
153
- def: array,
154
- ctx: ParseContext
155
- ): RawSchema | undefined => {
156
- const tupleExpressionResult =
157
- isIndexZeroExpression(def) ? prefixParsers[def[0]](def as never, ctx)
158
- : isIndexOneExpression(def) ? indexOneParsers[def[1]](def as never, ctx)
159
- : undefined
160
- return tupleExpressionResult
161
-
162
- // TODO: remove
163
- // return tupleExpressionResult.isNever()
164
- // ? throwParseError(
165
- // writeUnsatisfiableExpressionError(
166
- // def
167
- // .map((def) =>
168
- // typeof def === "string" ? def : printable(def)
169
- // )
170
- // .join(" ")
171
- // )
172
- // )
173
- // : tupleExpressionResult
174
- }
175
-
176
- // It is *extremely* important we use readonly any time we check a tuple against
177
- // something like this. Not doing so will always cause the check to fail, since
178
- // def is declared as a const parameter.
179
- type InfixExpression = readonly [unknown, InfixOperator, ...unknown[]]
180
-
181
- export type validateTuple<def extends array, $, args> =
182
- def extends IndexZeroExpression ? validatePrefixExpression<def, $, args>
183
- : def extends PostfixExpression ? validatePostfixExpression<def, $, args>
184
- : def extends InfixExpression ? validateInfixExpression<def, $, args>
185
- : def extends (
186
- readonly ["", ...unknown[]] | readonly [unknown, "", ...unknown[]]
187
- ) ?
188
- readonly [
189
- def[0] extends "" ? BaseCompletions<$, args, IndexZeroOperator | "...">
190
- : def[0],
191
- def[1] extends "" ?
192
- BaseCompletions<$, args, IndexOneOperator | "..." | "?">
193
- : def[1]
194
- ]
195
- : validateTupleLiteral<def, $, args>
196
-
197
- export type validateTupleLiteral<def extends array, $, args> = Readonly<
198
- parseSequence<def, $, args>["validated"]
199
- >
200
-
201
- type inferTupleLiteral<def extends array, $, args> = parseSequence<
202
- def,
203
- $,
204
- args
205
- >["inferred"]
206
-
207
- type SequenceParseState = {
208
- unscanned: array
209
- inferred: array
210
- validated: array
211
- }
212
-
213
- type parseSequence<def extends array, $, args> = parseNextElement<
214
- {
215
- unscanned: def
216
- inferred: []
217
- validated: []
218
- },
219
- $,
220
- args
221
- >
222
-
223
- type PreparsedElement = {
224
- head: unknown
225
- tail: array
226
- optional: boolean
227
- spread: boolean
228
- }
229
-
230
- namespace PreparsedElement {
231
- export type from<result extends PreparsedElement> = result
232
- }
233
-
234
- type preparseNextElement<s extends SequenceParseState> =
235
- s["unscanned"] extends readonly ["...", infer head, ...infer tail] ?
236
- tail extends readonly ["?", ...infer postOptionalTail] ?
237
- PreparsedElement.from<{
238
- head: head
239
- tail: postOptionalTail
240
- // this will be an error we have to handle
241
- optional: true
242
- spread: true
243
- }>
244
- : PreparsedElement.from<{
245
- head: head
246
- tail: tail
247
- optional: false
248
- spread: true
249
- }>
250
- : s["unscanned"] extends readonly [infer head, "?", ...infer tail] ?
251
- PreparsedElement.from<{
252
- head: head
253
- tail: tail
254
- optional: true
255
- spread: false
256
- }>
257
- : s["unscanned"] extends readonly [infer head, ...infer tail] ?
258
- PreparsedElement.from<{
259
- head: head
260
- tail: tail
261
- optional: false
262
- spread: false
263
- }>
264
- : null
265
-
266
- type parseNextElement<s extends SequenceParseState, $, args> =
267
- preparseNextElement<s> extends infer next extends PreparsedElement ?
268
- parseNextElement<
269
- {
270
- unscanned: next["tail"]
271
- inferred: next["spread"] extends true ?
272
- [
273
- ...s["inferred"],
274
- ...conform<inferDefinition<next["head"], $, args>, array>
275
- ]
276
- : next["optional"] extends true ?
277
- [...s["inferred"], inferDefinition<next["head"], $, args>?]
278
- : [...s["inferred"], inferDefinition<next["head"], $, args>]
279
- validated: [
280
- ...s["validated"],
281
- ...(next["spread"] extends true ?
282
- [
283
- inferDefinition<next["head"], $, args> extends (
284
- infer spreadOperand extends array
285
- ) ?
286
- [number, number] extends (
287
- [s["inferred"]["length"], spreadOperand["length"]]
288
- ) ?
289
- ErrorMessage<multipleVariadicMessage>
290
- : "..."
291
- : ErrorMessage<writeNonArraySpreadMessage<next["head"]>>
292
- ]
293
- : []),
294
- [next["optional"] | next["spread"], "?"] extends (
295
- [false, s["validated"][number]]
296
- ) ?
297
- ErrorMessage<requiredPostOptionalMessage>
298
- : validateDefinition<next["head"], $, args>,
299
- ...(next["optional"] extends true ?
300
- [
301
- next["spread"] extends true ? ErrorMessage<spreadOptionalMessage>
302
- : number extends s["inferred"]["length"] ?
303
- ErrorMessage<optionalPostVariadicMessage>
304
- : "?"
305
- ]
306
- : [])
307
- ]
308
- },
309
- $,
310
- args
311
- >
312
- : s
313
-
314
- export const writeNonArraySpreadMessage = <operand extends string>(
315
- operand: operand
316
- ): writeNonArraySpreadMessage<operand> =>
317
- `Spread element must be an array (was ${operand})` as never
318
-
319
- type writeNonArraySpreadMessage<operand> =
320
- `Spread element must be an array${operand extends string ? `(was ${operand})`
321
- : ""}`
322
-
323
- export const multipleVariadicMesage =
324
- "A tuple may have at most one variadic element"
325
-
326
- type multipleVariadicMessage = typeof multipleVariadicMesage
327
-
328
- export const requiredPostOptionalMessage =
329
- "A required element may not follow an optional element"
330
-
331
- type requiredPostOptionalMessage = typeof requiredPostOptionalMessage
332
-
333
- export const optionalPostVariadicMessage =
334
- "An optional element may not follow a variadic element"
335
-
336
- type optionalPostVariadicMessage = typeof optionalPostVariadicMessage
337
-
338
- export const spreadOptionalMessage = "A spread element cannot be optional"
339
-
340
- type spreadOptionalMessage = typeof optionalPostVariadicMessage
341
-
342
- export type inferTuple<def extends array, $, args> =
343
- def extends TupleExpression ? inferTupleExpression<def, $, args>
344
- : inferTupleLiteral<def, $, args>
345
-
346
- export type inferTupleExpression<def extends TupleExpression, $, args> =
347
- def[1] extends "[]" ? inferDefinition<def[0], $, args>[]
348
- : def[1] extends "&" ?
349
- inferIntersection<
350
- inferDefinition<def[0], $, args>,
351
- inferDefinition<def[2], $, args>
352
- >
353
- : def[1] extends "|" ?
354
- inferDefinition<def[0], $, args> | inferDefinition<def[2], $, args>
355
- : def[1] extends ":" ? inferNarrow<inferDefinition<def[0], $, args>, def[2]>
356
- : def[1] extends "=>" ? parseMorph<def[0], def[2], $, args>
357
- : def[1] extends "@" ? inferDefinition<def[0], $, args>
358
- : def extends readonly ["===", ...infer values] ? values[number]
359
- : def extends (
360
- readonly ["instanceof", ...infer constructors extends Constructor[]]
361
- ) ?
362
- InstanceType<constructors[number]>
363
- : def[0] extends "keyof" ? inferKeyOfExpression<def[1], $, args>
364
- : never
365
-
366
- export type validatePrefixExpression<def extends IndexZeroExpression, $, args> =
367
- def["length"] extends 1 ? readonly [writeMissingRightOperandMessage<def[0]>]
368
- : def[0] extends "keyof" ?
369
- readonly [def[0], validateDefinition<def[1], $, args>]
370
- : def[0] extends "===" ? readonly [def[0], ...unknown[]]
371
- : def[0] extends "instanceof" ? readonly [def[0], ...Constructor[]]
372
- : never
373
-
374
- export type validatePostfixExpression<
375
- def extends PostfixExpression,
376
- $,
377
- args
378
- // conform here is needed to preserve completions for shallow tuple
379
- // expressions at index 1 after TS 5.1
380
- > = conform<def, readonly [validateDefinition<def[0], $, args>, "[]"]>
381
-
382
- export type validateInfixExpression<def extends InfixExpression, $, args> =
383
- def["length"] extends 2 ?
384
- readonly [def[0], writeMissingRightOperandMessage<def[1]>]
385
- : readonly [
386
- validateDefinition<def[0], $, args>,
387
- def[1],
388
- def[1] extends "|" ? validateDefinition<def[2], $, args>
389
- : def[1] extends "&" ? validateDefinition<def[2], $, args>
390
- : def[1] extends ":" ?
391
- Predicate<distillConstrainableIn<inferDefinition<def[0], $, args>>>
392
- : def[1] extends "=>" ?
393
- Morph<
394
- distillConstrainableOut<inferDefinition<def[0], $, args>>,
395
- unknown
396
- >
397
- : def[1] extends "@" ? BaseMeta | string
398
- : validateDefinition<def[2], $, args>
399
- ]
400
-
401
- export type UnparsedTupleExpressionInput = {
402
- instanceof: Constructor
403
- "===": unknown
404
- }
405
-
406
- export type UnparsedTupleOperator = show<keyof UnparsedTupleExpressionInput>
407
-
408
- export const parseKeyOfTuple: PrefixParser<"keyof"> = (def, ctx) =>
409
- ctx.$.parse(def[1], ctx).keyof()
410
-
411
- export type inferKeyOfExpression<operandDef, $, args> = show<
412
- keyof inferDefinition<operandDef, $, args>
413
- >
414
-
415
- const parseBranchTuple: PostfixParser<"|" | "&"> = (def, ctx) => {
416
- if (def[2] === undefined)
417
- return throwParseError(writeMissingRightOperandMessage(def[1], ""))
418
-
419
- const l = ctx.$.parse(def[0], ctx)
420
- const r = ctx.$.parse(def[2], ctx)
421
- return def[1] === "&" ? l.and(r) : l.or(r)
422
- }
423
-
424
- const parseArrayTuple: PostfixParser<"[]"> = (def, ctx) =>
425
- ctx.$.parse(def[0], ctx).array()
426
-
427
- export type PostfixParser<token extends IndexOneOperator> = (
428
- def: IndexOneExpression<token>,
429
- ctx: ParseContext
430
- ) => RawSchema
431
-
432
- export type PrefixParser<token extends IndexZeroOperator> = (
433
- def: IndexZeroExpression<token>,
434
- ctx: ParseContext
435
- ) => RawSchema
436
-
437
- export type TupleExpression = IndexZeroExpression | IndexOneExpression
438
-
439
- export type TupleExpressionOperator = IndexZeroOperator | IndexOneOperator
440
-
441
- export type IndexOneOperator = TuplePostfixOperator | TupleInfixOperator
442
-
443
- export type TuplePostfixOperator = "[]"
444
-
445
- export type TupleInfixOperator = "&" | "|" | "=>" | ":" | "@"
446
-
447
- export type IndexOneExpression<
448
- token extends IndexOneOperator = IndexOneOperator
449
- > = readonly [unknown, token, ...unknown[]]
450
-
451
- const isIndexOneExpression = (def: array): def is IndexOneExpression =>
452
- indexOneParsers[def[1] as IndexOneOperator] !== undefined
453
-
454
- export const parseMorphTuple: PostfixParser<"=>"> = (def, ctx) => {
455
- if (typeof def[2] !== "function") {
456
- return throwParseError(
457
- writeMalformedFunctionalExpressionMessage("=>", def[2])
458
- )
459
- }
460
- // TODO: nested morphs?
461
- return ctx.$.parse(def[0], ctx).pipe(def[2] as Morph)
462
- }
463
-
464
- export const writeMalformedFunctionalExpressionMessage = (
465
- operator: ":" | "=>",
466
- value: unknown
467
- ) =>
468
- `${
469
- operator === ":" ? "Narrow" : "Morph"
470
- } expression requires a function following '${operator}' (was ${typeof value})` as const
471
-
472
- export type parseMorph<inDef, morph, $, args> =
473
- morph extends Morph ?
474
- (
475
- In: distillConstrainableIn<inferDefinition<inDef, $, args>>
476
- ) => Out<inferMorphOut<morph>>
477
- : never
478
-
479
- export const parseNarrowTuple: PostfixParser<":"> = (def, ctx) => {
480
- if (typeof def[2] !== "function") {
481
- return throwParseError(
482
- writeMalformedFunctionalExpressionMessage(":", def[2])
483
- )
484
- }
485
- return ctx.$.parse(def[0], ctx).constrain("predicate", def[2] as Predicate)
486
- }
487
-
488
- const parseAttributeTuple: PostfixParser<"@"> = (def, ctx) =>
489
- ctx.$.parse(def[0], ctx).configureShallowDescendants(def[2] as never)
490
-
491
- const indexOneParsers: {
492
- [token in IndexOneOperator]: PostfixParser<token>
493
- } = {
494
- "|": parseBranchTuple,
495
- "&": parseBranchTuple,
496
- "[]": parseArrayTuple,
497
- ":": parseNarrowTuple,
498
- "=>": parseMorphTuple,
499
- "@": parseAttributeTuple
500
- }
501
-
502
- export type IndexZeroOperator = "keyof" | "instanceof" | "==="
503
-
504
- export type IndexZeroExpression<
505
- token extends IndexZeroOperator = IndexZeroOperator
506
- > = readonly [token, ...unknown[]]
507
-
508
- const prefixParsers: {
509
- [token in IndexZeroOperator]: PrefixParser<token>
510
- } = {
511
- keyof: parseKeyOfTuple,
512
- instanceof: (def, ctx) => {
513
- if (typeof def[1] !== "function") {
514
- return throwParseError(
515
- writeInvalidConstructorMessage(objectKindOrDomainOf(def[1]))
516
- )
517
- }
518
- const branches = def
519
- .slice(1)
520
- .map(ctor =>
521
- typeof ctor === "function" ?
522
- ctx.$.node("proto", { proto: ctor as Constructor })
523
- : throwParseError(
524
- writeInvalidConstructorMessage(objectKindOrDomainOf(ctor))
525
- )
526
- )
527
- return branches.length === 1 ?
528
- branches[0]
529
- : ctx.$.node("union", { branches })
530
- },
531
- "===": (def, ctx) => ctx.$.units(def.slice(1))
532
- }
533
-
534
- const isIndexZeroExpression = (def: array): def is IndexZeroExpression =>
535
- prefixParsers[def[0] as IndexZeroOperator] !== undefined
536
-
537
- export const writeInvalidConstructorMessage = <
538
- actual extends Domain | BuiltinObjectKind
539
- >(
540
- actual: actual
541
- ) =>
542
- `Expected a constructor following 'instanceof' operator (was ${actual})` as const