arktype 2.0.0-dev.7 → 2.0.0-dev.8

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 (93) hide show
  1. package/__tests__/array.test.ts +23 -25
  2. package/__tests__/config.test.ts +8 -8
  3. package/__tests__/cyclic.test.ts +3 -3
  4. package/__tests__/examples.test.ts +3 -3
  5. package/__tests__/instanceof.test.ts +7 -7
  6. package/__tests__/intersection.test.ts +2 -2
  7. package/__tests__/keyTraversal.test.ts +15 -15
  8. package/__tests__/keywords.test.ts +50 -37
  9. package/__tests__/literal.test.ts +2 -2
  10. package/__tests__/match.bench.ts +6 -6
  11. package/__tests__/narrow.test.ts +17 -23
  12. package/__tests__/objectLiteral.test.ts +4 -4
  13. package/__tests__/operator.bench.ts +4 -4
  14. package/__tests__/{morph.test.ts → pipe.test.ts} +136 -75
  15. package/__tests__/scope.test.ts +5 -15
  16. package/__tests__/traverse.test.ts +36 -35
  17. package/__tests__/type.test.ts +8 -10
  18. package/ark.ts +14 -11
  19. package/generic.ts +8 -6
  20. package/match.ts +59 -70
  21. package/out/ark.d.ts +4 -3
  22. package/out/ark.d.ts.map +1 -1
  23. package/out/ark.js +4 -4
  24. package/out/ark.js.map +1 -1
  25. package/out/generic.d.ts.map +1 -1
  26. package/out/generic.js +8 -6
  27. package/out/generic.js.map +1 -1
  28. package/out/match.d.ts.map +1 -1
  29. package/out/match.js +50 -56
  30. package/out/match.js.map +1 -1
  31. package/out/parser/generic.d.ts +3 -3
  32. package/out/parser/generic.d.ts.map +1 -1
  33. package/out/parser/generic.js +4 -5
  34. package/out/parser/generic.js.map +1 -1
  35. package/out/parser/objectLiteral.d.ts.map +1 -1
  36. package/out/parser/objectLiteral.js +2 -3
  37. package/out/parser/objectLiteral.js.map +1 -1
  38. package/out/parser/semantic/validate.d.ts +2 -2
  39. package/out/parser/semantic/validate.d.ts.map +1 -1
  40. package/out/parser/semantic/validate.js.map +1 -1
  41. package/out/parser/string/reduce/dynamic.d.ts.map +1 -1
  42. package/out/parser/string/reduce/dynamic.js +4 -8
  43. package/out/parser/string/reduce/dynamic.js.map +1 -1
  44. package/out/parser/string/shift/operand/date.js +2 -4
  45. package/out/parser/string/shift/operand/date.js.map +1 -1
  46. package/out/parser/string/shift/operand/enclosed.js +6 -8
  47. package/out/parser/string/shift/operand/enclosed.js.map +1 -1
  48. package/out/parser/string/shift/operand/genericArgs.d.ts +4 -4
  49. package/out/parser/string/shift/operand/genericArgs.js +4 -5
  50. package/out/parser/string/shift/operand/genericArgs.js.map +1 -1
  51. package/out/parser/string/shift/operand/unenclosed.d.ts +4 -4
  52. package/out/parser/string/shift/operand/unenclosed.d.ts.map +1 -1
  53. package/out/parser/string/shift/operand/unenclosed.js +5 -10
  54. package/out/parser/string/shift/operand/unenclosed.js.map +1 -1
  55. package/out/parser/string/shift/operator/bounds.d.ts.map +1 -1
  56. package/out/parser/string/shift/operator/bounds.js +4 -8
  57. package/out/parser/string/shift/operator/bounds.js.map +1 -1
  58. package/out/parser/string/shift/operator/divisor.d.ts.map +1 -1
  59. package/out/parser/string/shift/operator/divisor.js +1 -2
  60. package/out/parser/string/shift/operator/divisor.js.map +1 -1
  61. package/out/parser/string/shift/scanner.d.ts.map +1 -1
  62. package/out/parser/string/shift/scanner.js +2 -4
  63. package/out/parser/string/shift/scanner.js.map +1 -1
  64. package/out/parser/string/string.d.ts.map +1 -1
  65. package/out/parser/string/string.js +1 -2
  66. package/out/parser/string/string.js.map +1 -1
  67. package/out/parser/tuple.d.ts.map +1 -1
  68. package/out/parser/tuple.js +12 -15
  69. package/out/parser/tuple.js.map +1 -1
  70. package/out/scope.d.ts +9 -9
  71. package/out/scope.d.ts.map +1 -1
  72. package/out/scope.js +11 -33
  73. package/out/scope.js.map +1 -1
  74. package/out/type.d.ts +38 -18
  75. package/out/type.d.ts.map +1 -1
  76. package/out/type.js +32 -1
  77. package/out/type.js.map +1 -1
  78. package/package.json +3 -3
  79. package/parser/generic.ts +11 -12
  80. package/parser/objectLiteral.ts +2 -4
  81. package/parser/semantic/validate.ts +2 -3
  82. package/parser/string/reduce/dynamic.ts +7 -10
  83. package/parser/string/shift/operand/date.ts +3 -6
  84. package/parser/string/shift/operand/enclosed.ts +8 -8
  85. package/parser/string/shift/operand/genericArgs.ts +8 -8
  86. package/parser/string/shift/operand/unenclosed.ts +14 -23
  87. package/parser/string/shift/operator/bounds.ts +7 -9
  88. package/parser/string/shift/operator/divisor.ts +2 -3
  89. package/parser/string/shift/scanner.ts +2 -4
  90. package/parser/string/string.ts +2 -3
  91. package/parser/tuple.ts +15 -16
  92. package/scope.ts +24 -48
  93. package/type.ts +150 -35
