arktype 2.0.0-dev.1 → 2.0.0-dev.11

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 (221) hide show
  1. package/out/api.d.ts +6 -0
  2. package/out/api.js +5 -0
  3. package/out/ark.d.ts +15 -39
  4. package/out/ark.js +9 -18
  5. package/out/config.d.ts +1 -0
  6. package/out/config.js +2 -0
  7. package/out/generic.d.ts +21 -0
  8. package/out/generic.js +24 -0
  9. package/out/main.d.ts +6 -7
  10. package/out/main.js +4 -3
  11. package/out/match.d.ts +57 -34
  12. package/out/match.js +51 -17
  13. package/out/module.d.ts +12 -0
  14. package/out/module.js +3 -0
  15. package/out/parser/definition.d.ts +14 -14
  16. package/out/parser/definition.js +10 -13
  17. package/out/parser/generic.d.ts +5 -7
  18. package/out/parser/generic.js +7 -11
  19. package/out/parser/objectLiteral.d.ts +44 -18
  20. package/out/parser/objectLiteral.js +57 -51
  21. package/out/parser/semantic/bounds.d.ts +10 -8
  22. package/out/parser/semantic/bounds.js +0 -1
  23. package/out/parser/semantic/divisor.d.ts +5 -7
  24. package/out/parser/semantic/divisor.js +0 -1
  25. package/out/parser/semantic/infer.d.ts +24 -0
  26. package/out/parser/semantic/infer.js +1 -0
  27. package/out/parser/semantic/utils.d.ts +3 -7
  28. package/out/parser/semantic/utils.js +0 -1
  29. package/out/parser/semantic/validate.d.ts +16 -13
  30. package/out/parser/semantic/validate.js +1 -1
  31. package/out/parser/string/reduce/dynamic.d.ts +13 -13
  32. package/out/parser/string/reduce/dynamic.js +31 -26
  33. package/out/parser/string/reduce/shared.d.ts +14 -28
  34. package/out/parser/string/reduce/shared.js +4 -3
  35. package/out/parser/string/reduce/static.d.ts +27 -24
  36. package/out/parser/string/reduce/static.js +0 -1
  37. package/out/parser/string/shift/operand/date.d.ts +2 -2
  38. package/out/parser/string/shift/operand/date.js +4 -7
  39. package/out/parser/string/shift/operand/enclosed.d.ts +3 -4
  40. package/out/parser/string/shift/operand/enclosed.js +14 -14
  41. package/out/parser/string/shift/operand/genericArgs.d.ts +9 -10
  42. package/out/parser/string/shift/operand/genericArgs.js +6 -10
  43. package/out/parser/string/shift/operand/operand.d.ts +3 -4
  44. package/out/parser/string/shift/operand/operand.js +8 -12
  45. package/out/parser/string/shift/operand/unenclosed.d.ts +16 -21
  46. package/out/parser/string/shift/operand/unenclosed.js +24 -36
  47. package/out/parser/string/shift/operator/bounds.d.ts +8 -12
  48. package/out/parser/string/shift/operator/bounds.js +45 -56
  49. package/out/parser/string/shift/operator/divisor.d.ts +1 -3
  50. package/out/parser/string/shift/operator/divisor.js +1 -3
  51. package/out/parser/string/shift/operator/operator.d.ts +1 -2
  52. package/out/parser/string/shift/operator/operator.js +12 -19
  53. package/out/parser/string/shift/scanner.d.ts +2 -2
  54. package/out/parser/string/shift/scanner.js +14 -16
  55. package/out/parser/string/string.d.ts +8 -6
  56. package/out/parser/string/string.js +1 -3
  57. package/out/parser/tuple.d.ts +102 -43
  58. package/out/parser/tuple.js +119 -87
  59. package/out/scope.d.ts +44 -119
  60. package/out/scope.js +39 -209
  61. package/out/type.d.ts +43 -64
  62. package/out/type.js +30 -131
  63. package/package.json +12 -10
  64. package/__tests__/array.test.ts +0 -272
  65. package/__tests__/astUtils.test.ts +0 -17
  66. package/__tests__/badDefinitionType.test.ts +0 -51
  67. package/__tests__/basis.test.ts +0 -46
  68. package/__tests__/bounds.test.ts +0 -388
  69. package/__tests__/cast.test.ts +0 -32
  70. package/__tests__/completions.test.ts +0 -46
  71. package/__tests__/config.test.ts +0 -168
  72. package/__tests__/cyclic.test.ts +0 -144
  73. package/__tests__/dateLiteral.test.ts +0 -32
  74. package/__tests__/declared.test.ts +0 -123
  75. package/__tests__/discrimination.test.ts +0 -74
  76. package/__tests__/divisor.test.ts +0 -90
  77. package/__tests__/enclosed.test.ts +0 -93
  78. package/__tests__/examples.test.ts +0 -56
  79. package/__tests__/expressions.test.ts +0 -138
  80. package/__tests__/generateBenchData.ts +0 -89
  81. package/__tests__/generated/cyclic.ts +0 -3058
  82. package/__tests__/generic.test.ts +0 -294
  83. package/__tests__/group.test.ts +0 -72
  84. package/__tests__/hkt.test.ts +0 -22
  85. package/__tests__/imports.test.ts +0 -113
  86. package/__tests__/instanceof.test.ts +0 -102
  87. package/__tests__/intersection.test.ts +0 -104
  88. package/__tests__/jsdoc.test.ts +0 -64
  89. package/__tests__/keyTraversal.test.ts +0 -73
  90. package/__tests__/keyof.test.ts +0 -134
  91. package/__tests__/keywords.test.ts +0 -188
  92. package/__tests__/literal.test.ts +0 -51
  93. package/__tests__/match.test.ts +0 -34
  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 -300
  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 -116
  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.map +0 -1
  125. package/out/main.js.map +0 -1
  126. package/out/match.d.ts.map +0 -1
  127. package/out/match.js.map +0 -1
  128. package/out/parser/definition.d.ts.map +0 -1
  129. package/out/parser/definition.js.map +0 -1
  130. package/out/parser/generic.d.ts.map +0 -1
  131. package/out/parser/generic.js.map +0 -1
  132. package/out/parser/objectLiteral.d.ts.map +0 -1
  133. package/out/parser/objectLiteral.js.map +0 -1
  134. package/out/parser/semantic/bounds.d.ts.map +0 -1
  135. package/out/parser/semantic/bounds.js.map +0 -1
  136. package/out/parser/semantic/divisor.d.ts.map +0 -1
  137. package/out/parser/semantic/divisor.js.map +0 -1
  138. package/out/parser/semantic/intersections.d.ts +0 -14
  139. package/out/parser/semantic/intersections.d.ts.map +0 -1
  140. package/out/parser/semantic/intersections.js +0 -2
  141. package/out/parser/semantic/intersections.js.map +0 -1
  142. package/out/parser/semantic/semantic.d.ts +0 -33
  143. package/out/parser/semantic/semantic.d.ts.map +0 -1
  144. package/out/parser/semantic/semantic.js +0 -2
  145. package/out/parser/semantic/semantic.js.map +0 -1
  146. package/out/parser/semantic/utils.d.ts.map +0 -1
  147. package/out/parser/semantic/utils.js.map +0 -1
  148. package/out/parser/semantic/validate.d.ts.map +0 -1
  149. package/out/parser/semantic/validate.js.map +0 -1
  150. package/out/parser/shared.d.ts +0 -54
  151. package/out/parser/shared.d.ts.map +0 -1
  152. package/out/parser/shared.js +0 -47
  153. package/out/parser/shared.js.map +0 -1
  154. package/out/parser/string/reduce/dynamic.d.ts.map +0 -1
  155. package/out/parser/string/reduce/dynamic.js.map +0 -1
  156. package/out/parser/string/reduce/shared.d.ts.map +0 -1
  157. package/out/parser/string/reduce/shared.js.map +0 -1
  158. package/out/parser/string/reduce/static.d.ts.map +0 -1
  159. package/out/parser/string/reduce/static.js.map +0 -1
  160. package/out/parser/string/shift/operand/date.d.ts.map +0 -1
  161. package/out/parser/string/shift/operand/date.js.map +0 -1
  162. package/out/parser/string/shift/operand/enclosed.d.ts.map +0 -1
  163. package/out/parser/string/shift/operand/enclosed.js.map +0 -1
  164. package/out/parser/string/shift/operand/genericArgs.d.ts.map +0 -1
  165. package/out/parser/string/shift/operand/genericArgs.js.map +0 -1
  166. package/out/parser/string/shift/operand/operand.d.ts.map +0 -1
  167. package/out/parser/string/shift/operand/operand.js.map +0 -1
  168. package/out/parser/string/shift/operand/unenclosed.d.ts.map +0 -1
  169. package/out/parser/string/shift/operand/unenclosed.js.map +0 -1
  170. package/out/parser/string/shift/operator/bounds.d.ts.map +0 -1
  171. package/out/parser/string/shift/operator/bounds.js.map +0 -1
  172. package/out/parser/string/shift/operator/divisor.d.ts.map +0 -1
  173. package/out/parser/string/shift/operator/divisor.js.map +0 -1
  174. package/out/parser/string/shift/operator/operator.d.ts.map +0 -1
  175. package/out/parser/string/shift/operator/operator.js.map +0 -1
  176. package/out/parser/string/shift/scanner.d.ts.map +0 -1
  177. package/out/parser/string/shift/scanner.js.map +0 -1
  178. package/out/parser/string/string.d.ts.map +0 -1
  179. package/out/parser/string/string.js.map +0 -1
  180. package/out/parser/tuple.d.ts.map +0 -1
  181. package/out/parser/tuple.js.map +0 -1
  182. package/out/scope.d.ts.map +0 -1
  183. package/out/scope.js.map +0 -1
  184. package/out/test.d.ts +0 -2
  185. package/out/test.d.ts.map +0 -1
  186. package/out/test.js +0 -7
  187. package/out/test.js.map +0 -1
  188. package/out/tsconfig.build.tsbuildinfo +0 -1
  189. package/out/type.d.ts.map +0 -1
  190. package/out/type.js.map +0 -1
  191. package/out/util.d.ts +0 -19
  192. package/out/util.d.ts.map +0 -1
  193. package/out/util.js +0 -7
  194. package/out/util.js.map +0 -1
  195. package/parser/definition.ts +0 -159
  196. package/parser/generic.ts +0 -68
  197. package/parser/objectLiteral.ts +0 -197
  198. package/parser/semantic/bounds.ts +0 -61
  199. package/parser/semantic/divisor.ts +0 -13
  200. package/parser/semantic/intersections.ts +0 -46
  201. package/parser/semantic/semantic.ts +0 -144
  202. package/parser/semantic/utils.ts +0 -26
  203. package/parser/semantic/validate.ts +0 -109
  204. package/parser/shared.ts +0 -152
  205. package/parser/string/reduce/dynamic.ts +0 -196
  206. package/parser/string/reduce/shared.ts +0 -101
  207. package/parser/string/reduce/static.ts +0 -298
  208. package/parser/string/shift/operand/date.ts +0 -54
  209. package/parser/string/shift/operand/enclosed.ts +0 -118
  210. package/parser/string/shift/operand/genericArgs.ts +0 -127
  211. package/parser/string/shift/operand/operand.ts +0 -48
  212. package/parser/string/shift/operand/unenclosed.ts +0 -268
  213. package/parser/string/shift/operator/bounds.ts +0 -264
  214. package/parser/string/shift/operator/divisor.ts +0 -36
  215. package/parser/string/shift/operator/operator.ts +0 -82
  216. package/parser/string/shift/scanner.ts +0 -209
  217. package/parser/string/string.ts +0 -90
  218. package/parser/tuple.ts +0 -474
  219. package/scope.ts +0 -574
  220. package/type.ts +0 -347
  221. package/util.ts +0 -36
