arktype 1.1.0-dev-2 → 2.0.0-beta.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 (225) hide show
  1. package/LICENSE +7 -0
  2. package/out/ark.d.ts +19 -0
  3. package/out/ark.js +9 -0
  4. package/out/config.d.ts +1 -0
  5. package/out/config.js +2 -0
  6. package/out/generic.d.ts +65 -0
  7. package/out/generic.js +39 -0
  8. package/out/index.d.ts +6 -0
  9. package/out/index.js +5 -0
  10. package/out/match.d.ts +71 -0
  11. package/out/match.js +50 -0
  12. package/out/module.d.ts +14 -0
  13. package/out/module.js +2 -0
  14. package/{dist → out}/parser/definition.d.ts +16 -16
  15. package/{dist → out}/parser/definition.js +12 -12
  16. package/out/parser/objectLiteral.d.ts +79 -0
  17. package/out/parser/objectLiteral.js +100 -0
  18. package/out/parser/semantic/bounds.d.ts +15 -0
  19. package/{dist → out}/parser/semantic/bounds.js +0 -1
  20. package/out/parser/semantic/default.d.ts +8 -0
  21. package/out/parser/semantic/default.js +1 -0
  22. package/out/parser/semantic/divisor.d.ts +7 -0
  23. package/out/parser/semantic/divisor.js +1 -0
  24. package/out/parser/semantic/infer.d.ts +27 -0
  25. package/out/parser/semantic/infer.js +1 -0
  26. package/out/parser/semantic/utils.d.ts +4 -0
  27. package/out/parser/semantic/utils.js +1 -0
  28. package/out/parser/semantic/validate.d.ts +30 -0
  29. package/out/parser/semantic/validate.js +2 -0
  30. package/out/parser/string/reduce/dynamic.d.ts +43 -0
  31. package/{dist → out}/parser/string/reduce/dynamic.js +50 -33
  32. package/out/parser/string/reduce/shared.d.ts +42 -0
  33. package/{dist → out}/parser/string/reduce/shared.js +22 -2
  34. package/{dist → out}/parser/string/reduce/static.d.ts +29 -27
  35. package/out/parser/string/reduce/static.js +1 -0
  36. package/out/parser/string/shift/operand/date.d.ts +9 -0
  37. package/{dist → out}/parser/string/shift/operand/date.js +5 -8
  38. package/{dist → out}/parser/string/shift/operand/enclosed.d.ts +5 -16
  39. package/{dist → out}/parser/string/shift/operand/enclosed.js +15 -15
  40. package/out/parser/string/shift/operand/genericArgs.d.ts +28 -0
  41. package/out/parser/string/shift/operand/genericArgs.js +16 -0
  42. package/out/parser/string/shift/operand/operand.d.ts +9 -0
  43. package/out/parser/string/shift/operand/operand.js +12 -0
  44. package/out/parser/string/shift/operand/unenclosed.d.ts +30 -0
  45. package/out/parser/string/shift/operand/unenclosed.js +53 -0
  46. package/out/parser/string/shift/operator/bounds.d.ts +27 -0
  47. package/out/parser/string/shift/operator/bounds.js +94 -0
  48. package/out/parser/string/shift/operator/default.d.ts +24 -0
  49. package/out/parser/string/shift/operator/default.js +23 -0
  50. package/{dist → out}/parser/string/shift/operator/divisor.d.ts +2 -4
  51. package/{dist → out}/parser/string/shift/operator/divisor.js +2 -4
  52. package/{dist → out}/parser/string/shift/operator/operator.d.ts +4 -4
  53. package/out/parser/string/shift/operator/operator.js +22 -0
  54. package/{dist → out}/parser/string/shift/scanner.d.ts +13 -17
  55. package/{dist → out}/parser/string/shift/scanner.js +27 -24
  56. package/out/parser/string/string.d.ts +28 -0
  57. package/out/parser/string/string.js +24 -0
  58. package/out/parser/tuple.d.ts +130 -0
  59. package/out/parser/tuple.js +157 -0
  60. package/out/scope.d.ts +86 -0
  61. package/out/scope.js +132 -0
  62. package/out/type.d.ts +83 -0
  63. package/out/type.js +36 -0
  64. package/package.json +37 -41
  65. package/dist/main.d.ts +0 -11
  66. package/dist/main.d.ts.map +0 -1
  67. package/dist/main.js +0 -8
  68. package/dist/main.js.map +0 -1
  69. package/dist/match.d.ts +0 -46
  70. package/dist/match.d.ts.map +0 -1
  71. package/dist/match.js +0 -23
  72. package/dist/match.js.map +0 -1
  73. package/dist/parser/definition.d.ts.map +0 -1
  74. package/dist/parser/definition.js.map +0 -1
  75. package/dist/parser/generic.d.ts +0 -44
  76. package/dist/parser/generic.d.ts.map +0 -1
  77. package/dist/parser/generic.js +0 -46
  78. package/dist/parser/generic.js.map +0 -1
  79. package/dist/parser/objectLiteral.d.ts +0 -24
  80. package/dist/parser/objectLiteral.d.ts.map +0 -1
  81. package/dist/parser/objectLiteral.js +0 -38
  82. package/dist/parser/objectLiteral.js.map +0 -1
  83. package/dist/parser/semantic/bounds.d.ts +0 -13
  84. package/dist/parser/semantic/bounds.d.ts.map +0 -1
  85. package/dist/parser/semantic/bounds.js.map +0 -1
  86. package/dist/parser/semantic/divisor.d.ts +0 -9
  87. package/dist/parser/semantic/divisor.d.ts.map +0 -1
  88. package/dist/parser/semantic/divisor.js +0 -2
  89. package/dist/parser/semantic/divisor.js.map +0 -1
  90. package/dist/parser/semantic/intersections.d.ts +0 -15
  91. package/dist/parser/semantic/intersections.d.ts.map +0 -1
  92. package/dist/parser/semantic/intersections.js +0 -2
  93. package/dist/parser/semantic/intersections.js.map +0 -1
  94. package/dist/parser/semantic/semantic.d.ts +0 -22
  95. package/dist/parser/semantic/semantic.d.ts.map +0 -1
  96. package/dist/parser/semantic/semantic.js +0 -2
  97. package/dist/parser/semantic/semantic.js.map +0 -1
  98. package/dist/parser/semantic/utils.d.ts +0 -8
  99. package/dist/parser/semantic/utils.d.ts.map +0 -1
  100. package/dist/parser/semantic/utils.js +0 -2
  101. package/dist/parser/semantic/utils.js.map +0 -1
  102. package/dist/parser/semantic/validate.d.ts +0 -24
  103. package/dist/parser/semantic/validate.d.ts.map +0 -1
  104. package/dist/parser/semantic/validate.js +0 -2
  105. package/dist/parser/semantic/validate.js.map +0 -1
  106. package/dist/parser/shared.d.ts +0 -48
  107. package/dist/parser/shared.d.ts.map +0 -1
  108. package/dist/parser/shared.js +0 -41
  109. package/dist/parser/shared.js.map +0 -1
  110. package/dist/parser/string/reduce/dynamic.d.ts +0 -40
  111. package/dist/parser/string/reduce/dynamic.d.ts.map +0 -1
  112. package/dist/parser/string/reduce/dynamic.js.map +0 -1
  113. package/dist/parser/string/reduce/shared.d.ts +0 -29
  114. package/dist/parser/string/reduce/shared.d.ts.map +0 -1
  115. package/dist/parser/string/reduce/shared.js.map +0 -1
  116. package/dist/parser/string/reduce/static.d.ts.map +0 -1
  117. package/dist/parser/string/reduce/static.js +0 -2
  118. package/dist/parser/string/reduce/static.js.map +0 -1
  119. package/dist/parser/string/shift/operand/date.d.ts +0 -11
  120. package/dist/parser/string/shift/operand/date.d.ts.map +0 -1
  121. package/dist/parser/string/shift/operand/date.js.map +0 -1
  122. package/dist/parser/string/shift/operand/enclosed.d.ts.map +0 -1
  123. package/dist/parser/string/shift/operand/enclosed.js.map +0 -1
  124. package/dist/parser/string/shift/operand/operand.d.ts +0 -9
  125. package/dist/parser/string/shift/operand/operand.d.ts.map +0 -1
  126. package/dist/parser/string/shift/operand/operand.js +0 -17
  127. package/dist/parser/string/shift/operand/operand.js.map +0 -1
  128. package/dist/parser/string/shift/operand/unenclosed.d.ts +0 -36
  129. package/dist/parser/string/shift/operand/unenclosed.d.ts.map +0 -1
  130. package/dist/parser/string/shift/operand/unenclosed.js +0 -68
  131. package/dist/parser/string/shift/operand/unenclosed.js.map +0 -1
  132. package/dist/parser/string/shift/operator/bounds.d.ts +0 -55
  133. package/dist/parser/string/shift/operator/bounds.d.ts.map +0 -1
  134. package/dist/parser/string/shift/operator/bounds.js +0 -95
  135. package/dist/parser/string/shift/operator/bounds.js.map +0 -1
  136. package/dist/parser/string/shift/operator/divisor.d.ts.map +0 -1
  137. package/dist/parser/string/shift/operator/divisor.js.map +0 -1
  138. package/dist/parser/string/shift/operator/operator.d.ts.map +0 -1
  139. package/dist/parser/string/shift/operator/operator.js +0 -29
  140. package/dist/parser/string/shift/operator/operator.js.map +0 -1
  141. package/dist/parser/string/shift/scanner.d.ts.map +0 -1
  142. package/dist/parser/string/shift/scanner.js.map +0 -1
  143. package/dist/parser/string/string.d.ts +0 -26
  144. package/dist/parser/string/string.d.ts.map +0 -1
  145. package/dist/parser/string/string.js +0 -31
  146. package/dist/parser/string/string.js.map +0 -1
  147. package/dist/parser/tuple.d.ts +0 -73
  148. package/dist/parser/tuple.d.ts.map +0 -1
  149. package/dist/parser/tuple.js +0 -134
  150. package/dist/parser/tuple.js.map +0 -1
  151. package/dist/scope.d.ts +0 -159
  152. package/dist/scope.d.ts.map +0 -1
  153. package/dist/scope.js +0 -248
  154. package/dist/scope.js.map +0 -1
  155. package/dist/scopes/ark.d.ts +0 -38
  156. package/dist/scopes/ark.d.ts.map +0 -1
  157. package/dist/scopes/ark.js +0 -24
  158. package/dist/scopes/ark.js.map +0 -1
  159. package/dist/scopes/jsObjects.d.ts +0 -19
  160. package/dist/scopes/jsObjects.d.ts.map +0 -1
  161. package/dist/scopes/jsObjects.js +0 -15
  162. package/dist/scopes/jsObjects.js.map +0 -1
  163. package/dist/scopes/parsing.d.ts +0 -18
  164. package/dist/scopes/parsing.d.ts.map +0 -1
  165. package/dist/scopes/parsing.js +0 -61
  166. package/dist/scopes/parsing.js.map +0 -1
  167. package/dist/scopes/tsGenerics.d.ts +0 -17
  168. package/dist/scopes/tsGenerics.d.ts.map +0 -1
  169. package/dist/scopes/tsGenerics.js +0 -13
  170. package/dist/scopes/tsGenerics.js.map +0 -1
  171. package/dist/scopes/tsKeywords.d.ts +0 -24
  172. package/dist/scopes/tsKeywords.d.ts.map +0 -1
  173. package/dist/scopes/tsKeywords.js +0 -20
  174. package/dist/scopes/tsKeywords.js.map +0 -1
  175. package/dist/scopes/utils/creditCard.d.ts +0 -3
  176. package/dist/scopes/utils/creditCard.d.ts.map +0 -1
  177. package/dist/scopes/utils/creditCard.js +0 -36
  178. package/dist/scopes/utils/creditCard.js.map +0 -1
  179. package/dist/scopes/utils/date.d.ts +0 -24
  180. package/dist/scopes/utils/date.d.ts.map +0 -1
  181. package/dist/scopes/utils/date.js +0 -53
  182. package/dist/scopes/utils/date.js.map +0 -1
  183. package/dist/scopes/validation.d.ts +0 -20
  184. package/dist/scopes/validation.d.ts.map +0 -1
  185. package/dist/scopes/validation.js +0 -57
  186. package/dist/scopes/validation.js.map +0 -1
  187. package/dist/type.d.ts +0 -101
  188. package/dist/type.d.ts.map +0 -1
  189. package/dist/type.js +0 -138
  190. package/dist/type.js.map +0 -1
  191. package/main.ts +0 -20
  192. package/match.ts +0 -137
  193. package/parser/definition.ts +0 -156
  194. package/parser/generic.ts +0 -208
  195. package/parser/objectLiteral.ts +0 -120
  196. package/parser/semantic/bounds.ts +0 -63
  197. package/parser/semantic/divisor.ts +0 -13
  198. package/parser/semantic/intersections.ts +0 -47
  199. package/parser/semantic/semantic.ts +0 -107
  200. package/parser/semantic/utils.ts +0 -26
  201. package/parser/semantic/validate.ts +0 -105
  202. package/parser/shared.ts +0 -141
  203. package/parser/string/reduce/dynamic.ts +0 -181
  204. package/parser/string/reduce/shared.ts +0 -73
  205. package/parser/string/reduce/static.ts +0 -300
  206. package/parser/string/shift/operand/date.ts +0 -57
  207. package/parser/string/shift/operand/enclosed.ts +0 -119
  208. package/parser/string/shift/operand/operand.ts +0 -48
  209. package/parser/string/shift/operand/unenclosed.ts +0 -282
  210. package/parser/string/shift/operator/bounds.ts +0 -229
  211. package/parser/string/shift/operator/divisor.ts +0 -36
  212. package/parser/string/shift/operator/operator.ts +0 -82
  213. package/parser/string/shift/scanner.ts +0 -209
  214. package/parser/string/string.ts +0 -93
  215. package/parser/tuple.ts +0 -475
  216. package/scope.ts +0 -573
  217. package/scopes/ark.ts +0 -64
  218. package/scopes/jsObjects.ts +0 -29
  219. package/scopes/parsing.ts +0 -81
  220. package/scopes/tsGenerics.ts +0 -28
  221. package/scopes/tsKeywords.ts +0 -39
  222. package/scopes/utils/creditCard.ts +0 -37
  223. package/scopes/utils/date.ts +0 -108
  224. package/scopes/validation.ts +0 -81
  225. package/type.ts +0 -403
