arktype 2.0.0-rc.31 → 2.0.0-rc.33
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.
- package/out/methods/base.d.ts +235 -54
- package/out/parser/string.js +5 -5
- package/out/parser/tupleExpressions.d.ts +1 -0
- package/out/type.d.ts +46 -10
- package/package.json +3 -3
package/out/methods/base.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { ArkErrors, BaseRoot, Disjoint, JsonSchema, MetaSchema, Morph, Predicate,
|
|
1
|
+
import type { ArkErrors, BaseRoot, Disjoint, JsonSchema, MetaSchema, Morph, Predicate, StandardSchemaV1, UndeclaredKeyBehavior } from "@ark/schema";
|
|
2
2
|
import type { anyOrNever, array, Callable, ErrorMessage, inferred, JsonStructure, unset } from "@ark/util";
|
|
3
3
|
import type { defaultFor, distill, inferIntersection, inferMorphOut, inferPipes, InferredMorph, Out, To } from "../attributes.ts";
|
|
4
4
|
import type { ArkAmbient } from "../config.ts";
|
|
@@ -8,53 +8,219 @@ import type { ArrayType } from "./array.ts";
|
|
|
8
8
|
import type { instantiateType } from "./instantiate.ts";
|
|
9
9
|
/** @ts-ignore cast variance */
|
|
10
10
|
interface Type<out t = unknown, $ = {}> extends Callable<(data: unknown) => distill.Out<t> | ArkErrors> {
|
|
11
|
+
internal: BaseRoot;
|
|
11
12
|
[inferred]: t;
|
|
13
|
+
/**
|
|
14
|
+
* The precompiled JS used to optimize validation.
|
|
15
|
+
* Will be `undefined` in [jitless](https://arktype.io/docs/configuration#jitless) mode.
|
|
16
|
+
*/
|
|
17
|
+
precompilation: string | undefined;
|
|
18
|
+
/**
|
|
19
|
+
* The generic parameter representing this Type
|
|
20
|
+
*
|
|
21
|
+
* - 🥸 Inference-only property that will be `undefined` at runtime
|
|
22
|
+
*
|
|
23
|
+
* - ⚠️ May contain types representing morphs or default values that would
|
|
24
|
+
* be inaccurate if used directly for runtime values. In those cases,
|
|
25
|
+
* you should use {@link infer} or {@link inferIn} on this object instead.
|
|
26
|
+
*/
|
|
12
27
|
t: t;
|
|
28
|
+
/**
|
|
29
|
+
* The {@link Scope} in which definitions for this Type its chained methods are parsed
|
|
30
|
+
* @api Type
|
|
31
|
+
*/
|
|
32
|
+
$: Scope<$>;
|
|
33
|
+
/**
|
|
34
|
+
* The type of data this returns
|
|
35
|
+
*
|
|
36
|
+
* - 🥸 Inference-only property that will be `undefined` at runtime
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* const parseNumber = type("string").pipe(s => Number.parseInt(s))
|
|
40
|
+
* type ParsedNumber = typeof parseNumber.infer // number
|
|
41
|
+
*
|
|
42
|
+
* @api Type
|
|
43
|
+
*/
|
|
13
44
|
infer: this["inferOut"];
|
|
14
|
-
|
|
45
|
+
/**
|
|
46
|
+
* Alias of {@link infer}
|
|
47
|
+
*
|
|
48
|
+
* - 🥸 Inference-only property that will be `undefined` at runtime
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* const parseNumber = type("string").pipe(s => Number.parseInt(s))
|
|
52
|
+
* type ParsedNumber = typeof parseNumber.infer // number
|
|
53
|
+
*/
|
|
15
54
|
inferOut: distill.Out<t>;
|
|
55
|
+
/**
|
|
56
|
+
* The type of output that can be introspected at runtime (e.g. via {@link out})
|
|
57
|
+
*
|
|
58
|
+
* - If your Type contains morphs, they will be inferred as `unknown` unless
|
|
59
|
+
* they are an ArkType keyword or have an explicitly defined output validator.
|
|
60
|
+
* - 🥸 Inference-only property that will be `undefined` at runtime
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* const unmorphed = type("string")
|
|
64
|
+
* // with no morphs, we can introspect the input and output as a single Type
|
|
65
|
+
* type UnmorphedOut = typeof unmorphed.inferIntrospectableOut // string
|
|
66
|
+
*
|
|
67
|
+
* const morphed = type("string").pipe(s => s.length)
|
|
68
|
+
* // with a standard user-defined morph, TypeScript can infer a
|
|
69
|
+
* // return type from your function, but we have no way to
|
|
70
|
+
* // know the shape at runtime
|
|
71
|
+
* type MorphOut = typeof morphed.inferIntrospectableOut // unknown
|
|
72
|
+
*
|
|
73
|
+
* const validated = type("string").pipe(s => s.length).to("number")
|
|
74
|
+
* // morphs with validated output, including all morph keywords, are introspectable
|
|
75
|
+
* type ValidatedMorphOut = typeof validated.inferIntrospectableOut
|
|
76
|
+
*/
|
|
77
|
+
inferIntrospectableOut: distill.introspectable.Out<t>;
|
|
78
|
+
/**
|
|
79
|
+
* The type of data this expects
|
|
80
|
+
*
|
|
81
|
+
* - 🥸 Inference-only property that will be `undefined` at runtime
|
|
82
|
+
*
|
|
83
|
+
* @example
|
|
84
|
+
* const parseNumber = type("string").pipe(s => Number.parseInt(s))
|
|
85
|
+
* type UnparsedNumber = typeof parseNumber.inferIn // string
|
|
86
|
+
* @api Type
|
|
87
|
+
*/
|
|
16
88
|
inferIn: distill.In<t>;
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
89
|
+
/**
|
|
90
|
+
* The internal JSON representation
|
|
91
|
+
* @api Type
|
|
92
|
+
*/
|
|
21
93
|
json: JsonStructure;
|
|
94
|
+
/**
|
|
95
|
+
* Alias of {@link json} for `JSON.stringify` compatibility
|
|
96
|
+
*/
|
|
22
97
|
toJSON(): JsonStructure;
|
|
23
|
-
|
|
24
|
-
|
|
98
|
+
/**
|
|
99
|
+
* Generate a JSON Schema
|
|
100
|
+
* @throws {JsonSchema.UnjsonifiableError} if this cannot be converted to JSON Schema
|
|
101
|
+
* @api Type
|
|
102
|
+
*/
|
|
25
103
|
toJsonSchema(): JsonSchema;
|
|
104
|
+
/**
|
|
105
|
+
* Metadata like custom descriptions and error messages
|
|
106
|
+
*
|
|
107
|
+
* @description The type of this property {@link https://arktype.io/docs/configuration#custom | can be extended} by your project.
|
|
108
|
+
* @api Type
|
|
109
|
+
*/
|
|
110
|
+
meta: ArkAmbient.meta;
|
|
111
|
+
/**
|
|
112
|
+
* An English description
|
|
113
|
+
*
|
|
114
|
+
* - Work best for primitive values
|
|
115
|
+
*
|
|
116
|
+
* @example
|
|
117
|
+
* const n = type("0 < number <= 100")
|
|
118
|
+
* console.log(n.description) // positive and at most 100
|
|
119
|
+
*
|
|
120
|
+
* @api Type
|
|
121
|
+
*/
|
|
26
122
|
description: string;
|
|
123
|
+
/**
|
|
124
|
+
* A syntax string similar to native TypeScript
|
|
125
|
+
*
|
|
126
|
+
* - Works well for both primitives and structures
|
|
127
|
+
*
|
|
128
|
+
* @example
|
|
129
|
+
* const loc = type({ coords: ["number", "number"] })
|
|
130
|
+
* console.log(loc.expression) // { coords: [number, number] }
|
|
131
|
+
*
|
|
132
|
+
* @api Type
|
|
133
|
+
*/
|
|
27
134
|
expression: string;
|
|
28
|
-
internal: BaseRoot;
|
|
29
|
-
$: Scope<$>;
|
|
30
135
|
/**
|
|
31
|
-
* Validate data, throwing
|
|
32
|
-
*
|
|
33
|
-
* @
|
|
136
|
+
* Validate and morph data, throwing a descriptive AggregateError on failure
|
|
137
|
+
*
|
|
138
|
+
* - Sugar to avoid checking for {@link type.errors} if they are unrecoverable
|
|
139
|
+
*
|
|
140
|
+
* @example
|
|
141
|
+
* const criticalPayload = type({
|
|
142
|
+
* superImportantValue: "string"
|
|
143
|
+
* })
|
|
144
|
+
* // throws AggregateError: superImportantValue must be a string (was missing)
|
|
145
|
+
* const data = criticalPayload.assert({ irrelevantValue: "whoops" })
|
|
146
|
+
* console.log(data.superImportantValue) // valid output can be accessed directly
|
|
147
|
+
*
|
|
148
|
+
* @throws {AggregateError}
|
|
149
|
+
* @api Type
|
|
34
150
|
*/
|
|
35
151
|
assert(data: unknown): this["infer"];
|
|
36
152
|
/**
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
153
|
+
* Validate input data without applying morphs
|
|
154
|
+
*
|
|
155
|
+
* - Good for cases like filtering that don't benefit from detailed errors
|
|
156
|
+
*
|
|
157
|
+
* @example
|
|
158
|
+
* const numeric = type("number | bigint")
|
|
159
|
+
* // [0, 2n]
|
|
160
|
+
* const numerics = [0, "one", 2n].filter(numeric.allows)
|
|
161
|
+
*
|
|
162
|
+
* @api Type
|
|
40
163
|
*/
|
|
41
164
|
allows(data: unknown): data is this["inferIn"];
|
|
42
|
-
|
|
165
|
+
/**
|
|
166
|
+
* Clone and add metadata to shallow references
|
|
167
|
+
*
|
|
168
|
+
* - Does not affect error messages within properties of an object
|
|
169
|
+
* - Overlapping keys on existing meta will be overwritten
|
|
170
|
+
*
|
|
171
|
+
* @example
|
|
172
|
+
* const notOdd = type("number % 2").configure({ description: "not odd" })
|
|
173
|
+
* // all constraints at the root are affected
|
|
174
|
+
* const odd = notOdd(3) // must be not odd (was 3)
|
|
175
|
+
* const nonNumber = notOdd("two") // must be not odd (was "two")
|
|
176
|
+
*
|
|
177
|
+
* const notOddBox = type({
|
|
178
|
+
* // we should have referenced notOdd or added meta here
|
|
179
|
+
* notOdd: "number % 2",
|
|
180
|
+
* // but instead chained from the root object
|
|
181
|
+
* }).configure({ description: "not odd" })
|
|
182
|
+
* // error message at path notOdd is not affected
|
|
183
|
+
* const oddProp = notOddBox({ notOdd: 3 }) // notOdd must be even (was 3)
|
|
184
|
+
* // error message at root is affected, leading to a misleading description
|
|
185
|
+
* const nonObject = notOddBox(null) // must be not odd (was null)
|
|
186
|
+
*
|
|
187
|
+
* @api Type
|
|
188
|
+
*/
|
|
43
189
|
configure(meta: MetaSchema): this;
|
|
190
|
+
/**
|
|
191
|
+
* Clone and add the description to shallow references
|
|
192
|
+
*
|
|
193
|
+
* - Equivalent to `.configure({ description })` (see {@link configure})
|
|
194
|
+
* - Does not affect error messages within properties of an object
|
|
195
|
+
*
|
|
196
|
+
* @example
|
|
197
|
+
* const aToZ = type(/^a.*z$/).describe("a string like 'a...z'")
|
|
198
|
+
* const good = aToZ("alcatraz") // "alcatraz"
|
|
199
|
+
* // notice how our description is integrated with other parts of the message
|
|
200
|
+
* const badPattern = aToZ("albatross") // must be a string like 'a...z' (was "albatross")
|
|
201
|
+
* const nonString = aToZ(123) // must be a string like 'a...z' (was 123)
|
|
202
|
+
*
|
|
203
|
+
* @api Type
|
|
204
|
+
*/
|
|
44
205
|
describe(description: string): this;
|
|
45
206
|
/**
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
* -
|
|
49
|
-
*
|
|
207
|
+
* Clone to a new Type with the specified undeclared key behavior.
|
|
208
|
+
*
|
|
209
|
+
* `"ignore"` (default) - allow and preserve extra properties
|
|
210
|
+
*
|
|
211
|
+
* `"reject"` - disallow extra properties
|
|
212
|
+
*
|
|
213
|
+
* `"delete"` - clone and remove extra properties from output
|
|
50
214
|
*/
|
|
51
215
|
onUndeclaredKey(behavior: UndeclaredKeyBehavior): this;
|
|
52
216
|
/**
|
|
53
|
-
*
|
|
54
|
-
*
|
|
55
|
-
*
|
|
56
|
-
*
|
|
57
|
-
* -
|
|
217
|
+
* Deeply clone to a new Type with the specified undeclared key behavior.
|
|
218
|
+
*
|
|
219
|
+
* `"ignore"` (default) - allow and preserve extra properties
|
|
220
|
+
*
|
|
221
|
+
* `"reject"` - disallow extra properties
|
|
222
|
+
*
|
|
223
|
+
* `"delete"` - clone and remove extra properties from output
|
|
58
224
|
*/
|
|
59
225
|
onDeepUndeclaredKey(behavior: UndeclaredKeyBehavior): this;
|
|
60
226
|
/**
|
|
@@ -62,12 +228,6 @@ interface Type<out t = unknown, $ = {}> extends Callable<(data: unknown) => dist
|
|
|
62
228
|
* @example const ConfigT = type({ foo: "string" }); export const config = ConfigT.from({ foo: "bar" })
|
|
63
229
|
*/
|
|
64
230
|
from(literal: this["inferIn"]): this["infer"];
|
|
65
|
-
/**
|
|
66
|
-
* Cast the way this `Type` is inferred (has no effect at runtime).
|
|
67
|
-
* const branded = type(/^a/).as<`a${string}`>() // Type<`a${string}`>
|
|
68
|
-
*/
|
|
69
|
-
as<castTo = unset>(...args: validateChainedAsArgs<castTo>): instantiateType<castTo, $>;
|
|
70
|
-
brand<const name extends string, r = type.brand<t, name>>(name: name): instantiateType<r, $>;
|
|
71
231
|
/**
|
|
72
232
|
* A `Type` representing the deeply-extracted input of the `Type` (before morphs are applied).
|
|
73
233
|
* @example const inputT = T.in
|
|
@@ -81,11 +241,11 @@ interface Type<out t = unknown, $ = {}> extends Callable<(data: unknown) => dist
|
|
|
81
241
|
*/
|
|
82
242
|
get out(): instantiateType<this["inferIntrospectableOut"], $>;
|
|
83
243
|
/**
|
|
84
|
-
*
|
|
85
|
-
*
|
|
86
|
-
* @example const intersection = type({ foo: "number" }).intersect({ foo: "string" }) // Disjoint
|
|
244
|
+
* Cast the way this `Type` is inferred (has no effect at runtime).
|
|
245
|
+
* const branded = type(/^a/).as<`a${string}`>() // Type<`a${string}`>
|
|
87
246
|
*/
|
|
88
|
-
|
|
247
|
+
as<castTo = unset>(...args: validateChainedAsArgs<castTo>): instantiateType<castTo, $>;
|
|
248
|
+
brand<const name extends string, r = type.brand<t, name>>(name: name): instantiateType<r, $>;
|
|
89
249
|
/**
|
|
90
250
|
* Intersect another `Type` definition, throwing an error if the result is unsatisfiable.
|
|
91
251
|
* @example const intersection = type({ foo: "number" }).intersect({ bar: "string" }) // Type<{ foo: number; bar: string }>
|
|
@@ -98,25 +258,41 @@ interface Type<out t = unknown, $ = {}> extends Callable<(data: unknown) => dist
|
|
|
98
258
|
* @example const union = type("string.numeric.parse").or("number") // Type<((In: string) => Out<number>) | number>
|
|
99
259
|
*/
|
|
100
260
|
or<const def, r = type.infer<def, $>>(def: type.validate<def, $>): instantiateType<t | r, $>;
|
|
261
|
+
/**
|
|
262
|
+
* Create a `Type` for array with elements of this `Type`
|
|
263
|
+
* @example const T = type(/^foo/); const array = T.array() // Type<string[]>
|
|
264
|
+
*/
|
|
265
|
+
array(): ArrayType<t[], $>;
|
|
266
|
+
optional(): [this, "?"];
|
|
267
|
+
/**
|
|
268
|
+
* Add a default value for this `Type` when it is used as a property.\
|
|
269
|
+
* Default value should be a valid input value for this `Type, or a function that returns a valid input value.\
|
|
270
|
+
* If the type has a morph, it will be applied to the default value.
|
|
271
|
+
* @example const withDefault = type({ foo: type("string").default("bar") }); withDefault({}) // { foo: "bar" }
|
|
272
|
+
* @example const withFactory = type({ foo: type("number[]").default(() => [1])) }); withFactory({baz: 'a'}) // { foo: [1], baz: 'a' }
|
|
273
|
+
* @example const withMorph = type({ foo: type("string.numeric.parse").default("123") }); withMorph({}) // { foo: 123 }
|
|
274
|
+
*/
|
|
275
|
+
default<const value extends defaultFor<this["inferIn"]>>(value: value): [this, "=", value];
|
|
276
|
+
filter<narrowed extends this["inferIn"] = never, r = [narrowed] extends [never] ? t : t extends InferredMorph<any, infer o> ? (In: narrowed) => o : narrowed>(predicate: Predicate.Castable<this["inferIn"]>): instantiateType<r, $>;
|
|
101
277
|
/**
|
|
102
278
|
* Add a custom predicate to this `Type`.
|
|
103
279
|
* @example const nan = type('number').narrow(n => Number.isNaN(n)) // Type<number>
|
|
104
280
|
* @example const foo = type("string").narrow((s): s is `foo${string}` => s.startsWith('foo') || ctx.mustBe('string starting with "foo"')) // Type<"foo${string}">
|
|
105
281
|
* @example const unique = type('string[]').narrow((a, ctx) => new Set(a).size === a.length || ctx.mustBe('array with unique elements'))
|
|
106
282
|
*/
|
|
107
|
-
narrow<narrowed extends this["infer"] = never, r = [narrowed] extends [never] ? t : t extends InferredMorph<infer i, infer o> ? o extends To ? (In: i) => To<narrowed> : (In: i) => Out<narrowed> : narrowed>(predicate: Predicate<this["infer"]
|
|
108
|
-
satisfying<narrowed extends this["inferIn"] = never, r = [narrowed] extends [never] ? t : t extends InferredMorph<any, infer o> ? (In: narrowed) => o : narrowed>(predicate: Predicate<this["inferIn"]> | PredicateCast<this["inferIn"], narrowed>): instantiateType<r, $>;
|
|
109
|
-
/**
|
|
110
|
-
* Create a `Type` for array with elements of this `Type`
|
|
111
|
-
* @example const T = type(/^foo/); const array = T.array() // Type<string[]>
|
|
112
|
-
*/
|
|
113
|
-
array(): ArrayType<t[], $>;
|
|
283
|
+
narrow<narrowed extends this["infer"] = never, r = [narrowed] extends [never] ? t : t extends InferredMorph<infer i, infer o> ? o extends To ? (In: i) => To<narrowed> : (In: i) => Out<narrowed> : narrowed>(predicate: Predicate.Castable<this["infer"], narrowed>): instantiateType<r, $>;
|
|
114
284
|
/**
|
|
115
285
|
* Morph this `Type` through a chain of morphs.
|
|
116
286
|
* @example const dedupe = type('string[]').pipe(a => Array.from(new Set(a)))
|
|
117
287
|
* @example type({codes: 'string.numeric[]'}).pipe(obj => obj.codes).to('string.numeric.parse[]')
|
|
118
288
|
*/
|
|
119
|
-
pipe:
|
|
289
|
+
pipe: ChainedPipe<t, $>;
|
|
290
|
+
/**
|
|
291
|
+
* Intersect another `Type` definition, returning an introspectable `Disjoint` if the result is unsatisfiable.
|
|
292
|
+
* @example const intersection = type({ foo: "number" }).intersect({ bar: "string" }) // Type<{ foo: number; bar: string }>
|
|
293
|
+
* @example const intersection = type({ foo: "number" }).intersect({ foo: "string" }) // Disjoint
|
|
294
|
+
*/
|
|
295
|
+
intersect<const def, r = type.infer<def, $>>(def: type.validate<def, $>): instantiateType<inferIntersection<t, r>, $> | Disjoint;
|
|
120
296
|
equals<const def>(def: type.validate<def, $>): boolean;
|
|
121
297
|
ifEquals<const def, r = type.infer<def, $>>(def: type.validate<def, $>): instantiateType<r, $> | undefined;
|
|
122
298
|
extends<const def>(other: type.validate<def, $>): boolean;
|
|
@@ -124,17 +300,22 @@ interface Type<out t = unknown, $ = {}> extends Callable<(data: unknown) => dist
|
|
|
124
300
|
overlaps<const def>(r: type.validate<def, $>): boolean;
|
|
125
301
|
extract<const def, r = type.infer<def, $>>(r: type.validate<def, $>): instantiateType<Extract<t, r>, $>;
|
|
126
302
|
exclude<const def, r = type.infer<def, $>>(r: type.validate<def, $>): instantiateType<Exclude<t, r>, $>;
|
|
127
|
-
|
|
128
|
-
optional(): [this, "?"];
|
|
303
|
+
traverse(data: unknown): this["infer"] | ArkErrors;
|
|
129
304
|
/**
|
|
130
|
-
*
|
|
131
|
-
*
|
|
132
|
-
*
|
|
133
|
-
*
|
|
134
|
-
* @
|
|
135
|
-
*
|
|
305
|
+
* @experimental
|
|
306
|
+
* Map and optionally reduce branches of a union. Types that are not unions
|
|
307
|
+
* are treated as a single branch.
|
|
308
|
+
*
|
|
309
|
+
* @param mapBranch - the mapping function, accepting a branch Type
|
|
310
|
+
* Returning another `Type` is common, but any value can be returned and
|
|
311
|
+
* inferred as part of the output.
|
|
312
|
+
*
|
|
313
|
+
* @param [reduceMapped] - an operation to perform on the mapped branches
|
|
314
|
+
* Can be used to e.g. merge an array of returned Types representing
|
|
315
|
+
* branches back to a single union.
|
|
136
316
|
*/
|
|
137
|
-
|
|
317
|
+
distribute<mapOut, reduceOut = mapOut[]>(mapBranch: (branch: Type, i: number, branches: array<Type>) => mapOut, reduceMapped?: (mappedBranches: mapOut[]) => reduceOut): reduceOut;
|
|
318
|
+
/** The Type's [StandardSchema](https://github.com/standard-schema/standard-schema) properties */
|
|
138
319
|
"~standard": StandardSchemaV1.ArkTypeProps<this["inferIn"], this["inferOut"]>;
|
|
139
320
|
/** @deprecated */
|
|
140
321
|
apply: Function["apply"];
|
|
@@ -164,7 +345,7 @@ interface ChainedPipeSignature<t, $> {
|
|
|
164
345
|
<a extends Morph<distill.Out<t>>, b extends Morph<inferMorphOut<a>>, c extends Morph<inferMorphOut<b>>, d extends Morph<inferMorphOut<c>>, e extends Morph<inferMorphOut<d>>, f extends Morph<inferMorphOut<e>>, r = instantiateType<inferPipes<t, [a, b, c, d, e, f]>, $>>(a: a, b: b, c: c, d: d, e: e, f: f): NoInfer<r> extends infer result ? result : never;
|
|
165
346
|
<a extends Morph<distill.Out<t>>, b extends Morph<inferMorphOut<a>>, c extends Morph<inferMorphOut<b>>, d extends Morph<inferMorphOut<c>>, e extends Morph<inferMorphOut<d>>, f extends Morph<inferMorphOut<e>>, g extends Morph<inferMorphOut<f>>, r = instantiateType<inferPipes<t, [a, b, c, d, e, f, g]>, $>>(a: a, b: b, c: c, d: d, e: e, f: f, g: g): NoInfer<r> extends infer result ? result : never;
|
|
166
347
|
}
|
|
167
|
-
export interface
|
|
348
|
+
export interface ChainedPipe<t, $> extends ChainedPipeSignature<t, $> {
|
|
168
349
|
try: ChainedPipeSignature<t, $>;
|
|
169
350
|
}
|
|
170
351
|
type validateChainedAsArgs<t> = [
|
package/out/parser/string.js
CHANGED
|
@@ -7,11 +7,11 @@ export const parseString = (def, ctx) => {
|
|
|
7
7
|
const aliasResolution = ctx.$.maybeResolveRoot(def);
|
|
8
8
|
if (aliasResolution)
|
|
9
9
|
return aliasResolution;
|
|
10
|
-
|
|
11
|
-
ctx.$.maybeResolveRoot(def.slice(0, -2))
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
10
|
+
if (def.endsWith("[]")) {
|
|
11
|
+
const possibleElementResolution = ctx.$.maybeResolveRoot(def.slice(0, -2));
|
|
12
|
+
if (possibleElementResolution)
|
|
13
|
+
return possibleElementResolution.array();
|
|
14
|
+
}
|
|
15
15
|
const s = new DynamicState(new ArkTypeScanner(def), ctx);
|
|
16
16
|
const node = fullStringParse(s);
|
|
17
17
|
if (s.finalizer === ">")
|
|
@@ -30,6 +30,7 @@ export type PrefixParser<token extends IndexZeroOperator> = (def: IndexZeroExpre
|
|
|
30
30
|
export type TupleExpression = IndexZeroExpression | IndexOneExpression;
|
|
31
31
|
export type TupleExpressionOperator = IndexZeroOperator | IndexOneOperator;
|
|
32
32
|
export type IndexOneOperator = TuplePostfixOperator | TupleInfixOperator;
|
|
33
|
+
export type ArgTwoOperator = Exclude<IndexOneOperator, "?" | "=">;
|
|
33
34
|
export type TuplePostfixOperator = "[]" | "?";
|
|
34
35
|
export type TupleInfixOperator = "&" | "|" | "=>" | "=" | ":" | "@";
|
|
35
36
|
export type IndexOneExpression<token extends IndexOneOperator = IndexOneOperator> = readonly [unknown, token, ...unknown[]];
|
package/out/type.d.ts
CHANGED
|
@@ -6,23 +6,59 @@ import type { Ark, keywords, type } from "./keywords/keywords.ts";
|
|
|
6
6
|
import type { BaseType } from "./methods/base.ts";
|
|
7
7
|
import type { instantiateType } from "./methods/instantiate.ts";
|
|
8
8
|
import type { validateDeclared } from "./parser/definition.ts";
|
|
9
|
-
import type {
|
|
9
|
+
import type { ArgTwoOperator, IndexZeroOperator, TupleInfixOperator } from "./parser/tupleExpressions.ts";
|
|
10
10
|
import type { InternalScope, ModuleParser, Scope, ScopeParser, bindThis } from "./scope.ts";
|
|
11
11
|
/** The convenience properties attached to `type` */
|
|
12
12
|
export type TypeParserAttachments = Omit<TypeParser, never>;
|
|
13
13
|
export interface TypeParser<$ = {}> extends Ark.boundTypeAttachments<$> {
|
|
14
|
+
/**
|
|
15
|
+
* Create a {@link Type} from your definition.
|
|
16
|
+
*
|
|
17
|
+
* @example const person = type({ name: "string" })
|
|
18
|
+
*/
|
|
14
19
|
<const def, r = Type<type.infer<def, $>, $>>(def: type.validate<def, $>): r;
|
|
20
|
+
/**
|
|
21
|
+
* Create a {@link Generic} from a parameter string and body definition.
|
|
22
|
+
*
|
|
23
|
+
* @param params A string like "<t, n extends number>" specifying the
|
|
24
|
+
* {@link Generic}'s parameters and any associated constraints via `extends`.
|
|
25
|
+
*
|
|
26
|
+
* @param def The definition for the body of the {@link Generic}. Can reference the
|
|
27
|
+
* parameter names specified in the previous argument in addition to aliases
|
|
28
|
+
* from its {@link Scope}.
|
|
29
|
+
*
|
|
30
|
+
* @example const boxOf = type("<t extends string | number>", { contents: "t" })
|
|
31
|
+
*/
|
|
15
32
|
<const params extends ParameterString, const def>(params: validateParameterString<params, $>, def: type.validate<def, $, baseGenericConstraints<parseValidGenericParams<params, $>>>): Generic<parseValidGenericParams<params, $>, def, $>;
|
|
16
|
-
<const zero, const one, const rest extends array, r = Type<type.infer<[zero, one, ...rest], $>, $>>(_0: zero extends IndexZeroOperator ? zero : type.validate<zero, $>, _1: zero extends "keyof" ? type.validate<one, $> : zero extends "instanceof" ? conform<one, Constructor> : zero extends "===" ? conform<one, unknown> : conform<one, IndexOneOperator>, ..._2: zero extends "===" ? rest : zero extends "instanceof" ? conform<rest, readonly Constructor[]> : one extends TupleInfixOperator ? one extends ":" ? [Predicate<distill.In<type.infer<zero, $>>>] : one extends "=>" ? [Morph<distill.Out<type.infer<zero, $>>, unknown>] : one extends "@" ? [MetaSchema] : [type.validate<rest[0], $>] : []): r;
|
|
17
33
|
/**
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
34
|
+
* Create a {@link Type} from a [tuple expression](http://localhost:3000/docs/expressions)
|
|
35
|
+
* spread as this function's arguments.
|
|
36
|
+
*
|
|
37
|
+
* @example type("string", "|", { foo: "number" })
|
|
38
|
+
*/
|
|
39
|
+
<const zero, const one, const rest extends array, r = Type<type.infer<[zero, one, ...rest], $>, $>>(_0: zero extends IndexZeroOperator ? zero : type.validate<zero, $>, _1: zero extends "keyof" ? type.validate<one, $> : zero extends "instanceof" ? conform<one, Constructor> : zero extends "===" ? conform<one, unknown> : conform<one, ArgTwoOperator>, ..._2: zero extends "===" ? rest : zero extends "instanceof" ? conform<rest, readonly Constructor[]> : one extends TupleInfixOperator ? one extends ":" ? [Predicate<distill.In<type.infer<zero, $>>>] : one extends "=>" ? [Morph<distill.Out<type.infer<zero, $>>, unknown>] : one extends "@" ? [MetaSchema] : [type.validate<rest[0], $>] : []): r;
|
|
40
|
+
/**
|
|
41
|
+
* An alias of the {@link ArkErrors} class, an instance of which is returned when a {@link Type}
|
|
42
|
+
* is invoked with invalid input.
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* const out = myType(data)
|
|
46
|
+
*
|
|
47
|
+
* if(out instanceof type.errors) console.log(out.summary)
|
|
48
|
+
*
|
|
21
49
|
*/
|
|
22
50
|
errors: typeof ArkErrors;
|
|
23
51
|
hkt: typeof Hkt;
|
|
24
52
|
keywords: typeof keywords;
|
|
53
|
+
/**
|
|
54
|
+
* The {@link Scope} in which definitions passed to this function will be parsed.
|
|
55
|
+
*/
|
|
25
56
|
$: Scope<$>;
|
|
57
|
+
/**
|
|
58
|
+
* An alias of `type` with no type-level validation or inference.
|
|
59
|
+
*
|
|
60
|
+
* Useful when wrapping `type` or using it to parse a dynamic definition.
|
|
61
|
+
*/
|
|
26
62
|
raw(def: unknown): BaseType<any, $>;
|
|
27
63
|
module: ModuleParser;
|
|
28
64
|
scope: ScopeParser;
|
|
@@ -30,20 +66,20 @@ export interface TypeParser<$ = {}> extends Ark.boundTypeAttachments<$> {
|
|
|
30
66
|
generic: GenericParser<$>;
|
|
31
67
|
schema: SchemaParser<$>;
|
|
32
68
|
/**
|
|
33
|
-
* Create a
|
|
34
|
-
* @example const foo = type.unit('foo') // Type<'foo'>
|
|
35
|
-
* @example const sym: unique symbol = Symbol(); type.unit(sym) // Type<typeof sym>
|
|
69
|
+
* Create a {@link Type} that is satisfied only by a value strictly equal (`===`) to the argument passed to this function.
|
|
70
|
+
* @example const foo = type.unit('foo') // {@link Type}<'foo'>
|
|
71
|
+
* @example const sym: unique symbol = Symbol(); type.unit(sym) // {@link Type}<typeof sym>
|
|
36
72
|
*/
|
|
37
73
|
unit: UnitTypeParser<$>;
|
|
38
74
|
/**
|
|
39
|
-
* Create a
|
|
75
|
+
* Create a {@link Type} that is satisfied only by a value strictly equal (`===`) to one of the arguments passed to this function.
|
|
40
76
|
* @example const enum = type.enumerated('foo', 'bar', obj) // obj is a by-reference object
|
|
41
77
|
* @example const tupleForm = type(['===', 'foo', 'bar', obj])
|
|
42
78
|
* @example const argsForm = type('===', 'foo', 'bar', obj)
|
|
43
79
|
*/
|
|
44
80
|
enumerated: EnumeratedTypeParser<$>;
|
|
45
81
|
/**
|
|
46
|
-
* Create a
|
|
82
|
+
* Create a {@link Type} that is satisfied only by a value of a specific class.
|
|
47
83
|
* @example const array = type.instanceOf(Array)
|
|
48
84
|
*/
|
|
49
85
|
instanceOf: InstanceOfTypeParser<$>;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "arktype",
|
|
3
3
|
"description": "TypeScript's 1:1 validator, optimized from editor to runtime",
|
|
4
|
-
"version": "2.0.0-rc.
|
|
4
|
+
"version": "2.0.0-rc.33",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": {
|
|
7
7
|
"name": "David Blass",
|
|
@@ -34,8 +34,8 @@
|
|
|
34
34
|
"out"
|
|
35
35
|
],
|
|
36
36
|
"dependencies": {
|
|
37
|
-
"@ark/
|
|
38
|
-
"@ark/
|
|
37
|
+
"@ark/schema": "0.33.0",
|
|
38
|
+
"@ark/util": "0.33.0"
|
|
39
39
|
},
|
|
40
40
|
"publishConfig": {
|
|
41
41
|
"access": "public"
|