package/type.ts CHANGED
@@ -1,5 +1,8 @@
1
1
  import {
2
+ ArkErrors,
2
3
  type BaseMeta,
4
+ type BaseRoot,
5
+ type Disjoint,
3
6
  type Morph,
4
7
  type NodeDef,
5
8
  type Out,
@@ -10,31 +13,40 @@ import {
10
13
  type ambient,
11
14
  type constrain,
12
15
  type constraintKindOf,
13
- type distillConstrainableIn,
14
16
  type distillConstrainableOut,
15
17
  type distillIn,
16
18
  type distillOut,
17
19
  type includesMorphs,
18
20
  type inferIntersection,
19
21
  type inferMorphOut,
20
- type inferNarrow
22
+ type inferNarrow,
23
+ type inferPipes,
24
+ type internalImplementationOf
21
25
  } from "@arktype/schema"
22
- import type { Constructor, array, conform } from "@arktype/util"
23
- import type { Generic, validateParameterString } from "./generic.js"
26
+ import {
27
+ Callable,
28
+ type Constructor,
29
+ type array,
30
+ type conform
31
+ } from "@arktype/util"
32
+ import { Generic, type validateParameterString } from "./generic.js"
24
33
  import type {
25
34
  inferDefinition,
26
35
  validateDeclared,
27
36
  validateDefinition
28
37
  } from "./parser/definition.js"
29
- import type { parseGenericParams } from "./parser/generic.js"
38
+ import { parseGenericParams } from "./parser/generic.js"
30
39
  import type {
31
40
  IndexOneOperator,
32
41
  IndexZeroOperator,
33
42
  TupleInfixOperator
34
43
  } from "./parser/tuple.js"
35
- import type { Scope, bindThis } from "./scope.js"
44
+ import type { RawScope, Scope, bindThis } from "./scope.js"
45
+ export interface TypeParserAttachments {
46
+ errors: typeof ArkErrors
47
+ }
36
48
 
37
- export type TypeParser<$> = {
49
+ export interface TypeParser<$> extends TypeParserAttachments {
38
50
  // Parse and check the definition, returning either the original input for a
39
51
  // valid definition or a string representing an error message.
40
52
  <const def>(def: validateTypeRoot<def, $>): Type<inferTypeRoot<def, $>, $>
@@ -68,6 +80,44 @@ export type TypeParser<$> = {
68
80
  ): Generic<parseGenericParams<params>, def, $>
69
81
  }
70
82
 
83
+ const typeParserAttachments = Object.freeze({
84
+ errors: ArkErrors
85
+ } satisfies TypeParserAttachments)
86
+
87
+ export class RawTypeParser extends Callable<
88
+ (...args: unknown[]) => RawSchema | Generic,
89
+ TypeParserAttachments
90
+ > {
91
+ constructor($: RawScope) {
92
+ super(
93
+ (...args) => {
94
+ if (args.length === 1) {
95
+ // treat as a simple definition
96
+ return $.parseRoot(args[0])
97
+ }
98
+ if (
99
+ args.length === 2 &&
100
+ typeof args[0] === "string" &&
101
+ args[0][0] === "<" &&
102
+ args[0].at(-1) === ">"
103
+ ) {
104
+ // if there are exactly two args, the first of which looks like <${string}>,
105
+ // treat as a generic
106
+ const params = parseGenericParams(args[0].slice(1, -1))
107
+ const def = args[1]
108
+ // TODO: validateUninstantiatedGeneric, remove this cast
109
+ return new Generic(params, def, $ as never) as never
110
+ }
111
+ // otherwise, treat as a tuple expression. technically, this also allows
112
+ // non-expression tuple definitions to be parsed, but it's not a supported
113
+ // part of the API as specified by the associated types
114
+ return $.parseRoot(args)
115
+ },
116
+ { bind: $, attach: typeParserAttachments }
117
+ )
118
+ }
119
+ }
120
+
71
121
  export type DeclarationParser<$> = <preinferred>() => {
72
122
  // for some reason, making this a const parameter breaks preinferred validation
73
123
  type: <def>(
@@ -75,59 +125,124 @@ export type DeclarationParser<$> = <preinferred>() => {
75
125
  ) => Type<preinferred, $>
76
126
  }
77
127
 
78
- export interface Type<
79
- /** @ts-expect-error allow instantiation assignment to the base type */
80
- out t = unknown,
81
- $ = any
82
- > extends Schema<t, $> {
83
- and: (<def>(
128
+ // this is declared as a class internally so we can ensure all "abstract"
129
+ // methods of BaseRoot are overridden, but we end up exporting it as an interface
130
+ // to ensure it is not accessed as a runtime value
131
+ declare class _Type<t = unknown, $ = any>
132
+ extends BaseRoot<t, $>
133
+ implements internalImplementationOf<Schema>
134
+ {
135
+ $: Scope<$>;
136
+
137
+ get in(): Type<this["tIn"], $>
138
+ get out(): Type<this["tOut"], $>
139
+
140
+ intersect<def>(
84
141
  def: validateTypeRoot<def, $>
85
- ) => Type<inferIntersection<t, inferTypeRoot<def, $>>, $>) &
86
- Schema<t, $>["and"]
142
+ ): Type<inferIntersection<t, inferTypeRoot<def, $>>> | Disjoint
87
143
 
88
- or: (<def>(
144
+ and<def>(
89
145
  def: validateTypeRoot<def, $>
90
- ) => Type<t | inferTypeRoot<def, $>, $>) &
91
- Schema<t, $>["or"]
146
+ ): Type<inferIntersection<t, inferTypeRoot<def, $>>, $>
92
147
 
93
- get in(): Type<distillConstrainableIn<t>, $>
94
- get out(): Type<distillConstrainableOut<t>, $>
148
+ or<def>(def: validateTypeRoot<def, $>): Type<t | inferTypeRoot<def, $>, $>
95
149
 
96
150
  array(): Type<t[], $>
97
151
 
98
- morph: (<morph extends Morph<this["infer"]>, outValidatorDef = never>(
99
- morph: morph,
100
- outValidator?: validateTypeRoot<outValidatorDef, $>
101
- ) => Type<
102
- (
103
- In: distillConstrainableIn<t>
104
- ) => Out<
105
- [outValidatorDef] extends [never] ? inferMorphOut<morph>
106
- : distillConstrainableOut<inferTypeRoot<outValidatorDef, $>>
107
- >,
108
- $
109
- >) &
110
- Schema<t, $>["morph"]
152
+ keyof(): Type<keyof this["inferIn"], $>
153
+
154
+ pipe<a extends Morph<this["infer"]>>(a: a): Type<inferPipes<t, [a]>, $>
155
+ pipe<a extends Morph<this["infer"]>, b extends Morph<inferMorphOut<a>>>(
156
+ a: a,
157
+ b: b
158
+ ): Type<inferPipes<t, [a, b]>, $>
159
+ pipe<
160
+ a extends Morph<this["infer"]>,
161
+ b extends Morph<inferMorphOut<a>>,
162
+ c extends Morph<inferMorphOut<b>>
163
+ >(a: a, b: b, c: c): Type<inferPipes<t, [a, b, c]>, $>
164
+ pipe<
165
+ a extends Morph<this["infer"]>,
166
+ b extends Morph<inferMorphOut<a>>,
167
+ c extends Morph<inferMorphOut<b>>,
168
+ d extends Morph<inferMorphOut<c>>
169
+ >(a: a, b: b, c: c, d: d): Type<inferPipes<t, [a, b, c, d]>, $>
170
+ pipe<
171
+ a extends Morph<this["infer"]>,
172
+ b extends Morph<inferMorphOut<a>>,
173
+ c extends Morph<inferMorphOut<b>>,
174
+ d extends Morph<inferMorphOut<c>>,
175
+ e extends Morph<inferMorphOut<d>>
176
+ >(a: a, b: b, c: c, d: d, e: e): Type<inferPipes<t, [a, b, c, d, e]>, $>
177
+ pipe<
178
+ a extends Morph<this["infer"]>,
179
+ b extends Morph<inferMorphOut<a>>,
180
+ c extends Morph<inferMorphOut<b>>,
181
+ d extends Morph<inferMorphOut<c>>,
182
+ e extends Morph<inferMorphOut<d>>,
183
+ f extends Morph<inferMorphOut<e>>
184
+ >(
185
+ a: a,
186
+ b: b,
187
+ c: c,
188
+ d: d,
189
+ e: e,
190
+ f: f
191
+ ): Type<inferPipes<t, [a, b, c, d, e, f]>, $>
192
+ pipe<
193
+ a extends Morph<this["infer"]>,
194
+ b extends Morph<inferMorphOut<a>>,
195
+ c extends Morph<inferMorphOut<b>>,
196
+ d extends Morph<inferMorphOut<c>>,
197
+ e extends Morph<inferMorphOut<d>>,
198
+ f extends Morph<inferMorphOut<e>>,
199
+ g extends Morph<inferMorphOut<f>>
200
+ >(
201
+ a: a,
202
+ b: b,
203
+ c: c,
204
+ d: d,
205
+ e: e,
206
+ f: f,
207
+ g: g
208
+ ): Type<inferPipes<t, [a, b, c, d, e, f, g]>, $>
111
209
 
112
210
  // TODO: based on below, should maybe narrow morph output if used after
113
211
  narrow<def extends Predicate<distillConstrainableOut<t>>>(
114
212
  def: def
115
213
  ): Type<
116
214
  includesMorphs<t> extends true ?
117
- (In: distillIn<t>) => Out<inferNarrow<this["infer"], def>>
215
+ (In: this["tIn"]) => Out<inferNarrow<this["infer"], def>>
118
216
  : inferNarrow<this["infer"], def>,
119
217
  $
120
218
  >
121
219
 
220
+ equals<def>(
221
+ def: validateTypeRoot<def, $>
222
+ ): this is Type<inferTypeRoot<def>, $>
223
+
224
+ // TODO: i/o
225
+ extract<def>(r: validateTypeRoot<def, $>): Type<t, $>
226
+ exclude<def>(r: validateTypeRoot<def, $>): Type<t, $>
227
+ extends<def>(
228
+ other: validateTypeRoot<def, $>
229
+ ): this is Type<inferTypeRoot<def>, $>
230
+
122
231
  constrain<
123
232
  kind extends PrimitiveConstraintKind,
124
233
  const def extends NodeDef<kind>
125
234
  >(
126
- kind: conform<kind, constraintKindOf<this["in"]["infer"]>>,
235
+ kind: conform<kind, constraintKindOf<this["inferIn"]>>,
127
236
  def: def
128
237
  ): Type<constrain<t, kind, def>, $>
129
238
  }
130
239
 
240
+ export interface Type<
241
+ /** @ts-expect-error allow instantiation assignment to the base type */
242
+ out t = unknown,
243
+ $ = any
244
+ > extends _Type<t, $> {}
245
+
131
246
  export type TypeConstructor<t = unknown, $ = any> = new (
132
247
  def: unknown,
133
248
  $: Scope<$>