arktype 2.0.0-rc.9 → 2.0.0
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/attributes.d.ts +94 -0
- package/out/attributes.js +3 -0
- package/out/generic.d.ts +13 -13
- package/out/index.d.ts +3 -1
- package/out/index.js +2 -1
- package/out/keywords/{constructors/Array.d.ts → Array.d.ts} +2 -2
- package/out/keywords/{constructors/Array.js → Array.js} +2 -2
- package/out/keywords/{constructors/FormData.d.ts → FormData.d.ts} +2 -2
- package/out/keywords/{constructors/FormData.js → FormData.js} +2 -2
- package/out/keywords/{constructors/TypedArray.d.ts → TypedArray.d.ts} +1 -1
- package/out/keywords/{constructors/TypedArray.js → TypedArray.js} +2 -2
- package/out/keywords/builtins.js +2 -2
- package/out/keywords/{constructors/constructors.d.ts → constructors.d.ts} +1 -1
- package/out/keywords/{constructors/constructors.js → constructors.js} +2 -2
- package/out/keywords/keywords.d.ts +14 -13
- package/out/keywords/keywords.js +9 -5
- package/out/keywords/number.d.ts +23 -0
- package/out/keywords/{number/epoch.js → number.js} +22 -1
- package/out/keywords/string.d.ts +327 -0
- package/out/keywords/string.js +398 -0
- package/out/keywords/ts.d.ts +31 -4
- package/out/keywords/ts.js +21 -8
- package/out/methods/array.d.ts +6 -7
- package/out/methods/base.d.ts +461 -35
- package/out/methods/date.d.ts +5 -6
- package/out/methods/instantiate.d.ts +4 -4
- package/out/methods/morph.d.ts +6 -6
- package/out/methods/number.d.ts +8 -9
- package/out/methods/object.d.ts +39 -15
- package/out/methods/string.d.ts +9 -10
- package/out/parser/ast/bounds.d.ts +1 -1
- package/out/parser/ast/default.d.ts +2 -2
- package/out/parser/ast/generic.d.ts +17 -0
- package/out/parser/ast/infer.d.ts +8 -22
- package/out/parser/ast/validate.d.ts +13 -9
- package/out/parser/ast/validate.js +3 -1
- package/out/parser/definition.d.ts +17 -8
- package/out/parser/definition.js +38 -3
- package/out/parser/objectLiteral.d.ts +39 -39
- package/out/parser/objectLiteral.js +83 -67
- package/out/parser/property.d.ts +36 -0
- package/out/parser/property.js +16 -0
- package/out/parser/reduce/dynamic.d.ts +8 -9
- package/out/parser/reduce/shared.d.ts +1 -1
- package/out/parser/reduce/static.d.ts +4 -4
- package/out/parser/shift/operand/date.d.ts +1 -1
- package/out/parser/shift/operand/enclosed.d.ts +3 -4
- package/out/parser/shift/operand/operand.d.ts +3 -3
- package/out/parser/shift/operand/operand.js +2 -2
- package/out/parser/shift/operand/unenclosed.d.ts +13 -5
- package/out/parser/shift/operand/unenclosed.js +5 -4
- package/out/parser/shift/operator/bounds.d.ts +5 -5
- package/out/parser/shift/operator/bounds.js +5 -4
- package/out/parser/shift/operator/brand.d.ts +6 -0
- package/out/parser/shift/operator/brand.js +5 -0
- package/out/parser/shift/operator/default.d.ts +3 -2
- package/out/parser/shift/operator/default.js +4 -1
- package/out/parser/shift/operator/divisor.d.ts +2 -2
- package/out/parser/shift/operator/operator.d.ts +4 -3
- package/out/parser/shift/operator/operator.js +7 -5
- package/out/parser/shift/scanner.d.ts +31 -55
- package/out/parser/shift/scanner.js +24 -83
- package/out/parser/string.d.ts +5 -4
- package/out/parser/string.js +18 -1
- package/out/parser/tupleExpressions.d.ts +48 -0
- package/out/parser/tupleExpressions.js +67 -0
- package/out/parser/tupleLiteral.d.ts +95 -0
- package/out/parser/tupleLiteral.js +122 -0
- package/out/scope.d.ts +17 -12
- package/out/scope.js +25 -41
- package/out/type.d.ts +63 -5
- package/out/type.js +3 -2
- package/package.json +8 -8
- package/out/keywords/constructors/Date.d.ts +0 -31
- package/out/keywords/inference.d.ts +0 -220
- package/out/keywords/inference.js +0 -2
- package/out/keywords/number/epoch.d.ts +0 -9
- package/out/keywords/number/integer.d.ts +0 -3
- package/out/keywords/number/integer.js +0 -5
- package/out/keywords/number/number.d.ts +0 -53
- package/out/keywords/number/number.js +0 -21
- package/out/keywords/string/alpha.d.ts +0 -7
- package/out/keywords/string/alpha.js +0 -2
- package/out/keywords/string/alphanumeric.d.ts +0 -7
- package/out/keywords/string/alphanumeric.js +0 -2
- package/out/keywords/string/capitalize.d.ts +0 -15
- package/out/keywords/string/capitalize.js +0 -12
- package/out/keywords/string/creditCard.d.ts +0 -8
- package/out/keywords/string/creditCard.js +0 -37
- package/out/keywords/string/date.d.ts +0 -55
- package/out/keywords/string/date.js +0 -98
- package/out/keywords/string/digits.d.ts +0 -7
- package/out/keywords/string/digits.js +0 -2
- package/out/keywords/string/email.d.ts +0 -7
- package/out/keywords/string/email.js +0 -4
- package/out/keywords/string/integer.d.ts +0 -16
- package/out/keywords/string/integer.js +0 -16
- package/out/keywords/string/ip.d.ts +0 -20
- package/out/keywords/string/ip.js +0 -23
- package/out/keywords/string/json.d.ts +0 -16
- package/out/keywords/string/json.js +0 -53
- package/out/keywords/string/lower.d.ts +0 -15
- package/out/keywords/string/lower.js +0 -12
- package/out/keywords/string/normalize.d.ts +0 -88
- package/out/keywords/string/normalize.js +0 -43
- package/out/keywords/string/numeric.d.ts +0 -15
- package/out/keywords/string/numeric.js +0 -13
- package/out/keywords/string/semver.d.ts +0 -7
- package/out/keywords/string/semver.js +0 -4
- package/out/keywords/string/string.d.ts +0 -108
- package/out/keywords/string/string.js +0 -41
- package/out/keywords/string/trim.d.ts +0 -15
- package/out/keywords/string/trim.js +0 -14
- package/out/keywords/string/upper.d.ts +0 -15
- package/out/keywords/string/upper.js +0 -12
- package/out/keywords/string/url.d.ts +0 -15
- package/out/keywords/string/url.js +0 -37
- package/out/keywords/string/utils.d.ts +0 -2
- package/out/keywords/string/utils.js +0 -10
- package/out/keywords/string/uuid.d.ts +0 -42
- package/out/keywords/string/uuid.js +0 -19
- package/out/keywords/utils.d.ts +0 -1
- package/out/keywords/utils.js +0 -2
- package/out/methods/validator.d.ts +0 -11
- package/out/methods/validator.js +0 -1
- package/out/parser/tuple.d.ts +0 -143
- package/out/parser/tuple.js +0 -164
- /package/out/{keywords/constructors/Date.js → parser/ast/generic.js} +0 -0
package/out/methods/base.d.ts
CHANGED
|
@@ -1,56 +1,482 @@
|
|
|
1
|
-
import type { ArkErrors, BaseRoot, Disjoint, JsonSchema, MetaSchema, Morph, UndeclaredKeyBehavior } from "@ark/schema";
|
|
2
|
-
import type { anyOrNever, array, Callable, ErrorMessage, inferred,
|
|
1
|
+
import type { ArkErrors, BaseRoot, Disjoint, JsonSchema, MetaSchema, Morph, Predicate, StandardSchemaV1, UndeclaredKeyBehavior } from "@ark/schema";
|
|
2
|
+
import type { anyOrNever, array, Callable, ErrorMessage, inferred, JsonStructure, unset } from "@ark/util";
|
|
3
|
+
import type { defaultFor, distill, inferIntersection, inferMorphOut, inferPipes, InferredMorph, Out, To } from "../attributes.ts";
|
|
3
4
|
import type { ArkAmbient } from "../config.ts";
|
|
4
|
-
import type { applyConstraint, Default, distill, inferIntersection, inferMorphOut, inferPipes, Optional } from "../keywords/inference.ts";
|
|
5
5
|
import type { type } from "../keywords/keywords.ts";
|
|
6
6
|
import type { Scope } from "../scope.ts";
|
|
7
7
|
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;
|
|
12
|
+
[inferred]: t;
|
|
13
|
+
/**
|
|
14
|
+
* precompiled JS used to optimize validation
|
|
15
|
+
*
|
|
16
|
+
* ⚠️ will be `undefined` in [jitless](https://arktype.io/docs/configuration#jitless) mode
|
|
17
|
+
*/
|
|
18
|
+
precompilation: string | undefined;
|
|
19
|
+
/**
|
|
20
|
+
* generic parameter representing this Type
|
|
21
|
+
*
|
|
22
|
+
* 🥸 inference-only property that will be `undefined` at runtime
|
|
23
|
+
*
|
|
24
|
+
* ⚠️ May contain types representing morphs or default values that would
|
|
25
|
+
* be inaccurate if used directly for runtime values. In those cases,
|
|
26
|
+
* you should use {@link infer} or {@link inferIn} on this object instead.
|
|
27
|
+
*/
|
|
11
28
|
t: t;
|
|
29
|
+
/**
|
|
30
|
+
* #### {@link Scope} in which chained methods are parsed
|
|
31
|
+
*/
|
|
32
|
+
$: Scope<$>;
|
|
33
|
+
/**
|
|
34
|
+
* #### type of output 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
|
+
*/
|
|
12
42
|
infer: this["inferOut"];
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
43
|
+
/**
|
|
44
|
+
* type of output this returns
|
|
45
|
+
*
|
|
46
|
+
* 🔗 alias of {@link infer}
|
|
47
|
+
* 🥸 inference-only property that will be `undefined` at runtime
|
|
48
|
+
*
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* const parseNumber = type("string").pipe(s => Number.parseInt(s))
|
|
52
|
+
* type ParsedNumber = typeof parseNumber.infer // number
|
|
53
|
+
*/
|
|
16
54
|
inferOut: distill.Out<t>;
|
|
55
|
+
/**
|
|
56
|
+
* 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
|
+
*
|
|
61
|
+
* 🥸 inference-only property that will be `undefined` at runtime
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* const unmorphed = type("string")
|
|
65
|
+
* // with no morphs, we can introspect the input and output as a single Type
|
|
66
|
+
* type UnmorphedOut = typeof unmorphed.inferIntrospectableOut // string
|
|
67
|
+
*
|
|
68
|
+
* const morphed = type("string").pipe(s => s.length)
|
|
69
|
+
* // with a standard user-defined morph, TypeScript can infer a
|
|
70
|
+
* // return type from your function, but we have no way to
|
|
71
|
+
* // know the shape at runtime
|
|
72
|
+
* type MorphOut = typeof morphed.inferIntrospectableOut // unknown
|
|
73
|
+
*
|
|
74
|
+
* const validated = type("string").pipe(s => s.length).to("number")
|
|
75
|
+
* // morphs with validated output, including all morph keywords, are introspectable
|
|
76
|
+
* type ValidatedMorphOut = typeof validated.inferIntrospectableOut
|
|
77
|
+
*/
|
|
78
|
+
inferIntrospectableOut: distill.introspectable.Out<t>;
|
|
79
|
+
/**
|
|
80
|
+
* #### type of input this allows
|
|
81
|
+
*
|
|
82
|
+
* 🥸 inference-only property that will be `undefined` at runtime
|
|
83
|
+
*
|
|
84
|
+
* @example
|
|
85
|
+
* const parseNumber = type("string").pipe(s => Number.parseInt(s))
|
|
86
|
+
* type UnparsedNumber = typeof parseNumber.inferIn // string
|
|
87
|
+
*/
|
|
17
88
|
inferIn: distill.In<t>;
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
89
|
+
/**
|
|
90
|
+
* #### internal JSON representation
|
|
91
|
+
*/
|
|
92
|
+
json: JsonStructure;
|
|
93
|
+
/**
|
|
94
|
+
* alias of {@link json} for `JSON.stringify` compatibility
|
|
95
|
+
*/
|
|
96
|
+
toJSON(): JsonStructure;
|
|
97
|
+
/**
|
|
98
|
+
* #### generate a JSON Schema
|
|
99
|
+
*
|
|
100
|
+
* @throws {JsonSchema.UnjsonifiableError} if this cannot be converted to JSON Schema
|
|
101
|
+
*/
|
|
23
102
|
toJsonSchema(): JsonSchema;
|
|
103
|
+
/**
|
|
104
|
+
* #### metadata like custom descriptions and error messages
|
|
105
|
+
*
|
|
106
|
+
* ✅ type {@link https://arktype.io/docs/configuration#custom | can be customized} for your project
|
|
107
|
+
*/
|
|
108
|
+
meta: ArkAmbient.meta;
|
|
109
|
+
/**
|
|
110
|
+
* #### human-readable English description
|
|
111
|
+
*
|
|
112
|
+
* ✅ works best for primitive values
|
|
113
|
+
*
|
|
114
|
+
* @example
|
|
115
|
+
* const n = type("0 < number <= 100")
|
|
116
|
+
* console.log(n.description) // positive and at most 100
|
|
117
|
+
*/
|
|
24
118
|
description: string;
|
|
119
|
+
/**
|
|
120
|
+
* #### syntax string similar to native TypeScript
|
|
121
|
+
*
|
|
122
|
+
* ✅ works well for both primitives and structures
|
|
123
|
+
*
|
|
124
|
+
* @example
|
|
125
|
+
* const loc = type({ coords: ["number", "number"] })
|
|
126
|
+
* console.log(loc.expression) // { coords: [number, number] }
|
|
127
|
+
*/
|
|
25
128
|
expression: string;
|
|
26
|
-
|
|
27
|
-
|
|
129
|
+
/**
|
|
130
|
+
* #### validate and return transformed data or throw
|
|
131
|
+
*
|
|
132
|
+
* ✅ sugar to avoid checking for {@link type.errors} if they are unrecoverable
|
|
133
|
+
*
|
|
134
|
+
* @example
|
|
135
|
+
* const criticalPayload = type({
|
|
136
|
+
* superImportantValue: "string"
|
|
137
|
+
* })
|
|
138
|
+
* // throws AggregateError: superImportantValue must be a string (was missing)
|
|
139
|
+
* const data = criticalPayload.assert({ irrelevantValue: "whoops" })
|
|
140
|
+
* console.log(data.superImportantValue) // valid output can be accessed directly
|
|
141
|
+
*
|
|
142
|
+
* @throws {AggregateError}
|
|
143
|
+
*/
|
|
28
144
|
assert(data: unknown): this["infer"];
|
|
145
|
+
/**
|
|
146
|
+
* #### check input without applying morphs
|
|
147
|
+
*
|
|
148
|
+
* ✅ good for stuff like filtering that doesn't benefit from detailed errors
|
|
149
|
+
*
|
|
150
|
+
* @example
|
|
151
|
+
* const numeric = type("number | bigint")
|
|
152
|
+
* // [0, 2n]
|
|
153
|
+
* const numerics = [0, "one", 2n].filter(numeric.allows)
|
|
154
|
+
*/
|
|
29
155
|
allows(data: unknown): data is this["inferIn"];
|
|
30
|
-
|
|
156
|
+
/**
|
|
157
|
+
* #### add metadata to shallow references
|
|
158
|
+
*
|
|
159
|
+
* ⚠️ does not affect error messages within properties of an object
|
|
160
|
+
*
|
|
161
|
+
* @example
|
|
162
|
+
* const notOdd = type("number % 2").configure({ description: "not odd" })
|
|
163
|
+
* // all constraints at the root are affected
|
|
164
|
+
* const odd = notOdd(3) // must be not odd (was 3)
|
|
165
|
+
* const nonNumber = notOdd("two") // must be not odd (was "two")
|
|
166
|
+
*
|
|
167
|
+
* const notOddBox = type({
|
|
168
|
+
* // we should have referenced notOdd or added meta here
|
|
169
|
+
* notOdd: "number % 2",
|
|
170
|
+
* // but instead chained from the root object
|
|
171
|
+
* }).configure({ description: "not odd" })
|
|
172
|
+
* // error message at path notOdd is not affected
|
|
173
|
+
* const oddProp = notOddBox({ notOdd: 3 }) // notOdd must be even (was 3)
|
|
174
|
+
* // error message at root is affected, leading to a misleading description
|
|
175
|
+
* const nonObject = notOddBox(null) // must be not odd (was null)
|
|
176
|
+
*/
|
|
31
177
|
configure(meta: MetaSchema): this;
|
|
178
|
+
/**
|
|
179
|
+
* #### add description to shallow references
|
|
180
|
+
*
|
|
181
|
+
* 🔗 equivalent to `.configure({ description })` (see {@link configure})
|
|
182
|
+
* ⚠️ does not affect error messages within properties of an object
|
|
183
|
+
*
|
|
184
|
+
* @example
|
|
185
|
+
* const aToZ = type(/^a.*z$/).describe("a string like 'a...z'")
|
|
186
|
+
* const good = aToZ("alcatraz") // "alcatraz"
|
|
187
|
+
* // ArkErrors: must be a string like 'a...z' (was "albatross")
|
|
188
|
+
* const badPattern = aToZ("albatross")
|
|
189
|
+
*/
|
|
32
190
|
describe(description: string): this;
|
|
191
|
+
/**
|
|
192
|
+
* #### apply undeclared key behavior
|
|
193
|
+
*
|
|
194
|
+
* - `"ignore"` (default) - allow and preserve extra properties
|
|
195
|
+
* - `"reject"` - disallow extra properties
|
|
196
|
+
* - `"delete"` - clone and remove extra properties from output
|
|
197
|
+
*/
|
|
33
198
|
onUndeclaredKey(behavior: UndeclaredKeyBehavior): this;
|
|
199
|
+
/**
|
|
200
|
+
* #### deeply apply undeclared key behavior
|
|
201
|
+
*
|
|
202
|
+
* - `"ignore"` (default) - allow and preserve extra properties
|
|
203
|
+
* - `"reject"` - disallow extra properties
|
|
204
|
+
* - `"delete"` - clone and remove extra properties from output
|
|
205
|
+
*/
|
|
34
206
|
onDeepUndeclaredKey(behavior: UndeclaredKeyBehavior): this;
|
|
207
|
+
/**
|
|
208
|
+
* #### alias for {@link assert} with typed input
|
|
209
|
+
*
|
|
210
|
+
* @example
|
|
211
|
+
* const t = type({ foo: "string" });
|
|
212
|
+
* // TypeScript: foo must be a string (was 5)
|
|
213
|
+
* const data = t.from({ foo: 5 });
|
|
214
|
+
*/
|
|
35
215
|
from(literal: this["inferIn"]): this["infer"];
|
|
36
|
-
|
|
37
|
-
|
|
216
|
+
/**
|
|
217
|
+
* #### deeply extract inputs
|
|
218
|
+
*
|
|
219
|
+
* ✅ will never include morphs
|
|
220
|
+
* ✅ good for generating JSON Schema or other non-transforming formats
|
|
221
|
+
*
|
|
222
|
+
* @example
|
|
223
|
+
* const createUser = type({
|
|
224
|
+
* age: "string.numeric.parse"
|
|
225
|
+
* })
|
|
226
|
+
* // { age: 25 } (age parsed to a number)
|
|
227
|
+
* const out = createUser({ age: "25" })
|
|
228
|
+
* // { age: "25" } (age is still a string)
|
|
229
|
+
* const inOut = createUser.in({ age: "25" })
|
|
230
|
+
*/
|
|
231
|
+
get in(): instantiateType<this["inferIn"], $>;
|
|
232
|
+
/**
|
|
233
|
+
* #### deeply extract outputs
|
|
234
|
+
*
|
|
235
|
+
* ✅ will never include morphs
|
|
236
|
+
* ⚠️ if your type includes morphs, their output will likely be unknown unless they
|
|
237
|
+
* were defined with an explicit output validator via `.to(outputDef)` or `.pipe(morph, outputType)`
|
|
238
|
+
*
|
|
239
|
+
* @example
|
|
240
|
+
* const userMorph = type("string[]").pipe(a => a.join(","))
|
|
241
|
+
*
|
|
242
|
+
* const t = type({
|
|
243
|
+
* // all keywords have introspectable output
|
|
244
|
+
* keyword: "string.numeric.parse",
|
|
245
|
+
* // TypeScript knows this returns a boolean, but we can't introspect that at runtime
|
|
246
|
+
* unvalidated: userMorph,
|
|
247
|
+
* // if needed, it can be made introspectable with an output validator
|
|
248
|
+
* validated: userMorph.to("string")
|
|
249
|
+
* })
|
|
250
|
+
*
|
|
251
|
+
* // Type<{ keyword: number; unvalidated: unknown; validated: string }>
|
|
252
|
+
* const baseOut = base.out
|
|
253
|
+
*/
|
|
38
254
|
get out(): instantiateType<this["inferIntrospectableOut"], $>;
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
255
|
+
/**
|
|
256
|
+
* #### cast the way this is inferred
|
|
257
|
+
*
|
|
258
|
+
* 🥸 inference-only function that does nothing runtime
|
|
259
|
+
*
|
|
260
|
+
* @example
|
|
261
|
+
* // Type<`LEEEEEEEE${string}ROY`>
|
|
262
|
+
* const leeroy = type(/^LE{8,}ROY$/).as<`LEEEEEEEE${string}ROY`>()
|
|
263
|
+
*/
|
|
264
|
+
as<castTo = unset>(...args: validateChainedAsArgs<castTo>): instantiateType<castTo, $>;
|
|
265
|
+
/**
|
|
266
|
+
* #### add a compile-time brand to output
|
|
267
|
+
*
|
|
268
|
+
* 🥸 inference-only function that does nothing runtime
|
|
269
|
+
*
|
|
270
|
+
* @example
|
|
271
|
+
* const palindrome = type("string")
|
|
272
|
+
* .narrow(s => s === [...s].reverse().join(""))
|
|
273
|
+
* .brand("palindrome")
|
|
274
|
+
* // Brand<string, "palindrome">
|
|
275
|
+
* const out = palindrome.assert("racecar")
|
|
276
|
+
*/
|
|
277
|
+
brand<const name extends string, r = type.brand<t, name>>(name: name): instantiateType<r, $>;
|
|
278
|
+
/**
|
|
279
|
+
* #### intersect the parsed Type, throwing if the result is unsatisfiable
|
|
280
|
+
*
|
|
281
|
+
* @example
|
|
282
|
+
* // Type<{ foo: number; bar: string }>
|
|
283
|
+
* const t = type({ foo: "number" }).and({ bar: "string" })
|
|
284
|
+
* // ParseError: Intersection at foo of number and string results in an unsatisfiable type
|
|
285
|
+
* const bad = type({ foo: "number" }).and({ foo: "string" })
|
|
286
|
+
*/
|
|
287
|
+
and<const def, r = type.infer<def, $>>(def: type.validate<def, $>): instantiateType<inferIntersection<t, r>, $>;
|
|
288
|
+
/**
|
|
289
|
+
* #### union with the parsed Type
|
|
290
|
+
*
|
|
291
|
+
* ⚠️ a union that could apply different morphs to the same data is a ParseError ([docs](https://arktype.io/docs/expressions/union-morphs))
|
|
292
|
+
*
|
|
293
|
+
* @example
|
|
294
|
+
* // Type<string | { box: string }>
|
|
295
|
+
* const t = type("string").or({ box: "string" })
|
|
296
|
+
*/
|
|
297
|
+
or<const def, r = type.infer<def, $>>(def: type.validate<def, $>): instantiateType<t | r, $>;
|
|
298
|
+
/**
|
|
299
|
+
* #### an array of this
|
|
300
|
+
*
|
|
301
|
+
* @example
|
|
302
|
+
* // Type<{ rebmun: number }[]>
|
|
303
|
+
* const t = type({ rebmun: "number" }).array();
|
|
304
|
+
*/
|
|
42
305
|
array(): ArrayType<t[], $>;
|
|
43
|
-
|
|
306
|
+
/**
|
|
307
|
+
* #### {@link https://arktype.io/docs/objects#properties-optional | optional definition}
|
|
308
|
+
*
|
|
309
|
+
* ⚠️ unlike most other methods, this creates a definition rather than a Type (read why)
|
|
310
|
+
*
|
|
311
|
+
* @example
|
|
312
|
+
* const prop = type({ foo: "number" })
|
|
313
|
+
* // Type<{ bar?: { foo: number } }>
|
|
314
|
+
* const obj = type({ bar: prop.optional() })
|
|
315
|
+
*/
|
|
316
|
+
optional(): [this, "?"];
|
|
317
|
+
/**
|
|
318
|
+
* #### {@link https://arktype.io/docs/objects#properties-defaultable | defaultable definition}
|
|
319
|
+
*
|
|
320
|
+
* ✅ object defaults can be returned from a function
|
|
321
|
+
* ⚠️ throws if the default value is not allowed
|
|
322
|
+
* ⚠️ unlike most other methods, this creates a definition rather than a Type (read why)
|
|
323
|
+
*
|
|
324
|
+
* @example
|
|
325
|
+
* // Type<{ count: Default<number, 0> }>
|
|
326
|
+
* const state = type({ count: type.number.default(0) })
|
|
327
|
+
* const prop = type({ nested: "boolean" })
|
|
328
|
+
* const forObj = type({
|
|
329
|
+
* key: nested.default(() => ({ nested: false }))
|
|
330
|
+
* })
|
|
331
|
+
*/
|
|
332
|
+
default<const value extends defaultFor<this["inferIn"]>>(value: value): [this, "=", value];
|
|
333
|
+
/**
|
|
334
|
+
* #### apply a predicate function to input
|
|
335
|
+
*
|
|
336
|
+
* ⚠️ the behavior of {@link narrow}, this method's output counterpart, is usually more desirable
|
|
337
|
+
* ✅ most useful for morphs with input types that are re-used externally
|
|
338
|
+
* 🥸 {@link https://www.typescriptlang.org/docs/handbook/2/narrowing.html#using-type-predicates | Type predicates} can be used as casts
|
|
339
|
+
*
|
|
340
|
+
* @example
|
|
341
|
+
* const stringifyUser = type({ name: "string" }).pipe(user => JSON.stringify(user))
|
|
342
|
+
* const stringifySafe = stringifyUser.filter(user => user.name !== "Bobby Tables")
|
|
343
|
+
* // Type<(In: `${string}Z`) => To<Date>>
|
|
344
|
+
* const withPredicate = type("string.date.parse").filter((s): s is `${string}Z` =>
|
|
345
|
+
* s.endsWith("Z")
|
|
346
|
+
* )
|
|
347
|
+
*/
|
|
348
|
+
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"], narrowed>): instantiateType<r, $>;
|
|
349
|
+
/**
|
|
350
|
+
* #### apply a predicate function to output
|
|
351
|
+
*
|
|
352
|
+
* ✅ go-to fallback for validation not composable via built-in types and operators
|
|
353
|
+
* ✅ runs after all other validators and morphs, if present
|
|
354
|
+
* 🥸 {@link https://www.typescriptlang.org/docs/handbook/2/narrowing.html#using-type-predicates | Type predicates} can be used as casts
|
|
355
|
+
*
|
|
356
|
+
* @example
|
|
357
|
+
* const palindrome = type("string").narrow(s => s === [...s].reverse().join(""))
|
|
358
|
+
*
|
|
359
|
+
* const palindromicEmail = type("string.date.parse").narrow((date, ctx) =>
|
|
360
|
+
* date.getFullYear() === 2025 || ctx.mustBe("the current year")
|
|
361
|
+
* )
|
|
362
|
+
* // Type<`${string}.tsx`>
|
|
363
|
+
* const withPredicate = type("string").narrow((s): s is `${string}.tsx` => /\.tsx?$/.test(s))
|
|
364
|
+
*/
|
|
365
|
+
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, $>;
|
|
366
|
+
/**
|
|
367
|
+
* Morph this `Type` through a chain of morphs.
|
|
368
|
+
* @example const dedupe = type('string[]').pipe(a => Array.from(new Set(a)))
|
|
369
|
+
* @example type({codes: 'string.numeric[]'}).pipe(obj => obj.codes).to('string.numeric.parse[]')
|
|
370
|
+
*/
|
|
371
|
+
pipe: ChainedPipe<t, $>;
|
|
372
|
+
/**
|
|
373
|
+
* #### intersect the parsed Type, returning an introspectable {@link Disjoint} if the result is unsatisfiable
|
|
374
|
+
*
|
|
375
|
+
* @example
|
|
376
|
+
* // Type<{ foo: number; bar: string }>
|
|
377
|
+
* const t = type({ foo: "number" }).intersect({ bar: "string" })
|
|
378
|
+
* const bad = type("number > 10").intersect("number < 5")
|
|
379
|
+
* // logs "Intersection of > 10 and < 5 results in an unsatisfiable type"
|
|
380
|
+
* if (bad instanceof Disjoint) console.log(`${bad.summary}`)
|
|
381
|
+
*/
|
|
382
|
+
intersect<const def, r = type.infer<def, $>>(def: type.validate<def, $>): instantiateType<inferIntersection<t, r>, $> | Disjoint;
|
|
383
|
+
/**
|
|
384
|
+
* #### check if the parsed Type's constraints are identical
|
|
385
|
+
*
|
|
386
|
+
* ✅ equal types have identical input and output constraints and transforms
|
|
387
|
+
* ✅ ignores associated {@link meta}, which does not affect the set of allowed values
|
|
388
|
+
*
|
|
389
|
+
* @example
|
|
390
|
+
* const divisibleBy6 = type.number.divisibleBy(6).moreThan(0)
|
|
391
|
+
* // false (left side must also be positive)
|
|
392
|
+
* divisibleBy6.equals("number % 6")
|
|
393
|
+
* // false (right side has an additional <100 constraint)
|
|
394
|
+
* console.log(divisibleBy6.equals("0 < (number % 6) < 100"))
|
|
395
|
+
* const thirdTry = type("(number % 2) > 0").divisibleBy(3)
|
|
396
|
+
* // true (types are normalized and reduced)
|
|
397
|
+
* console.log(divisibleBy6.equals(thirdTry))
|
|
398
|
+
*/
|
|
44
399
|
equals<const def>(def: type.validate<def, $>): boolean;
|
|
45
|
-
|
|
400
|
+
/**
|
|
401
|
+
* #### narrow this based on an {@link equals} check
|
|
402
|
+
*
|
|
403
|
+
* ✅ ignores associated {@link meta}, which does not affect the set of allowed values
|
|
404
|
+
*
|
|
405
|
+
* @example
|
|
406
|
+
* const n = type.raw(`${Math.random()}`)
|
|
407
|
+
* // Type<0.5> | undefined
|
|
408
|
+
* const ez = n.ifEquals("0.5")
|
|
409
|
+
*/
|
|
410
|
+
ifEquals<const def, r = type.infer<def, $>>(def: type.validate<def, $>): instantiateType<r, $> | undefined;
|
|
411
|
+
/**
|
|
412
|
+
* #### check if this is a subtype of the parsed Type
|
|
413
|
+
*
|
|
414
|
+
* ✅ a subtype must include all constraints from the base type
|
|
415
|
+
* ✅ unlike {@link equals}, additional constraints may be present
|
|
416
|
+
* ✅ ignores associated {@link meta}, which does not affect the set of allowed values
|
|
417
|
+
*
|
|
418
|
+
* @example
|
|
419
|
+
* type.string.extends("unknown") // true
|
|
420
|
+
* type.string.extends(/^a.*z$/) // false
|
|
421
|
+
*/
|
|
46
422
|
extends<const def>(other: type.validate<def, $>): boolean;
|
|
47
|
-
|
|
423
|
+
/**
|
|
424
|
+
* #### narrow this based on an {@link extends} check
|
|
425
|
+
*
|
|
426
|
+
* ✅ ignores associated {@link meta}, which does not affect the set of allowed values
|
|
427
|
+
*
|
|
428
|
+
* @example
|
|
429
|
+
* const n = type(Math.random() > 0.5 ? "true" : "0") // Type<0 | true>
|
|
430
|
+
* const ez = n.ifExtends("boolean") // Type<true> | undefined
|
|
431
|
+
*/
|
|
432
|
+
ifExtends<const def, r = type.infer<def, $>>(other: type.validate<def, $>): instantiateType<r, $> | undefined;
|
|
433
|
+
/**
|
|
434
|
+
* #### check if a value could satisfy this and the parsed Type
|
|
435
|
+
*
|
|
436
|
+
* ⚠️ will return true unless a {@link Disjoint} can be proven
|
|
437
|
+
*
|
|
438
|
+
* @example
|
|
439
|
+
* type.string.overlaps("string | number") // true (e.g. "foo")
|
|
440
|
+
* type("string | number").overlaps("1") // true (1)
|
|
441
|
+
* type("number > 0").overlaps("number < 0") // false (no values exist)
|
|
442
|
+
*
|
|
443
|
+
* const noAt = type("string").narrow(s => !s.includes("@"))
|
|
444
|
+
* noAt.overlaps("string.email") // true (no values exist, but not provable)
|
|
445
|
+
*/
|
|
48
446
|
overlaps<const def>(r: type.validate<def, $>): boolean;
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
447
|
+
/**
|
|
448
|
+
* #### extract branches {@link extend}ing the parsed Type
|
|
449
|
+
*
|
|
450
|
+
* @example
|
|
451
|
+
* // Type<true | 0 | 2>
|
|
452
|
+
* const t = type("boolean | 0 | 'one' | 2 | bigint").extract("number | 0n | true")
|
|
453
|
+
*/
|
|
454
|
+
extract<const def, r = type.infer<def, $>>(r: type.validate<def, $>): instantiateType<Extract<t, r>, $>;
|
|
455
|
+
/**
|
|
456
|
+
* #### exclude branches {@link extend}ing the parsed Type
|
|
457
|
+
*
|
|
458
|
+
* @example
|
|
459
|
+
*
|
|
460
|
+
* // Type<false | 'one' | bigint>
|
|
461
|
+
* const t = type("boolean | 0 | 'one' | 2 | bigint").exclude("number | 0n | true")
|
|
462
|
+
*/
|
|
463
|
+
exclude<const def, r = type.infer<def, $>>(r: type.validate<def, $>): instantiateType<Exclude<t, r>, $>;
|
|
464
|
+
/**
|
|
465
|
+
* @experimental
|
|
466
|
+
* Map and optionally reduce branches of a union. Types that are not unions
|
|
467
|
+
* are treated as a single branch.
|
|
468
|
+
*
|
|
469
|
+
* @param mapBranch - the mapping function, accepting a branch Type
|
|
470
|
+
* Returning another `Type` is common, but any value can be returned and
|
|
471
|
+
* inferred as part of the output.
|
|
472
|
+
*
|
|
473
|
+
* @param [reduceMapped] - an operation to perform on the mapped branches
|
|
474
|
+
* Can be used to e.g. merge an array of returned Types representing
|
|
475
|
+
* branches back to a single union.
|
|
476
|
+
*/
|
|
477
|
+
distribute<mapOut, reduceOut = mapOut[]>(mapBranch: (branch: Type, i: number, branches: array<Type>) => mapOut, reduceMapped?: (mappedBranches: mapOut[]) => reduceOut): reduceOut;
|
|
478
|
+
/** The Type's [StandardSchema](https://github.com/standard-schema/standard-schema) properties */
|
|
479
|
+
"~standard": StandardSchemaV1.ArkTypeProps<this["inferIn"], this["inferOut"]>;
|
|
54
480
|
/** @deprecated */
|
|
55
481
|
apply: Function["apply"];
|
|
56
482
|
/** @deprecated */
|
|
@@ -71,15 +497,15 @@ interface Type<out t = unknown, $ = {}> extends Callable<(data: unknown) => dist
|
|
|
71
497
|
Symbol: never;
|
|
72
498
|
}
|
|
73
499
|
interface ChainedPipeSignature<t, $> {
|
|
74
|
-
<a extends Morph<distill.Out<t>>, r = instantiateType<inferPipes<t, [a]>, $>>(a: a): NoInfer<r
|
|
75
|
-
<a extends Morph<distill.Out<t>>, b extends Morph<inferMorphOut<a>>, r = instantiateType<inferPipes<t, [a, b]>, $>>(a: a, b: b): NoInfer<r
|
|
76
|
-
<a extends Morph<distill.Out<t>>, b extends Morph<inferMorphOut<a>>, c extends Morph<inferMorphOut<b>>, r = instantiateType<inferPipes<t, [a, b, c]>, $>>(a: a, b: b, c: c): NoInfer<r
|
|
77
|
-
<a extends Morph<distill.Out<t>>, b extends Morph<inferMorphOut<a>>, c extends Morph<inferMorphOut<b>>, d extends Morph<inferMorphOut<c>>, r = instantiateType<inferPipes<t, [a, b, c, d]>, $>>(a: a, b: b, c: c, d: d): NoInfer<r
|
|
78
|
-
<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>>, r = instantiateType<inferPipes<t, [a, b, c, d, e]>, $>>(a: a, b: b, c: c, d: d, e: e): NoInfer<r
|
|
79
|
-
<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
|
|
80
|
-
<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
|
|
500
|
+
<a extends Morph<distill.Out<t>>, r = instantiateType<inferPipes<t, [a]>, $>>(a: a): NoInfer<r> extends infer result ? result : never;
|
|
501
|
+
<a extends Morph<distill.Out<t>>, b extends Morph<inferMorphOut<a>>, r = instantiateType<inferPipes<t, [a, b]>, $>>(a: a, b: b): NoInfer<r> extends infer result ? result : never;
|
|
502
|
+
<a extends Morph<distill.Out<t>>, b extends Morph<inferMorphOut<a>>, c extends Morph<inferMorphOut<b>>, r = instantiateType<inferPipes<t, [a, b, c]>, $>>(a: a, b: b, c: c): NoInfer<r> extends infer result ? result : never;
|
|
503
|
+
<a extends Morph<distill.Out<t>>, b extends Morph<inferMorphOut<a>>, c extends Morph<inferMorphOut<b>>, d extends Morph<inferMorphOut<c>>, r = instantiateType<inferPipes<t, [a, b, c, d]>, $>>(a: a, b: b, c: c, d: d): NoInfer<r> extends infer result ? result : never;
|
|
504
|
+
<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>>, r = instantiateType<inferPipes<t, [a, b, c, d, e]>, $>>(a: a, b: b, c: c, d: d, e: e): NoInfer<r> extends infer result ? result : never;
|
|
505
|
+
<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;
|
|
506
|
+
<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;
|
|
81
507
|
}
|
|
82
|
-
export interface
|
|
508
|
+
export interface ChainedPipe<t, $> extends ChainedPipeSignature<t, $> {
|
|
83
509
|
try: ChainedPipeSignature<t, $>;
|
|
84
510
|
}
|
|
85
511
|
type validateChainedAsArgs<t> = [
|
package/out/methods/date.d.ts
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
import type { ExclusiveDateRangeSchema,
|
|
2
|
-
import type { applyConstraintSchema } from "../keywords/inference.ts";
|
|
1
|
+
import type { ExclusiveDateRangeSchema, InclusiveDateRangeSchema } from "@ark/schema";
|
|
3
2
|
import type { ObjectType } from "./object.ts";
|
|
4
3
|
/** @ts-ignore cast variance */
|
|
5
4
|
interface Type<out t extends globalThis.Date = globalThis.Date, $ = {}> extends ObjectType<t, $> {
|
|
6
|
-
atOrAfter
|
|
7
|
-
atOrBefore
|
|
8
|
-
laterThan
|
|
9
|
-
earlierThan
|
|
5
|
+
atOrAfter(schema: InclusiveDateRangeSchema): this;
|
|
6
|
+
atOrBefore(schema: InclusiveDateRangeSchema): this;
|
|
7
|
+
laterThan(schema: ExclusiveDateRangeSchema): this;
|
|
8
|
+
earlierThan(schema: ExclusiveDateRangeSchema): this;
|
|
10
9
|
}
|
|
11
10
|
export type { Type as DateType };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { anyOrNever, array } from "@ark/util";
|
|
2
|
-
import type { InferredMorph } from "../
|
|
2
|
+
import type { InferredMorph } from "../attributes.ts";
|
|
3
3
|
import type { ArrayType } from "./array.ts";
|
|
4
4
|
import type { BaseType } from "./base.ts";
|
|
5
5
|
import type { DateType } from "./date.ts";
|
|
@@ -7,11 +7,11 @@ import type { MorphType } from "./morph.ts";
|
|
|
7
7
|
import type { NumberType } from "./number.ts";
|
|
8
8
|
import type { ObjectType } from "./object.ts";
|
|
9
9
|
import type { StringType } from "./string.ts";
|
|
10
|
-
|
|
11
|
-
export type
|
|
10
|
+
export type instantiateType<t, $> = NoInfer<_instantiateType<t, $>> extends infer r ? r : never;
|
|
11
|
+
export type _instantiateType<t, $> = [
|
|
12
12
|
Extract<t, InferredMorph>
|
|
13
13
|
] extends [anyOrNever] ? [
|
|
14
14
|
t
|
|
15
15
|
] extends [anyOrNever] ? BaseType<t, $> : [t] extends [string] ? StringType<t, $> : [t] extends [number] ? NumberType<t, $> : [t] extends [object] ? [
|
|
16
16
|
t
|
|
17
|
-
] extends [array] ? ArrayType<t, $> : [t] extends [Date] ? DateType<t, $> : ObjectType<t, $> :
|
|
17
|
+
] extends [array] ? ArrayType<t, $> : [t] extends [Date] ? DateType<t, $> : ObjectType<t, $> : BaseType<t, $> : MorphType<t, $>;
|
package/out/methods/morph.d.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type { applyConstraintSchema, inferPipe, Out, To } from "../keywords/inference.ts";
|
|
1
|
+
import type { inferPipe } from "../attributes.ts";
|
|
3
2
|
import type { type } from "../keywords/keywords.ts";
|
|
4
3
|
import type { BaseType } from "./base.ts";
|
|
5
4
|
/** @ts-ignore cast variance */
|
|
6
5
|
interface Type<out t = unknown, $ = {}> extends BaseType<t, $> {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
6
|
+
/**
|
|
7
|
+
* Append extra validation shape on the pipe output
|
|
8
|
+
* @example type({codes: 'string.numeric[]'}).pipe(obj => obj.codes).to('string.numeric.parse[]')
|
|
9
|
+
*/
|
|
10
|
+
to<const def, r = type.infer<def, $>>(def: type.validate<def, $>): Type<inferPipe<t, r>, $>;
|
|
11
11
|
}
|
|
12
12
|
export type { Type as MorphType };
|
package/out/methods/number.d.ts
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
|
-
import type { Divisor, ExclusiveNumericRangeSchema,
|
|
2
|
-
import type {
|
|
3
|
-
import type { ValidatorType } from "./validator.ts";
|
|
1
|
+
import type { Divisor, ExclusiveNumericRangeSchema, InclusiveNumericRangeSchema } from "@ark/schema";
|
|
2
|
+
import type { BaseType } from "./base.ts";
|
|
4
3
|
/** @ts-ignore cast variance */
|
|
5
|
-
interface Type<out t extends number = number, $ = {}> extends
|
|
6
|
-
divisibleBy
|
|
7
|
-
atLeast
|
|
8
|
-
atMost
|
|
9
|
-
moreThan
|
|
10
|
-
lessThan
|
|
4
|
+
interface Type<out t extends number = number, $ = {}> extends BaseType<t, $> {
|
|
5
|
+
divisibleBy(schema: Divisor.Schema): this;
|
|
6
|
+
atLeast(schema: InclusiveNumericRangeSchema): this;
|
|
7
|
+
atMost(schema: InclusiveNumericRangeSchema): this;
|
|
8
|
+
moreThan(schema: ExclusiveNumericRangeSchema): this;
|
|
9
|
+
lessThan(schema: ExclusiveNumericRangeSchema): this;
|
|
11
10
|
}
|
|
12
11
|
export type { Type as NumberType };
|