@@ -1,197 +0,0 @@
1
- import { keywords, schema, type Inner, type TypeNode } from "@arktype/schema"
2
- import {
3
- printable,
4
- throwParseError,
5
- type Dict,
6
- type ErrorMessage,
7
- type evaluate,
8
- type merge
9
- } from "@arktype/util"
10
-
11
- import type { ParseContext } from "../scope.js"
12
- import type { inferDefinition, validateDefinition } from "./definition.js"
13
- import type { astToString } from "./semantic/utils.js"
14
- import type { validateString } from "./semantic/validate.js"
15
- import {
16
- parseEntry,
17
- type EntryParseResult,
18
- type IndexedKey,
19
- type OptionalValue,
20
- type validateObjectValue
21
- } from "./shared.js"
22
-
23
- const stringAndSymbolicEntriesOf = (o: Record<string | symbol, unknown>) => [
24
- ...Object.entries(o),
25
- ...Object.getOwnPropertySymbols(o).map((k) => [k, o[k]] as const)
26
- ]
27
-
28
- export const parseObjectLiteral = (def: Dict, ctx: ParseContext): TypeNode => {
29
- const required: Inner<"required">[] = []
30
- const optional: Inner<"optional">[] = []
31
-
32
- // We only allow a spread operator to be used as the first key in an object
33
- // because to match JS behavior any keys before the spread are overwritten
34
- // by the values in the target object, so there'd be no useful purpose in having it
35
- // anywhere except for the beginning.
36
- // Discussion in ArkType Discord:
37
- // https://discord.com/channels/957797212103016458/1103023445035462678/1182814502471860334
38
- let hasSeenFirstKey = false
39
-
40
- const entries = stringAndSymbolicEntriesOf(def)
41
- for (const entry of entries) {
42
- const result = parseEntry(entry)
43
-
44
- if (result.kind === "spread") {
45
- if (hasSeenFirstKey) {
46
- return throwParseError(
47
- "Spread operator may only be used as the first key in an object"
48
- )
49
- }
50
-
51
- const spreadNode = ctx.scope.parse(result.innerValue, ctx)
52
-
53
- if (
54
- spreadNode.kind !== "intersection" ||
55
- !spreadNode.extends(keywords.object)
56
- ) {
57
- return throwParseError(
58
- writeInvalidSpreadTypeMessage(printable(result.innerValue))
59
- )
60
- }
61
-
62
- // For each key on spreadNode, add it to our object.
63
- // We filter out keys from the spreadNode that will be defined later on this same object
64
- // because the currently parsed definition will overwrite them.
65
- const requiredEntriesFromSpread = (spreadNode.required ?? []).filter(
66
- (e) => !entries.some(([k]) => k === e.key)
67
- )
68
-
69
- const optionalEntriesFromSpread = (spreadNode.optional ?? []).filter(
70
- (e) => !entries.some(([k]) => k === e.key)
71
- )
72
-
73
- required.push(...requiredEntriesFromSpread)
74
- optional.push(...optionalEntriesFromSpread)
75
-
76
- continue
77
- }
78
-
79
- ctx.path.push(
80
- `${
81
- typeof result.innerKey === "symbol"
82
- ? `[${printable(result.innerKey)}]`
83
- : result.innerKey
84
- }`
85
- )
86
- const valueNode = ctx.scope.parse(result.innerValue, ctx)
87
-
88
- if (result.kind === "optional") {
89
- optional.push({
90
- key: result.innerKey,
91
- value: valueNode
92
- })
93
- } else {
94
- required.push({
95
- key: result.innerKey,
96
- value: valueNode
97
- })
98
- }
99
- ctx.path.pop()
100
-
101
- hasSeenFirstKey ||= true
102
- }
103
-
104
- return schema({
105
- basis: "object",
106
- required,
107
- optional
108
- })
109
- }
110
-
111
- /**
112
- * Infers the contents of an object literal, ignoring a spread definition
113
- * You probably want to use {@link inferObjectLiteral} instead.
114
- */
115
- type inferObjectLiteralInner<def extends object, $, args> = {
116
- // since def is a const parameter, we remove the readonly modifier here
117
- // support for builtin readonly tracked here:
118
- // https://github.com/arktypeio/arktype/issues/808
119
- -readonly [k in keyof def as nonOptionalKeyFrom<
120
- k,
121
- def[k],
122
- $,
123
- args
124
- >]: inferDefinition<def[k], $, args>
125
- } & {
126
- -readonly [k in keyof def as optionalKeyFrom<k, def[k]>]?: inferDefinition<
127
- def[k] extends OptionalValue<infer inner> ? inner : def[k],
128
- $,
129
- args
130
- >
131
- }
132
-
133
- export type inferObjectLiteral<def extends object, $, args> = evaluate<
134
- "..." extends keyof def
135
- ? merge<
136
- inferDefinition<def["..."], $, args>,
137
- inferObjectLiteralInner<def, $, args>
138
- >
139
- : inferObjectLiteralInner<def, $, args>
140
- >
141
-
142
- export type validateObjectLiteral<def, $, args> = {
143
- [k in keyof def]: k extends IndexedKey<infer indexDef>
144
- ? validateString<indexDef, $, args> extends ErrorMessage<infer message>
145
- ? // add a nominal type here to avoid allowing the error message as input
146
- indexParseError<message>
147
- : inferDefinition<indexDef, $, args> extends PropertyKey
148
- ? // if the indexDef is syntactically and semantically valid,
149
- // move on to the validating the value definition
150
- validateDefinition<def[k], $, args>
151
- : indexParseError<writeInvalidPropertyKeyMessage<indexDef>>
152
- : k extends "..."
153
- ? inferDefinition<def[k], $, args> extends object
154
- ? validateDefinition<def[k], $, args>
155
- : indexParseError<writeInvalidSpreadTypeMessage<astToString<def[k]>>>
156
- : validateObjectValue<def[k], $, args>
157
- }
158
-
159
- type nonOptionalKeyFrom<k extends PropertyKey, valueDef, $, args> = parseEntry<
160
- k,
161
- valueDef
162
- > extends infer result extends EntryParseResult
163
- ? result["kind"] extends "required"
164
- ? result["innerKey"]
165
- : result["kind"] extends "indexed"
166
- ? inferDefinition<result["innerKey"], $, args> & PropertyKey
167
- : never
168
- : never
169
-
170
- type optionalKeyFrom<k extends PropertyKey, valueDef> = parseEntry<
171
- k,
172
- valueDef
173
- > extends infer result extends EntryParseResult<"optional">
174
- ? result["innerKey"]
175
- : never
176
-
177
- declare const indexParseSymbol: unique symbol
178
-
179
- export type indexParseError<message extends string = string> = {
180
- [indexParseSymbol]: message
181
- }
182
-
183
- export const writeInvalidPropertyKeyMessage = <indexDef extends string>(
184
- indexDef: indexDef
185
- ): writeInvalidPropertyKeyMessage<indexDef> =>
186
- `Indexed key definition '${indexDef}' must be a string, number or symbol`
187
-
188
- type writeInvalidPropertyKeyMessage<indexDef extends string> =
189
- `Indexed key definition '${indexDef}' must be a string, number or symbol`
190
-
191
- export const writeInvalidSpreadTypeMessage = <def extends string>(
192
- def: def
193
- ): writeInvalidSpreadTypeMessage<def> =>
194
- `Spread operand must resolve to an object literal type (was ${def})`
195
-
196
- type writeInvalidSpreadTypeMessage<def extends string> =
197
- `Spread operand must resolve to an object literal type (was ${def})`
@@ -1,61 +0,0 @@
1
- import type { NumericallyBoundable } from "@arktype/schema"
2
- import type { ErrorMessage } from "@arktype/util"
3
- import type {
4
- Comparator,
5
- InvertedComparators,
6
- LimitLiteral
7
- } from "../string/reduce/shared.js"
8
- import type {
9
- BoundExpressionKind,
10
- writeInvalidLimitMessage,
11
- writeUnboundableMessage
12
- } from "../string/shift/operator/bounds.js"
13
- import type { inferAstBase } from "./semantic.js"
14
- import type { astToString } from "./utils.js"
15
- import type { validateAst } from "./validate.js"
16
-
17
- export type validateRange<
18
- l,
19
- comparator extends Comparator,
20
- r,
21
- $,
22
- args
23
- > = l extends LimitLiteral
24
- ? validateBound<r, InvertedComparators[comparator], l, "left", $, args>
25
- : l extends [infer leftAst, Comparator, unknown]
26
- ? ErrorMessage<writeDoubleRightBoundMessage<astToString<leftAst>>>
27
- : validateBound<l, comparator, r & LimitLiteral, "right", $, args>
28
-
29
- export type validateBound<
30
- boundedAst,
31
- comparator extends Comparator,
32
- limit extends LimitLiteral,
33
- boundKind extends BoundExpressionKind,
34
- $,
35
- args
36
- > = inferAstBase<boundedAst, $, args> extends infer bounded
37
- ? [bounded] extends [NumericallyBoundable]
38
- ? limit extends number
39
- ? validateAst<boundedAst, $, args>
40
- : ErrorMessage<writeInvalidLimitMessage<comparator, limit, boundKind>>
41
- : bounded extends Date
42
- ? // allow numeric or date literal as a Date limit
43
- validateAst<boundedAst, $, args>
44
- : ErrorMessage<
45
- writeUnboundableMessage<
46
- astToString<
47
- boundKind extends "left"
48
- ? boundedAst[0 & keyof boundedAst]
49
- : boundedAst
50
- >
51
- >
52
- >
53
- : never
54
-
55
- export const writeDoubleRightBoundMessage = <root extends string>(
56
- root: root
57
- ): writeDoubleRightBoundMessage<root> =>
58
- `Expression ${root} must have at most one right bound`
59
-
60
- type writeDoubleRightBoundMessage<root extends string> =
61
- `Expression ${root} must have at most one right bound`
@@ -1,13 +0,0 @@
1
- import type { writeIndivisibleMessage } from "@arktype/schema"
2
- import type { ErrorMessage } from "@arktype/util"
3
- import type { inferAstBase } from "./semantic.js"
4
- import type { astToString } from "./utils.js"
5
- import type { validateAst } from "./validate.js"
6
-
7
- export type validateDivisor<l, $, args> = isDivisible<
8
- inferAstBase<l, $, args>
9
- > extends true
10
- ? validateAst<l, $, args>
11
- : ErrorMessage<writeIndivisibleMessage<astToString<l>>>
12
-
13
- type isDivisible<data> = [data] extends [number] ? true : false
@@ -1,46 +0,0 @@
1
- import type { MorphAst, Out } from "@arktype/schema"
2
- import {
3
- Hkt,
4
- type List,
5
- type conform,
6
- type evaluate,
7
- type intersectArrays,
8
- type isAny
9
- } from "@arktype/util"
10
-
11
- export type inferIntersection<l, r> = [l] extends [never]
12
- ? never
13
- : [r] extends [never]
14
- ? never
15
- : [l & r] extends [never]
16
- ? never
17
- : isAny<l | r> extends true
18
- ? any
19
- : l extends MorphAst<infer lIn, infer lOut>
20
- ? r extends MorphAst
21
- ? never
22
- : (In: evaluate<lIn & r>) => Out<lOut>
23
- : r extends MorphAst<infer rIn, infer rOut>
24
- ? (In: evaluate<rIn & l>) => Out<rOut>
25
- : [l, r] extends [object, object]
26
- ? intersectObjects<l, r> extends infer result
27
- ? result
28
- : never
29
- : l & r
30
-
31
- declare class MorphableIntersection extends Hkt.Kind {
32
- f: (
33
- In: conform<this[Hkt.key], [l: unknown, r: unknown]>
34
- ) => inferIntersection<(typeof In)[0], (typeof In)[1]>
35
- }
36
-
37
- type intersectObjects<l, r> = [l, r] extends [
38
- infer lList extends List,
39
- infer rList extends List
40
- ]
41
- ? intersectArrays<lList, rList, MorphableIntersection>
42
- : evaluate<
43
- {
44
- [k in keyof l]: k extends keyof r ? inferIntersection<l[k], r[k]> : l[k]
45
- } & Omit<r, keyof l>
46
- >
@@ -1,144 +0,0 @@
1
- import type {
2
- DateLiteral,
3
- Refinements,
4
- RegexLiteral,
5
- distill,
6
- is
7
- } from "@arktype/schema"
8
- import type {
9
- BigintLiteral,
10
- List,
11
- NumberLiteral,
12
- evaluate,
13
- extend
14
- } from "@arktype/util"
15
- import type {
16
- UnparsedScope,
17
- resolve,
18
- tryInferSubmoduleReference
19
- } from "../../scope.js"
20
- import type { GenericProps } from "../../type.js"
21
- import type { inferDefinition } from "../definition.js"
22
- import type {
23
- Comparator,
24
- InvertedComparators,
25
- LimitLiteral
26
- } from "../string/reduce/shared.js"
27
- import type { StringLiteral } from "../string/shift/operand/enclosed.js"
28
- import type { inferIntersection } from "./intersections.js"
29
-
30
- export type inferAstRoot<ast, $, args> = inferAst<ast, $, args, {}>
31
-
32
- export type inferAstBase<ast, $, args> = distill<inferAstRoot<ast, $, args>>
33
-
34
- export type inferAst<
35
- ast,
36
- $,
37
- args,
38
- refinements extends Refinements
39
- > = ast extends List
40
- ? inferExpression<ast, $, args, refinements>
41
- : inferTerminal<ast, $, args, refinements>
42
-
43
- export type GenericInstantiationAst<
44
- g extends GenericProps = GenericProps,
45
- argAsts extends unknown[] = unknown[]
46
- > = [g, "<>", argAsts]
47
-
48
- export type inferExpression<
49
- ast extends List,
50
- $,
51
- args,
52
- refinements extends Refinements
53
- > = ast extends GenericInstantiationAst
54
- ? inferDefinition<
55
- ast[0]["definition"],
56
- ast[0]["$"] extends UnparsedScope
57
- ? // If the generic was defined in the current scope, its definition can be
58
- // resolved using the same scope as that of the input args.
59
- $
60
- : // Otherwise, use the scope that was explicitly associated with it.
61
- ast[0]["$"],
62
- {
63
- // Using keyof g["parameters"] & number here results in the element types
64
- // being mixed- another reason TS should not have separate `${number}` and number keys!
65
- [i in keyof ast[0]["parameters"] &
66
- `${number}` as ast[0]["parameters"][i]]: inferAst<
67
- ast[2][i & keyof ast[2]],
68
- $,
69
- args,
70
- refinements
71
- >
72
- }
73
- >
74
- : ast[1] extends "[]"
75
- ? inferAst<ast[0], $, args, refinements>[]
76
- : ast[1] extends "|"
77
- ?
78
- | inferAst<ast[0], $, args, refinements>
79
- | inferAst<ast[2], $, args, refinements>
80
- : ast[1] extends "&"
81
- ? inferIntersection<
82
- inferAst<ast[0], $, args, refinements>,
83
- inferAst<ast[2], $, args, refinements>
84
- >
85
- : ast[1] extends Comparator
86
- ? ast[0] extends LimitLiteral
87
- ? inferAst<
88
- ast[2],
89
- $,
90
- args,
91
- refinements & { [_ in InvertedComparators[ast[1]]]: ast[0] }
92
- >
93
- : inferAst<ast[0], $, args, refinements & { [_ in ast[1]]: ast[2] }>
94
- : ast[1] extends "%"
95
- ? inferAst<ast[0], $, args, refinements & { [k in `%${ast[2] & string}`]: 0 }>
96
- : ast[0] extends "keyof"
97
- ? keyof inferAst<ast[1], $, args, refinements>
98
- : never
99
-
100
- export type PrefixOperator = "keyof" | "instanceof" | "===" | "node"
101
-
102
- export type PrefixExpression<
103
- operator extends PrefixOperator = PrefixOperator,
104
- operand = unknown
105
- > = [operator, operand]
106
-
107
- export type PostfixOperator = "[]"
108
-
109
- export type PostfixExpression<
110
- operator extends PostfixOperator = PostfixOperator,
111
- operand = unknown
112
- > = readonly [operand, operator]
113
-
114
- export type InfixOperator = "|" | "&" | Comparator | "%" | ":" | "=>" | "@"
115
-
116
- export type InfixExpression<
117
- operator extends InfixOperator = InfixOperator,
118
- l = unknown,
119
- r = unknown
120
- > = [l, operator, r]
121
-
122
- export type inferTerminal<
123
- token,
124
- $,
125
- args,
126
- refinements extends Refinements
127
- > = token extends keyof args | keyof $
128
- ? {} extends refinements
129
- ? resolve<token, $, args>
130
- : is<resolve<token, $, args>, evaluate<refinements>>
131
- : token extends StringLiteral<infer text>
132
- ? text
133
- : token extends RegexLiteral
134
- ? is<string, extend<refinements, { [_ in token]: true }>>
135
- : token extends DateLiteral
136
- ? is<Date, extend<refinements, { [_ in token]: true }>>
137
- : token extends NumberLiteral<infer value>
138
- ? value
139
- : token extends BigintLiteral<infer value>
140
- ? value
141
- : // TODO: refinements
142
- // doing this last allows us to infer never if it isn't valid rather than check
143
- // if it's a valid submodule reference ahead of time
144
- tryInferSubmoduleReference<$, token>
@@ -1,26 +0,0 @@
1
- import type { List, Stringifiable } from "@arktype/util"
2
- import type { Comparator } from "../string/reduce/shared.js"
3
- import type { InfixExpression, PostfixExpression } from "./semantic.js"
4
-
5
- export type astToString<ast> = `'${astToStringRecurse<ast>}'`
6
-
7
- type astToStringRecurse<ast> = ast extends PostfixExpression<
8
- infer operator,
9
- infer operand
10
- >
11
- ? operator extends "[]"
12
- ? `${groupAst<operand>}[]`
13
- : never
14
- : ast extends InfixExpression<infer operator, infer l, infer r>
15
- ? operator extends "&" | "|" | "%" | Comparator
16
- ? `${groupAst<l>}${operator}${groupAst<r>}`
17
- : never
18
- : ast extends Stringifiable
19
- ? `${ast extends bigint ? `${ast}n` : ast}`
20
- : "..."
21
-
22
- type groupAst<ast> = ast extends List
23
- ? ast[1] extends "[]"
24
- ? astToStringRecurse<ast>
25
- : `(${astToStringRecurse<ast>})`
26
- : astToStringRecurse<ast>
@@ -1,109 +0,0 @@
1
- import type { Refinements } from "@arktype/schema"
2
- import type {
3
- BigintLiteral,
4
- Completion,
5
- ErrorMessage,
6
- NumberLiteral,
7
- writeMalformedNumericLiteralMessage
8
- } from "@arktype/util"
9
- import type { Module } from "../../scope.js"
10
- import type { GenericProps } from "../../type.js"
11
- import type { Comparator } from "../string/reduce/shared.js"
12
- import type { writeInvalidGenericArgsMessage } from "../string/shift/operand/genericArgs.js"
13
- import type { writeMissingSubmoduleAccessMessage } from "../string/shift/operand/unenclosed.js"
14
- import type { parseString } from "../string/string.js"
15
- import type { validateRange } from "./bounds.js"
16
- import type { validateDivisor } from "./divisor.js"
17
- import type {
18
- GenericInstantiationAst,
19
- InfixExpression,
20
- PostfixExpression
21
- } from "./semantic.js"
22
- import type { astToString } from "./utils.js"
23
-
24
- export type validateAst<ast, $, args> = ast extends string
25
- ? validateStringAst<ast, $>
26
- : ast extends PostfixExpression<infer operator, infer operand>
27
- ? operator extends "[]"
28
- ? validateAst<operand, $, args>
29
- : never
30
- : ast extends InfixExpression<infer operator, infer l, infer r>
31
- ? operator extends "&" | "|"
32
- ? validateInfix<ast, $, args>
33
- : operator extends Comparator
34
- ? validateRange<l, operator, r, $, args>
35
- : operator extends "%"
36
- ? validateDivisor<l, $, args>
37
- : undefined
38
- : ast extends readonly ["keyof", infer operand]
39
- ? validateAst<operand, $, args>
40
- : ast extends GenericInstantiationAst
41
- ? validateGenericArgs<ast["2"], $, args>
42
- : ErrorMessage<writeUnexpectedExpressionMessage<astToString<ast>>>
43
-
44
- type writeUnexpectedExpressionMessage<expression extends string> =
45
- `Unexpectedly failed to parse the expression resulting from ${expression}`
46
-
47
- type validateGenericArgs<argAsts extends unknown[], $, args> = argAsts extends [
48
- infer head,
49
- ...infer tail
50
- ]
51
- ? validateAst<head, $, args> extends ErrorMessage<infer message>
52
- ? ErrorMessage<message>
53
- : validateGenericArgs<tail, $, args>
54
- : undefined
55
-
56
- export const writeUnsatisfiableExpressionError = <expression extends string>(
57
- expression: expression
58
- ): writeUnsatisfiableExpressionError<expression> =>
59
- `${expression} results in an unsatisfiable type`
60
-
61
- export type writeUnsatisfiableExpressionError<expression extends string> =
62
- `${expression} results in an unsatisfiable type`
63
-
64
- type validateStringAst<def extends string, $> = def extends NumberLiteral<
65
- infer value
66
- >
67
- ? number extends value
68
- ? ErrorMessage<writeMalformedNumericLiteralMessage<def, "number">>
69
- : undefined
70
- : def extends BigintLiteral<infer value>
71
- ? bigint extends value
72
- ? ErrorMessage<writeMalformedNumericLiteralMessage<def, "bigint">>
73
- : undefined
74
- : def extends keyof $
75
- ? $[def] extends null
76
- ? // handle any/never
77
- def
78
- : // these problems would've been caught during a fullStringParse, but it's most
79
- // efficient to check for them here in case the string was naively parsed
80
- $[def] extends GenericProps
81
- ? ErrorMessage<
82
- writeInvalidGenericArgsMessage<def, $[def]["parameters"], []>
83
- >
84
- : $[def] extends Module
85
- ? ErrorMessage<writeMissingSubmoduleAccessMessage<def>>
86
- : undefined
87
- : def extends ErrorMessage
88
- ? def
89
- : undefined
90
-
91
- export type validateString<def extends string, $, args> = validateAst<
92
- parseString<def, $, args>,
93
- $,
94
- args
95
- > extends infer result extends ErrorMessage
96
- ? result extends Completion<infer text>
97
- ? text
98
- : result
99
- : def
100
-
101
- type validateInfix<ast extends InfixExpression, $, args> = validateAst<
102
- ast[0],
103
- $,
104
- args
105
- > extends ErrorMessage<infer message>
106
- ? ErrorMessage<message>
107
- : validateAst<ast[2], $, args> extends ErrorMessage<infer message>
108
- ? ErrorMessage<message>
109
- : undefined