package/type.ts DELETED
@@ -1,403 +0,0 @@
1
- import {
2
- In,
3
- TraversalState,
4
- arkKind,
5
- builtins,
6
- inferred,
7
- registry,
8
- type BaseAttributes,
9
- type CheckResult,
10
- type Morph,
11
- type Out,
12
- type Predicate,
13
- type Root,
14
- type UnknownNode,
15
- type inferMorphOut,
16
- type inferNarrow
17
- } from "@arktype/schema"
18
- import {
19
- CompiledFunction,
20
- transform,
21
- type BuiltinObjectKind,
22
- type BuiltinObjects,
23
- type Constructor,
24
- type Json,
25
- type Primitive,
26
- type conform
27
- } from "@arktype/util"
28
- import type {
29
- inferDefinition,
30
- validateDeclared,
31
- validateDefinition
32
- } from "./parser/definition.js"
33
- import {
34
- parseGenericParams,
35
- type GenericParamsParseError
36
- } from "./parser/generic.js"
37
- import type { inferIntersection } from "./parser/semantic/intersections.js"
38
- import type {
39
- IndexOneOperator,
40
- IndexZeroOperator,
41
- MorphAst,
42
- TupleInfixOperator
43
- } from "./parser/tuple.js"
44
- import { bindThis, type Module, type Scope } from "./scope.js"
45
-
46
- export type TypeParser<$> = {
47
- // Parse and check the definition, returning either the original input for a
48
- // valid definition or a string representing an error message.
49
- <const def>(def: validateTypeRoot<def, $>): Type<inferTypeRoot<def, $>, $>
50
-
51
- // Spread version of a tuple expression
52
- <const zero, const one, const rest extends readonly unknown[]>(
53
- _0: zero extends IndexZeroOperator ? zero : validateTypeRoot<zero, $>,
54
- _1: zero extends "keyof"
55
- ? validateTypeRoot<one, $>
56
- : zero extends "instanceof"
57
- ? conform<one, Constructor>
58
- : zero extends "==="
59
- ? conform<one, unknown>
60
- : conform<one, IndexOneOperator>,
61
- ..._2: zero extends "==="
62
- ? rest
63
- : zero extends "instanceof"
64
- ? conform<rest, readonly Constructor[]>
65
- : one extends TupleInfixOperator
66
- ? one extends ":"
67
- ? [Predicate<extractIn<inferTypeRoot<zero, $>>>]
68
- : one extends "=>"
69
- ? // TODO: centralize
70
- [Morph<extractOut<inferTypeRoot<zero, $>>, unknown>]
71
- : one extends "@"
72
- ? [string | BaseAttributes]
73
- : [validateTypeRoot<rest[0], $>]
74
- : []
75
- ): Type<inferTypeRoot<[zero, one, ...rest], $>, $>
76
-
77
- <params extends string, const def>(
78
- params: `<${validateParameterString<params>}>`,
79
- def: validateDefinition<
80
- def,
81
- $,
82
- {
83
- [param in parseGenericParams<params>[number]]: unknown
84
- }
85
- >
86
- ): Generic<parseGenericParams<params>, def, $>
87
- }
88
-
89
- export type DeclarationParser<$> = <preinferred>() => {
90
- // for some reason, making this a const parameter breaks preinferred validation
91
- type: <def>(
92
- def: validateDeclared<preinferred, def, $, bindThis<def>>
93
- ) => Type<preinferred, $>
94
- }
95
-
96
- export const createTypeParser = <$>(scope: Scope): TypeParser<$> => {
97
- const parser = (...args: unknown[]): Type | Generic => {
98
- if (args.length === 1) {
99
- // treat as a simple definition
100
- return new Type(args[0], scope)
101
- }
102
- if (
103
- args.length === 2 &&
104
- typeof args[0] === "string" &&
105
- args[0][0] === "<" &&
106
- args[0].at(-1) === ">"
107
- ) {
108
- // if there are exactly two args, the first of which looks like <${string}>,
109
- // treat as a generic
110
- const params = parseGenericParams(args[0].slice(1, -1))
111
- const def = args[1]
112
- return validateUninstantiatedGeneric(generic(params, def, scope) as never)
113
- }
114
- // otherwise, treat as a tuple expression. technically, this also allows
115
- // non-expression tuple definitions to be parsed, but it's not a supported
116
- // part of the API as specified by the associated types
117
- return new Type(args, scope)
118
- }
119
- return parser as never
120
- }
121
-
122
- export type ArkKinds = {
123
- node: UnknownNode
124
- generic: Generic
125
- module: Module
126
- }
127
-
128
- export const addArkKind = <kind extends ArkKind>(
129
- value: Omit<ArkKinds[kind], arkKind> & { [arkKind]?: kind },
130
- kind: kind
131
- ): ArkKinds[kind] =>
132
- Object.defineProperty(value, arkKind, {
133
- value: kind,
134
- enumerable: false
135
- }) as never
136
-
137
- export type arkKind = typeof arkKind
138
-
139
- export type ArkKind = keyof ArkKinds
140
-
141
- export const hasArkKind = <kind extends ArkKind>(
142
- value: unknown,
143
- kind: kind
144
- ): value is ArkKinds[kind] => (value as any)?.[arkKind] === kind
145
-
146
- export type DefinitionParser<$> = <def>(
147
- def: validateDefinition<def, $, bindThis<def>>
148
- ) => def
149
-
150
- export type KeyCheckKind = "distilled" | "strict" | "loose"
151
-
152
- export type TypeConfig = {
153
- keys?: KeyCheckKind
154
- mustBe?: string
155
- }
156
-
157
- registry().register(TraversalState, "state")
158
-
159
- export class Type<t = unknown, $ = any> extends CompiledFunction<
160
- (data: unknown) => CheckResult<extractOut<t>>
161
- > {
162
- declare [inferred]: t
163
- declare inferMorph: t
164
- declare infer: extractOut<t>
165
- declare inferIn: extractIn<t>
166
-
167
- config: TypeConfig
168
- root: Root<t>
169
- condition = ""
170
- alias: string
171
- allows: this["root"]["allows"]
172
- json: Json
173
-
174
- constructor(
175
- public definition: unknown,
176
- public scope: Scope
177
- ) {
178
- const root = parseTypeRoot(definition, scope) as Root<t>
179
- super(In, `return true ? { data: ${In} } : { problems: [] } `)
180
- // const state = new ${registry().reference("state")}();
181
- // const morphs = [];
182
- // for(let i = 0; i < morphs.length; i++) {
183
- // morphs[i]()
184
- // }
185
- // return state.finalize(${In});
186
- this.root = root
187
- this.allows = root.allows
188
- this.config = scope.config
189
- this.json = this.root.json
190
- this.alias = this.root.alias
191
- }
192
-
193
- configure(config: TypeConfig) {
194
- this.config = { ...this.config, ...config }
195
- return this
196
- }
197
-
198
- // TODO: should return out
199
- from(literal: this["infer"]) {
200
- return literal
201
- }
202
-
203
- fromIn(literal: this["inferIn"]) {
204
- return literal
205
- }
206
-
207
- // TODO: Morph intersections, ordering
208
- and<def>(
209
- def: validateTypeRoot<def, $>
210
- ): Type<inferIntersection<t, inferTypeRoot<def, $>>, $> {
211
- return new Type(
212
- this.root.and(parseTypeRoot(def, this.scope)),
213
- this.scope
214
- ) as never
215
- }
216
-
217
- or<def>(def: validateTypeRoot<def, $>): Type<t | inferTypeRoot<def, $>, $> {
218
- return new Type(
219
- this.root.or(parseTypeRoot(def, this.scope)),
220
- this.scope
221
- ) as never
222
- }
223
-
224
- morph<morph extends Morph<extractOut<t>>>(
225
- morph: morph
226
- ): Type<(In: this["inferIn"]) => Out<inferMorphOut<ReturnType<morph>>>, $>
227
- morph<morph extends Morph<extractOut<t>>, def>(
228
- morph: morph,
229
- outValidator: validateTypeRoot<def, $>
230
- ): Type<
231
- (In: this["inferIn"]) => Out<
232
- // TODO: validate overlapping
233
- // inferMorphOut<ReturnType<morph>> &
234
- extractOut<inferTypeRoot<def, $>>
235
- >,
236
- $
237
- >
238
- morph(morph: Morph, outValidator?: unknown) {
239
- // TODO: tuple expression for out validator
240
- outValidator
241
- return this as never
242
- // return new Type(
243
- // this.root.constrain("morph", morph),
244
- // this.scope
245
- // ) as never
246
- }
247
-
248
- // TODO: based on below, should maybe narrow morph output if used after
249
- narrow<def extends Predicate<extractOut<t>>>(
250
- def: def
251
- ): Type<
252
- includesMorphs<t> extends true
253
- ? (In: this["inferIn"]) => Out<inferNarrow<this["infer"], def>>
254
- : inferNarrow<this["infer"], def>,
255
- $
256
- > {
257
- return new Type(this.root.constrain("predicate", def), this.scope) as never
258
- }
259
-
260
- array(): Type<t[], $> {
261
- return new Type(this.root.array(), this.scope) as never
262
- }
263
-
264
- keyof(): Type<keyof this["inferIn"], $> {
265
- return new Type(this.root.keyof(), this.scope) as never
266
- }
267
-
268
- assert(data: unknown): extractOut<t> {
269
- const result = this.call(null, data)
270
- return result.problems ? result.problems.throw() : result.data
271
- }
272
-
273
- // TODO: parse these
274
- equals<other>(other: Type<other>): this is Type<other, $> {
275
- return this.root === (other.root as unknown)
276
- }
277
-
278
- extends<other>(other: Type<other>): this is Type<other, $> {
279
- return this.root.extends(other.root)
280
- }
281
- }
282
-
283
- const parseTypeRoot = (def: unknown, scope: Scope, args?: BoundArgs) =>
284
- scope.parseDefinition(def, { args: args ?? bindThis(), baseName: "type" })
285
-
286
- export type validateTypeRoot<def, $> = validateDefinition<def, $, bindThis<def>>
287
-
288
- export type inferTypeRoot<def, $> = inferDefinition<def, $, bindThis<def>>
289
-
290
- type validateParameterString<params extends string> =
291
- parseGenericParams<params> extends GenericParamsParseError<infer message>
292
- ? message
293
- : params
294
-
295
- export const validateUninstantiatedGeneric = (g: Generic) => {
296
- // the unconstrained instantiation of the generic is not used for now
297
- // other than to eagerly validate that the def does not contain any errors
298
- g.scope.parseDefinition(
299
- g.definition,
300
- // once we support constraints on generic parameters, we'd use
301
- // the base type here: https://github.com/arktypeio/arktype/issues/796
302
- {
303
- baseName: "generic",
304
- args: transform(g.parameters, ([, name]) => [name, builtins.unknown])
305
- }
306
- )
307
- return g
308
- }
309
-
310
- export const generic = (
311
- parameters: string[],
312
- definition: unknown,
313
- scope: Scope
314
- ) => {
315
- return Object.assign(
316
- (...args: unknown[]) => {
317
- const argNodes = transform(parameters, ([i, param]) => [
318
- param,
319
- parseTypeRoot(args[i], scope)
320
- ])
321
- const root = parseTypeRoot(definition, scope, argNodes)
322
- return new Type(root, scope)
323
- },
324
- {
325
- [arkKind]: "generic",
326
- parameters,
327
- definition,
328
- scope
329
- // $ is only needed at compile-time
330
- } satisfies Omit<GenericProps, "$">
331
- ) as unknown as Generic
332
- }
333
-
334
- // Comparing to Generic directly doesn't work well, so we compare to only its props
335
- export type GenericProps<
336
- params extends string[] = string[],
337
- def = unknown,
338
- $ = any
339
- > = {
340
- [arkKind]: "generic"
341
- $: $
342
- parameters: params
343
- definition: def
344
- scope: Scope
345
- }
346
-
347
- export type BoundArgs = Record<string, Root>
348
-
349
- // TODO: Fix external reference (i.e. if this is attached to a scope, then args are defined using it)
350
- export type Generic<
351
- params extends string[] = string[],
352
- def = unknown,
353
- $ = any
354
- > = {
355
- <args>(
356
- ...args: conform<
357
- args,
358
- {
359
- [i in keyof params]: validateTypeRoot<args[i & keyof args], $>
360
- }
361
- >
362
- ): Type<inferDefinition<def, $, bindGenericInstantiation<params, $, args>>, $>
363
- } & GenericProps<params, def, $>
364
-
365
- type bindGenericInstantiation<params extends string[], $, args> = {
366
- [i in keyof params & `${number}` as params[i]]: inferTypeRoot<
367
- args[i & keyof args],
368
- $
369
- >
370
- }
371
-
372
- export type extractIn<t> = includesMorphs<t> extends true
373
- ? extractMorphs<t, "in">
374
- : t
375
-
376
- export type extractOut<t> = includesMorphs<t> extends true
377
- ? extractMorphs<t, "out">
378
- : t
379
-
380
- type includesMorphs<t> = [
381
- t,
382
- extractMorphs<t, "in">,
383
- t,
384
- extractMorphs<t, "out">
385
- ] extends [extractMorphs<t, "in">, t, extractMorphs<t, "out">, t]
386
- ? false
387
- : true
388
-
389
- type extractMorphs<t, io extends "in" | "out"> = t extends MorphAst<
390
- infer i,
391
- infer o
392
- >
393
- ? io extends "in"
394
- ? i
395
- : o
396
- : t extends TerminallyInferredObjectKind | Primitive
397
- ? t
398
- : { [k in keyof t]: extractMorphs<t[k], io> }
399
-
400
- /** Objects we don't want to expand during inference like Date or Promise */
401
- type TerminallyInferredObjectKind =
402
- | ReturnType<ArkConfig["preserve"]>
403
- | BuiltinObjects[Exclude<BuiltinObjectKind, "Object" | "Array">]