arktype 2.1.15 → 2.1.17
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/LICENSE +1 -1
- package/out/attributes.d.ts +1 -1
- package/out/index.d.ts +6 -4
- package/out/index.js +2 -2
- package/out/keywords/builtins.d.ts +1 -0
- package/out/keywords/builtins.js +1 -0
- package/out/keywords/constructors.d.ts +2 -1
- package/out/keywords/keywords.d.ts +3 -3
- package/out/keywords/ts.d.ts +6 -0
- package/out/keywords/ts.js +7 -1
- package/out/match.d.ts +14 -4
- package/out/methods/base.d.ts +67 -67
- package/out/type.d.ts +8 -8
- package/package.json +3 -3
package/LICENSE
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
Copyright
|
|
1
|
+
Copyright 2025 ArkType
|
|
2
2
|
|
|
3
3
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
4
|
|
package/out/attributes.d.ts
CHANGED
|
@@ -47,7 +47,7 @@ type distillArrayFromPostfix<t extends array, endpoint extends distill.Endpoint,
|
|
|
47
47
|
]> : [...{
|
|
48
48
|
[i in keyof t]: _distill<t[i], endpoint, seen>;
|
|
49
49
|
}, ...postfix];
|
|
50
|
-
type BuiltinTerminalObjectKind = Exclude<
|
|
50
|
+
type BuiltinTerminalObjectKind = Exclude<arkPrototypes.NonDegenerateName, "Array" | "Function">;
|
|
51
51
|
/** Objects we don't want to expand during inference like Date or Promise */
|
|
52
52
|
type TerminallyInferredObject = arkPrototypes.instanceOf<BuiltinTerminalObjectKind> | ArkEnv.prototypes;
|
|
53
53
|
export type inferPredicate<t, predicate> = predicate extends (data: any, ...args: any[]) => data is infer narrowed ? narrowed : t;
|
package/out/index.d.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
|
-
export { ArkError, ArkErrors, type ArkSchemaConfig, type ArkSchemaScopeConfig, type JsonSchema } from "@ark/schema";
|
|
2
|
-
export { Hkt, inferred } from "@ark/util";
|
|
3
|
-
export type { distill } from "./attributes.ts";
|
|
1
|
+
export { ArkError, ArkErrors, Traversal, TraversalError, type ArkSchemaConfig, type ArkSchemaScopeConfig, type JsonSchema } from "@ark/schema";
|
|
2
|
+
export { Hkt, inferred, ParseError } from "@ark/util";
|
|
3
|
+
export type { distill, Out } from "./attributes.ts";
|
|
4
4
|
export * from "./config.ts";
|
|
5
5
|
export { Generic } from "./generic.ts";
|
|
6
6
|
export { ark, declare, define, generic, keywords, match, type, type Ark } from "./keywords/keywords.ts";
|
|
7
|
+
export type { BaseType } from "./methods/base.ts";
|
|
7
8
|
export { Module, type BoundModule, type Submodule } from "./module.ts";
|
|
8
|
-
export {
|
|
9
|
+
export type { inferDefinition, validateDefinition } from "./parser/definition.ts";
|
|
10
|
+
export { scope, type bindThis, type Scope } from "./scope.ts";
|
|
9
11
|
export { Type } from "./type.ts";
|
package/out/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
export { ArkError, ArkErrors } from "@ark/schema";
|
|
2
|
-
export { Hkt, inferred } from "@ark/util";
|
|
1
|
+
export { ArkError, ArkErrors, Traversal, TraversalError } from "@ark/schema";
|
|
2
|
+
export { Hkt, inferred, ParseError } from "@ark/util";
|
|
3
3
|
export * from "./config.js";
|
|
4
4
|
export { Generic } from "./generic.js";
|
|
5
5
|
export { ark, declare, define, generic, keywords, match, type } from "./keywords/keywords.js";
|
|
@@ -3,6 +3,7 @@ import { Hkt, type Key } from "@ark/util";
|
|
|
3
3
|
import type { Module, Submodule } from "../module.ts";
|
|
4
4
|
declare class MergeHkt extends Hkt<[base: object, props: object]> {
|
|
5
5
|
body: util.merge<this[0], this[1]>;
|
|
6
|
+
description: string;
|
|
6
7
|
}
|
|
7
8
|
declare const Merge: import("@ark/schema").GenericRoot<readonly [["base", object], ["props", object]], MergeHkt>;
|
|
8
9
|
export declare const arkBuiltins: arkBuiltins;
|
package/out/keywords/builtins.js
CHANGED
|
@@ -2,6 +2,7 @@ import { genericNode, intrinsic } from "@ark/schema";
|
|
|
2
2
|
import { Hkt } from "@ark/util";
|
|
3
3
|
import { Scope } from "../scope.js";
|
|
4
4
|
class MergeHkt extends Hkt {
|
|
5
|
+
description = 'merge an object\'s properties onto another like `Merge(User, { isAdmin: "true" })`';
|
|
5
6
|
}
|
|
6
7
|
const Merge = genericNode(["base", intrinsic.object], ["props", intrinsic.object])(args => args.base.merge(args.props), MergeHkt);
|
|
7
8
|
export const arkBuiltins = Scope.module({
|
|
@@ -25,6 +25,7 @@ export declare namespace arkPrototypes {
|
|
|
25
25
|
type platform = PlatformObjects;
|
|
26
26
|
interface instances extends ecmascript, platform {
|
|
27
27
|
}
|
|
28
|
-
type
|
|
28
|
+
type NonDegenerateName = keyof instances extends infer k ? k extends keyof instances ? {} extends instances[k] ? never : k : never : never;
|
|
29
|
+
type instanceOf<name extends NonDegenerateName = NonDegenerateName> = instances[name];
|
|
29
30
|
}
|
|
30
31
|
export {};
|
|
@@ -44,6 +44,8 @@ export declare namespace type {
|
|
|
44
44
|
[inferred]?: to;
|
|
45
45
|
}
|
|
46
46
|
type errors = ArkErrors;
|
|
47
|
+
type validate<def, $ = {}, args = bindThis<def>> = validateDefinition<def, $, args>;
|
|
48
|
+
type instantiate<def, $ = {}, args = bindThis<def>> = instantiateType<inferDefinition<def, $, args>, $>;
|
|
47
49
|
type infer<def, $ = {}, args = bindThis<def>> = inferDefinition<def, $, args>;
|
|
48
50
|
namespace infer {
|
|
49
51
|
type In<def, $ = {}, args = {}> = distill.In<inferDefinition<def, $, args>>;
|
|
@@ -52,11 +54,9 @@ export declare namespace type {
|
|
|
52
54
|
type Out<def, $ = {}, args = {}> = distill.introspectable.Out<inferDefinition<def, $, args>>;
|
|
53
55
|
}
|
|
54
56
|
}
|
|
55
|
-
type validate<def, $ = {}, args = bindThis<def>> = validateDefinition<def, $, args>;
|
|
56
|
-
type instantiate<def, $ = {}, args = bindThis<def>> = instantiateType<inferDefinition<def, $, args>, $>;
|
|
57
57
|
type brand<t, id> = t extends InferredMorph<infer i, infer o> ? o["introspectable"] extends true ? (In: i) => To<Brand<o["t"], id>> : (In: i) => Out<Brand<o["t"], id>> : Brand<t, id>;
|
|
58
58
|
/** @ts-ignore cast variance */
|
|
59
|
-
interface Any<out t = any> extends BaseType<t,
|
|
59
|
+
interface Any<out t = any, $ = any> extends BaseType<t, $> {
|
|
60
60
|
}
|
|
61
61
|
}
|
|
62
62
|
export type type<t = unknown, $ = {}> = Type<t, $>;
|
package/out/keywords/ts.d.ts
CHANGED
|
@@ -63,26 +63,32 @@ declare class RecordHkt extends Hkt<[Key, unknown]> {
|
|
|
63
63
|
declare const Record: import("@ark/schema").GenericRoot<readonly [["K", Key], ["V", unknown]], RecordHkt>;
|
|
64
64
|
declare class PickHkt extends Hkt<[object, Key]> {
|
|
65
65
|
body: pick<this[0], this[1] & keyof this[0]>;
|
|
66
|
+
description: string;
|
|
66
67
|
}
|
|
67
68
|
declare const Pick: import("@ark/schema").GenericRoot<readonly [["T", object], ["K", Key]], PickHkt>;
|
|
68
69
|
declare class OmitHkt extends Hkt<[object, Key]> {
|
|
69
70
|
body: omit<this[0], this[1] & keyof this[0]>;
|
|
71
|
+
description: string;
|
|
70
72
|
}
|
|
71
73
|
declare const Omit: import("@ark/schema").GenericRoot<readonly [["T", object], ["K", Key]], OmitHkt>;
|
|
72
74
|
declare class PartialHkt extends Hkt<[object]> {
|
|
73
75
|
body: show<Partial<this[0]>>;
|
|
76
|
+
description: string;
|
|
74
77
|
}
|
|
75
78
|
declare const Partial: import("@ark/schema").GenericRoot<readonly [["T", object]], PartialHkt>;
|
|
76
79
|
declare class RequiredHkt extends Hkt<[object]> {
|
|
77
80
|
body: show<Required<this[0]>>;
|
|
81
|
+
description: string;
|
|
78
82
|
}
|
|
79
83
|
declare const Required: import("@ark/schema").GenericRoot<readonly [["T", object]], RequiredHkt>;
|
|
80
84
|
declare class ExcludeHkt extends Hkt<[unknown, unknown]> {
|
|
81
85
|
body: Exclude<this[0], this[1]>;
|
|
86
|
+
description: string;
|
|
82
87
|
}
|
|
83
88
|
declare const Exclude: import("@ark/schema").GenericRoot<readonly [["T", unknown], ["U", unknown]], ExcludeHkt>;
|
|
84
89
|
declare class ExtractHkt extends Hkt<[unknown, unknown]> {
|
|
85
90
|
body: Extract<this[0], this[1]>;
|
|
91
|
+
description: string;
|
|
86
92
|
}
|
|
87
93
|
declare const Extract: import("@ark/schema").GenericRoot<readonly [["T", unknown], ["U", unknown]], ExtractHkt>;
|
|
88
94
|
export declare const arkTsGenerics: arkTsGenerics.module;
|
package/out/keywords/ts.js
CHANGED
|
@@ -38,7 +38,7 @@ export const object = Scope.module({
|
|
|
38
38
|
name: "object"
|
|
39
39
|
});
|
|
40
40
|
class RecordHkt extends Hkt {
|
|
41
|
-
description =
|
|
41
|
+
description = 'instantiate an object from an index signature and corresponding value type like `Record("string", "number")`';
|
|
42
42
|
}
|
|
43
43
|
const Record = genericNode(["K", intrinsic.key], "V")(args => ({
|
|
44
44
|
domain: "object",
|
|
@@ -48,21 +48,27 @@ const Record = genericNode(["K", intrinsic.key], "V")(args => ({
|
|
|
48
48
|
}
|
|
49
49
|
}), RecordHkt);
|
|
50
50
|
class PickHkt extends Hkt {
|
|
51
|
+
description = 'pick a set of properties from an object like `Pick(User, "name | age")`';
|
|
51
52
|
}
|
|
52
53
|
const Pick = genericNode(["T", intrinsic.object], ["K", intrinsic.key])(args => args.T.pick(args.K), PickHkt);
|
|
53
54
|
class OmitHkt extends Hkt {
|
|
55
|
+
description = 'omit a set of properties from an object like `Omit(User, "age")`';
|
|
54
56
|
}
|
|
55
57
|
const Omit = genericNode(["T", intrinsic.object], ["K", intrinsic.key])(args => args.T.omit(args.K), OmitHkt);
|
|
56
58
|
class PartialHkt extends Hkt {
|
|
59
|
+
description = "make all named properties of an object optional like `Partial(User)`";
|
|
57
60
|
}
|
|
58
61
|
const Partial = genericNode(["T", intrinsic.object])(args => args.T.partial(), PartialHkt);
|
|
59
62
|
class RequiredHkt extends Hkt {
|
|
63
|
+
description = "make all named properties of an object required like `Required(User)`";
|
|
60
64
|
}
|
|
61
65
|
const Required = genericNode(["T", intrinsic.object])(args => args.T.required(), RequiredHkt);
|
|
62
66
|
class ExcludeHkt extends Hkt {
|
|
67
|
+
description = 'exclude branches of a union like `Exclude("boolean", "true")`';
|
|
63
68
|
}
|
|
64
69
|
const Exclude = genericNode("T", "U")(args => args.T.exclude(args.U), ExcludeHkt);
|
|
65
70
|
class ExtractHkt extends Hkt {
|
|
71
|
+
description = 'extract branches of a union like `Extract("0 | false | 1", "number")`';
|
|
66
72
|
}
|
|
67
73
|
const Extract = genericNode("T", "U")(args => args.T.extract(args.U), ExtractHkt);
|
|
68
74
|
export const arkTsGenerics = Scope.module({
|
package/out/match.d.ts
CHANGED
|
@@ -73,16 +73,26 @@ type maybeLiftToKey<t, ctx extends MatchParserContext> = ctx["key"] extends Prop
|
|
|
73
73
|
type _finalizeCaseArg<t, ctx extends MatchParserContext, endpoint extends "in" | "out"> = [
|
|
74
74
|
distill<t, "in">,
|
|
75
75
|
distill<t, endpoint>
|
|
76
|
-
] extends [infer i, infer result] ?
|
|
76
|
+
] extends [infer i, infer result] ? [
|
|
77
|
+
i
|
|
78
|
+
] extends [ctx["input"]] ? result : Extract<ctx["input"], i> extends never ? result : Extract<ctx["input"], result> : never;
|
|
77
79
|
type CaseParser<ctx extends MatchParserContext> = <const def, ret>(def: type.validate<def, ctx["$"]>, resolve: (In: inferCaseArg<def, ctx, "out">) => ret) => ChainableMatchParser<addCasesToContext<ctx, [(In: inferCaseArg<def, ctx, "in">) => ret]>>;
|
|
78
80
|
type validateKey<key extends Key, ctx extends MatchParserContext> = ctx["key"] extends Key ? ErrorMessage<doubleAtMessage> : ctx["cases"]["length"] extends 0 ? keyof ctx["input"] extends never ? key : conform<key, keyof ctx["input"]> : ErrorMessage<chainedAtMessage>;
|
|
79
81
|
interface StringsParser<ctx extends MatchParserContext> {
|
|
80
82
|
<const cases>(def: cases extends validateStringCases<cases, ctx> ? cases : validateStringCases<cases, ctx>): addCasesToParser<cases, ctx, "string">;
|
|
81
83
|
}
|
|
82
|
-
type validateStringCases<cases, ctx extends MatchParserContext> = {
|
|
83
|
-
[k in keyof cases
|
|
84
|
+
type validateStringCases<cases, ctx extends MatchParserContext> = unknown extends ctx["input"] ? {
|
|
85
|
+
[k in keyof cases]?: k extends "default" ? DefaultCase<ctx> : (In: _finalizeCaseArg<maybeLiftToKey<k, ctx>, ctx, "out">) => unknown;
|
|
86
|
+
} & {
|
|
87
|
+
default?: DefaultCase<ctx>;
|
|
88
|
+
} : {
|
|
89
|
+
[k in keyof cases]?: k extends "default" ? DefaultCase<ctx> : k extends stringValue<ctx> ? (In: _finalizeCaseArg<maybeLiftToKey<k, ctx>, ctx, "out">) => unknown : ErrorType<`${k & string} must be a possible string value`>;
|
|
90
|
+
} & {
|
|
91
|
+
[k in stringValue<ctx>]?: unknown;
|
|
92
|
+
} & {
|
|
93
|
+
default?: DefaultCase<ctx>;
|
|
84
94
|
};
|
|
85
|
-
type stringValue<ctx extends MatchParserContext> = ctx["
|
|
95
|
+
type stringValue<ctx extends MatchParserContext> = ctx["input"] extends string ? ctx["input"] : ctx["key"] extends keyof ctx["input"] ? ctx["input"][ctx["key"]] extends infer s extends string ? s : never : never;
|
|
86
96
|
interface AtParser<ctx extends MatchParserContext> {
|
|
87
97
|
<const key extends string>(key: validateKey<key, ctx>): ChainableMatchParser<ctx.atKey<ctx, key>>;
|
|
88
98
|
<const key extends string, const cases, ctxAtKey extends MatchParserContext = ctx.atKey<ctx, key>>(key: validateKey<key, ctx>, cases: cases extends validateCases<cases, ctxAtKey> ? cases : errorCases<cases, ctxAtKey>): addCasesToParser<cases, ctxAtKey, "def">;
|
package/out/methods/base.d.ts
CHANGED
|
@@ -62,19 +62,19 @@ export interface Inferred<out t = unknown, $ = {}> {
|
|
|
62
62
|
* 🥸 inference-only property that will be `undefined` at runtime
|
|
63
63
|
*
|
|
64
64
|
* @example
|
|
65
|
-
* const
|
|
65
|
+
* const Unmorphed = type("string")
|
|
66
66
|
* // with no morphs, we can introspect the input and output as a single Type
|
|
67
|
-
* type UnmorphedOut = typeof
|
|
67
|
+
* type UnmorphedOut = typeof Unmorphed.inferIntrospectableOut // string
|
|
68
68
|
*
|
|
69
|
-
* const
|
|
69
|
+
* const Morphed = type("string").pipe(s => s.length)
|
|
70
70
|
* // with a standard user-defined morph, TypeScript can infer a
|
|
71
71
|
* // return type from your function, but we have no way to
|
|
72
72
|
* // know the shape at runtime
|
|
73
|
-
* type MorphOut = typeof
|
|
73
|
+
* type MorphOut = typeof Morphed.inferIntrospectableOut // unknown
|
|
74
74
|
*
|
|
75
|
-
* const
|
|
75
|
+
* const Validated = type("string").pipe(s => s.length).to("number")
|
|
76
76
|
* // morphs with validated output, including all morph keywords, are introspectable
|
|
77
|
-
* type ValidatedMorphOut = typeof
|
|
77
|
+
* type ValidatedMorphOut = typeof Validated.inferIntrospectableOut
|
|
78
78
|
*/
|
|
79
79
|
inferIntrospectableOut: distill.introspectable.Out<t>;
|
|
80
80
|
/**
|
|
@@ -113,8 +113,8 @@ export interface Inferred<out t = unknown, $ = {}> {
|
|
|
113
113
|
* ✅ works best for primitive values
|
|
114
114
|
*
|
|
115
115
|
* @example
|
|
116
|
-
* const
|
|
117
|
-
* console.log(
|
|
116
|
+
* const N = type("0 < number <= 100")
|
|
117
|
+
* console.log(N.description) // positive and at most 100
|
|
118
118
|
*/
|
|
119
119
|
description: string;
|
|
120
120
|
/**
|
|
@@ -123,8 +123,8 @@ export interface Inferred<out t = unknown, $ = {}> {
|
|
|
123
123
|
* ✅ works well for both primitives and structures
|
|
124
124
|
*
|
|
125
125
|
* @example
|
|
126
|
-
* const
|
|
127
|
-
* console.log(
|
|
126
|
+
* const Loc = type({ coords: ["number", "number"] })
|
|
127
|
+
* console.log(Loc.expression) // { coords: [number, number] }
|
|
128
128
|
*/
|
|
129
129
|
expression: string;
|
|
130
130
|
/**
|
|
@@ -133,11 +133,11 @@ export interface Inferred<out t = unknown, $ = {}> {
|
|
|
133
133
|
* ✅ sugar to avoid checking for {@link type.errors} if they are unrecoverable
|
|
134
134
|
*
|
|
135
135
|
* @example
|
|
136
|
-
* const
|
|
136
|
+
* const CriticalPayload = type({
|
|
137
137
|
* superImportantValue: "string"
|
|
138
138
|
* })
|
|
139
139
|
* // throws TraversalError: superImportantValue must be a string (was missing)
|
|
140
|
-
* const data =
|
|
140
|
+
* const data = CriticalPayload.assert({ irrelevantValue: "whoops" })
|
|
141
141
|
* console.log(data.superImportantValue) // valid output can be accessed directly
|
|
142
142
|
*
|
|
143
143
|
* @throws {TraversalError}
|
|
@@ -149,9 +149,9 @@ export interface Inferred<out t = unknown, $ = {}> {
|
|
|
149
149
|
* ✅ good for stuff like filtering that doesn't benefit from detailed errors
|
|
150
150
|
*
|
|
151
151
|
* @example
|
|
152
|
-
* const
|
|
152
|
+
* const Numeric = type("number | bigint")
|
|
153
153
|
* // [0, 2n]
|
|
154
|
-
* const numerics = [0, "one", 2n].filter(
|
|
154
|
+
* const numerics = [0, "one", 2n].filter(Numeric.allows)
|
|
155
155
|
*/
|
|
156
156
|
allows(data: unknown): data is this["inferIn"];
|
|
157
157
|
/**
|
|
@@ -160,20 +160,20 @@ export interface Inferred<out t = unknown, $ = {}> {
|
|
|
160
160
|
* ⚠️ does not affect error messages within properties of an object
|
|
161
161
|
*
|
|
162
162
|
* @example
|
|
163
|
-
* const
|
|
163
|
+
* const NotOdd = type("number % 2").configure({ description: "not odd" })
|
|
164
164
|
* // all constraints at the root are affected
|
|
165
|
-
* const odd =
|
|
166
|
-
* const nonNumber =
|
|
165
|
+
* const odd = NotOdd(3) // must be not odd (was 3)
|
|
166
|
+
* const nonNumber = NotOdd("two") // must be not odd (was "two")
|
|
167
167
|
*
|
|
168
|
-
* const
|
|
168
|
+
* const NotOddBox = type({
|
|
169
169
|
* // we should have referenced notOdd or added meta here
|
|
170
170
|
* notOdd: "number % 2",
|
|
171
171
|
* // but instead chained from the root object
|
|
172
172
|
* }).configure({ description: "not odd" })
|
|
173
173
|
* // error message at path notOdd is not affected
|
|
174
|
-
* const oddProp =
|
|
174
|
+
* const oddProp = NotOddBox({ notOdd: 3 }) // notOdd must be even (was 3)
|
|
175
175
|
* // error message at root is affected, leading to a misleading description
|
|
176
|
-
* const nonObject =
|
|
176
|
+
* const nonObject = NotOddBox(null) // must be not odd (was null)
|
|
177
177
|
*/
|
|
178
178
|
configure: NodeSelector.SelectableFn<TypeMeta.MappableInput, this>;
|
|
179
179
|
/**
|
|
@@ -183,10 +183,10 @@ export interface Inferred<out t = unknown, $ = {}> {
|
|
|
183
183
|
* ⚠️ does not affect error messages within properties of an object
|
|
184
184
|
*
|
|
185
185
|
* @example
|
|
186
|
-
* const
|
|
187
|
-
* const good =
|
|
186
|
+
* const AToZ = type(/^a.*z$/).describe("a string like 'a...z'")
|
|
187
|
+
* const good = AToZ("alcatraz") // "alcatraz"
|
|
188
188
|
* // ArkErrors: must be a string like 'a...z' (was "albatross")
|
|
189
|
-
* const badPattern =
|
|
189
|
+
* const badPattern = AToZ("albatross")
|
|
190
190
|
*/
|
|
191
191
|
describe: NodeSelector.SelectableFn<string, this>;
|
|
192
192
|
/**
|
|
@@ -209,9 +209,9 @@ export interface Inferred<out t = unknown, $ = {}> {
|
|
|
209
209
|
* #### alias for {@link assert} with typed input
|
|
210
210
|
*
|
|
211
211
|
* @example
|
|
212
|
-
* const
|
|
212
|
+
* const T = type({ foo: "string" });
|
|
213
213
|
* // TypeScript: foo must be a string (was 5)
|
|
214
|
-
* const data =
|
|
214
|
+
* const data = T.from({ foo: 5 });
|
|
215
215
|
*/
|
|
216
216
|
from(literal: this["inferIn"]): this["infer"];
|
|
217
217
|
/**
|
|
@@ -221,13 +221,13 @@ export interface Inferred<out t = unknown, $ = {}> {
|
|
|
221
221
|
* ✅ good for generating JSON Schema or other non-transforming formats
|
|
222
222
|
*
|
|
223
223
|
* @example
|
|
224
|
-
* const
|
|
224
|
+
* const User = type({
|
|
225
225
|
* age: "string.numeric.parse"
|
|
226
226
|
* })
|
|
227
227
|
* // { age: 25 } (age parsed to a number)
|
|
228
|
-
* const out =
|
|
228
|
+
* const out = User({ age: "25" })
|
|
229
229
|
* // { age: "25" } (age is still a string)
|
|
230
|
-
* const inOut =
|
|
230
|
+
* const inOut = User.in({ age: "25" })
|
|
231
231
|
*/
|
|
232
232
|
get in(): instantiateType<this["inferIn"], $>;
|
|
233
233
|
/**
|
|
@@ -238,15 +238,15 @@ export interface Inferred<out t = unknown, $ = {}> {
|
|
|
238
238
|
* were defined with an explicit output validator via `.to(outputDef)` or `.pipe(morph, outputType)`
|
|
239
239
|
*
|
|
240
240
|
* @example
|
|
241
|
-
* const
|
|
241
|
+
* const join = type("string[]").pipe(a => a.join(","))
|
|
242
242
|
*
|
|
243
|
-
* const
|
|
243
|
+
* const T = type({
|
|
244
244
|
* // all keywords have introspectable output
|
|
245
245
|
* keyword: "string.numeric.parse",
|
|
246
|
-
* // TypeScript knows this returns a
|
|
247
|
-
* unvalidated:
|
|
246
|
+
* // TypeScript knows this returns a string, but we can't introspect that at runtime
|
|
247
|
+
* unvalidated: join,
|
|
248
248
|
* // if needed, it can be made introspectable with an output validator
|
|
249
|
-
* validated:
|
|
249
|
+
* validated: join.to("string")
|
|
250
250
|
* })
|
|
251
251
|
*
|
|
252
252
|
* // Type<{ keyword: number; unvalidated: unknown; validated: string }>
|
|
@@ -259,11 +259,11 @@ export interface Inferred<out t = unknown, $ = {}> {
|
|
|
259
259
|
* 🥸 inference-only function that does nothing runtime
|
|
260
260
|
*
|
|
261
261
|
* @example
|
|
262
|
-
* const
|
|
262
|
+
* const Palindrome = type("string")
|
|
263
263
|
* .narrow(s => s === [...s].reverse().join(""))
|
|
264
264
|
* .brand("palindrome")
|
|
265
265
|
* // Brand<string, "palindrome">
|
|
266
|
-
* const out =
|
|
266
|
+
* const out = Palindrome.assert("racecar")
|
|
267
267
|
*/
|
|
268
268
|
brand<const name extends string, r = instantiateType<type.brand<t, name>, $>>(name: name): r extends infer _ ? _ : never;
|
|
269
269
|
/**
|
|
@@ -271,7 +271,7 @@ export interface Inferred<out t = unknown, $ = {}> {
|
|
|
271
271
|
*
|
|
272
272
|
* @example
|
|
273
273
|
* // Type<{ rebmun: number }[]>
|
|
274
|
-
* const
|
|
274
|
+
* const T = type({ rebmun: "number" }).array();
|
|
275
275
|
*/
|
|
276
276
|
array(): ArrayType<t[], $>;
|
|
277
277
|
/**
|
|
@@ -280,9 +280,9 @@ export interface Inferred<out t = unknown, $ = {}> {
|
|
|
280
280
|
* ⚠️ unlike most other methods, this creates a definition rather than a Type (read why)
|
|
281
281
|
*
|
|
282
282
|
* @example
|
|
283
|
-
* const
|
|
283
|
+
* const Prop = type({ foo: "number" })
|
|
284
284
|
* // Type<{ bar?: { foo: number } }>
|
|
285
|
-
* const
|
|
285
|
+
* const Obj = type({ bar: Prop.optional() })
|
|
286
286
|
*/
|
|
287
287
|
optional(): [this, "?"];
|
|
288
288
|
/**
|
|
@@ -294,10 +294,10 @@ export interface Inferred<out t = unknown, $ = {}> {
|
|
|
294
294
|
*
|
|
295
295
|
* @example
|
|
296
296
|
* // Type<{ count: Default<number, 0> }>
|
|
297
|
-
* const
|
|
298
|
-
* const
|
|
299
|
-
* const
|
|
300
|
-
* key:
|
|
297
|
+
* const State = type({ count: type.number.default(0) })
|
|
298
|
+
* const Prop = type({ nested: "boolean" })
|
|
299
|
+
* const ForObj = type({
|
|
300
|
+
* key: Prop.default(() => ({ nested: false }))
|
|
301
301
|
* })
|
|
302
302
|
*/
|
|
303
303
|
default<const value extends defaultFor<this["inferIn"]>>(value: value): [this, "=", value];
|
|
@@ -312,7 +312,7 @@ export interface Inferred<out t = unknown, $ = {}> {
|
|
|
312
312
|
* const stringifyUser = type({ name: "string" }).pipe(user => JSON.stringify(user))
|
|
313
313
|
* const stringifySafe = stringifyUser.filter(user => user.name !== "Bobby Tables")
|
|
314
314
|
* // Type<(In: `${string}Z`) => To<Date>>
|
|
315
|
-
* const
|
|
315
|
+
* const WithPredicate = type("string.date.parse").filter((s): s is `${string}Z` =>
|
|
316
316
|
* s.endsWith("Z")
|
|
317
317
|
* )
|
|
318
318
|
*/
|
|
@@ -327,13 +327,13 @@ export interface Inferred<out t = unknown, $ = {}> {
|
|
|
327
327
|
* 🥸 {@link https://www.typescriptlang.org/docs/handbook/2/narrowing.html#using-type-predicates | Type predicates} can be used as casts
|
|
328
328
|
*
|
|
329
329
|
* @example
|
|
330
|
-
* const
|
|
330
|
+
* const Palindrome = type("string").narrow(s => s === [...s].reverse().join(""))
|
|
331
331
|
*
|
|
332
|
-
* const
|
|
332
|
+
* const PalindromicEmail = type("string.date.parse").narrow((date, ctx) =>
|
|
333
333
|
* date.getFullYear() === 2025 || ctx.mustBe("the current year")
|
|
334
334
|
* )
|
|
335
335
|
* // Type<`${string}.tsx`>
|
|
336
|
-
* const
|
|
336
|
+
* const WithPredicate = type("string").narrow((s): s is `${string}.tsx` => /\.tsx?$/.test(s))
|
|
337
337
|
*/
|
|
338
338
|
narrow<narrowed extends this["infer"] = never, r = instantiateType<[
|
|
339
339
|
narrowed
|
|
@@ -342,7 +342,7 @@ export interface Inferred<out t = unknown, $ = {}> {
|
|
|
342
342
|
* #### pipe output through arbitrary transformations or other Types
|
|
343
343
|
*
|
|
344
344
|
* @example
|
|
345
|
-
* const
|
|
345
|
+
* const User = type({ name: "string" })
|
|
346
346
|
*
|
|
347
347
|
* // parse a string and validate that the result as a user
|
|
348
348
|
* const parseUser = type("string").pipe(s => JSON.parse(s), user)
|
|
@@ -378,7 +378,7 @@ interface Type<out t = unknown, $ = {}> extends Callable<(data: unknown) => dist
|
|
|
378
378
|
*
|
|
379
379
|
* @example
|
|
380
380
|
* // Type<`LEEEEEEEE${string}ROY`>
|
|
381
|
-
* const
|
|
381
|
+
* const Leeroy = type(/^LE{8,}ROY$/).as<`LEEEEEEEE${string}ROY`>()
|
|
382
382
|
*/
|
|
383
383
|
as<castTo = unset>(...args: validateChainedAsArgs<castTo>): instantiateType<castTo, $>;
|
|
384
384
|
/**
|
|
@@ -386,9 +386,9 @@ interface Type<out t = unknown, $ = {}> extends Callable<(data: unknown) => dist
|
|
|
386
386
|
*
|
|
387
387
|
* @example
|
|
388
388
|
* // Type<{ foo: number; bar: string }>
|
|
389
|
-
* const
|
|
389
|
+
* const T = type({ foo: "number" }).and({ bar: "string" })
|
|
390
390
|
* // ParseError: Intersection at foo of number and string results in an unsatisfiable type
|
|
391
|
-
* const
|
|
391
|
+
* const Bad = type({ foo: "number" }).and({ foo: "string" })
|
|
392
392
|
*/
|
|
393
393
|
and<const def, r = instantiateType<inferIntersection<t, type.infer<def, $>>, $>>(def: type.validate<def, $>): r extends infer _ ? _ : never;
|
|
394
394
|
/**
|
|
@@ -398,7 +398,7 @@ interface Type<out t = unknown, $ = {}> extends Callable<(data: unknown) => dist
|
|
|
398
398
|
*
|
|
399
399
|
* @example
|
|
400
400
|
* // Type<string | { box: string }>
|
|
401
|
-
* const
|
|
401
|
+
* const T = type("string").or({ box: "string" })
|
|
402
402
|
*/
|
|
403
403
|
or<const def, r = instantiateType<t | type.infer<def, $>, $>>(def: type.validate<def, $>): r extends infer _ ? _ : never;
|
|
404
404
|
/**
|
|
@@ -406,10 +406,10 @@ interface Type<out t = unknown, $ = {}> extends Callable<(data: unknown) => dist
|
|
|
406
406
|
*
|
|
407
407
|
* @example
|
|
408
408
|
* // Type<{ foo: number; bar: string }>
|
|
409
|
-
* const
|
|
410
|
-
* const
|
|
409
|
+
* const T = type({ foo: "number" }).intersect({ bar: "string" })
|
|
410
|
+
* const Bad = type("number > 10").intersect("number < 5")
|
|
411
411
|
* // logs "Intersection of > 10 and < 5 results in an unsatisfiable type"
|
|
412
|
-
* if (
|
|
412
|
+
* if (Bad instanceof Disjoint) console.log(`${bad.summary}`)
|
|
413
413
|
*/
|
|
414
414
|
intersect<const def, r = instantiateType<inferIntersection<t, type.infer<def, $>>, $>>(def: type.validate<def, $>): r extends infer _ ? _ | Disjoint : never;
|
|
415
415
|
/**
|
|
@@ -419,14 +419,14 @@ interface Type<out t = unknown, $ = {}> extends Callable<(data: unknown) => dist
|
|
|
419
419
|
* ✅ ignores associated {@link meta}, which does not affect the set of allowed values
|
|
420
420
|
*
|
|
421
421
|
* @example
|
|
422
|
-
* const
|
|
422
|
+
* const DivisibleBy6 = type.number.divisibleBy(6).moreThan(0)
|
|
423
423
|
* // false (left side must also be positive)
|
|
424
|
-
*
|
|
424
|
+
* DivisibleBy6.equals("number % 6")
|
|
425
425
|
* // false (right side has an additional <100 constraint)
|
|
426
|
-
* console.log(
|
|
427
|
-
* const
|
|
426
|
+
* console.log(DivisibleBy6.equals("0 < (number % 6) < 100"))
|
|
427
|
+
* const ThirdTry = type("(number % 2) > 0").divisibleBy(3)
|
|
428
428
|
* // true (types are normalized and reduced)
|
|
429
|
-
* console.log(
|
|
429
|
+
* console.log(DivisibleBy6.equals(ThirdTry))
|
|
430
430
|
*/
|
|
431
431
|
equals<const def>(def: type.validate<def, $>): boolean;
|
|
432
432
|
/**
|
|
@@ -435,9 +435,9 @@ interface Type<out t = unknown, $ = {}> extends Callable<(data: unknown) => dist
|
|
|
435
435
|
* ✅ ignores associated {@link meta}, which does not affect the set of allowed values
|
|
436
436
|
*
|
|
437
437
|
* @example
|
|
438
|
-
* const
|
|
438
|
+
* const N = type.raw(`${Math.random()}`)
|
|
439
439
|
* // Type<0.5> | undefined
|
|
440
|
-
* const
|
|
440
|
+
* const Ez = N.ifEquals("0.5")
|
|
441
441
|
*/
|
|
442
442
|
ifEquals<const def, r = type.instantiate<def, $>>(def: type.validate<def, $>): r extends infer _ ? _ | undefined : never;
|
|
443
443
|
/**
|
|
@@ -458,8 +458,8 @@ interface Type<out t = unknown, $ = {}> extends Callable<(data: unknown) => dist
|
|
|
458
458
|
* ✅ ignores associated {@link meta}, which does not affect the set of allowed values
|
|
459
459
|
*
|
|
460
460
|
* @example
|
|
461
|
-
* const
|
|
462
|
-
* const
|
|
461
|
+
* const N = type(Math.random() > 0.5 ? "true" : "0") // Type<0 | true>
|
|
462
|
+
* const Ez = N.ifExtends("boolean") // Type<true> | undefined
|
|
463
463
|
*/
|
|
464
464
|
ifExtends<const def, r = type.instantiate<def, $>>(other: type.validate<def, $>): r extends infer _ ? _ | undefined : never;
|
|
465
465
|
/**
|
|
@@ -472,8 +472,8 @@ interface Type<out t = unknown, $ = {}> extends Callable<(data: unknown) => dist
|
|
|
472
472
|
* type("string | number").overlaps("1") // true (1)
|
|
473
473
|
* type("number > 0").overlaps("number < 0") // false (no values exist)
|
|
474
474
|
*
|
|
475
|
-
* const
|
|
476
|
-
*
|
|
475
|
+
* const NoAt = type("string").narrow(s => !s.includes("@"))
|
|
476
|
+
* NoAt.overlaps("string.email") // true (no values exist, but not provable)
|
|
477
477
|
*/
|
|
478
478
|
overlaps<const def>(r: type.validate<def, $>): boolean;
|
|
479
479
|
/**
|
|
@@ -481,7 +481,7 @@ interface Type<out t = unknown, $ = {}> extends Callable<(data: unknown) => dist
|
|
|
481
481
|
*
|
|
482
482
|
* @example
|
|
483
483
|
* // Type<true | 0 | 2>
|
|
484
|
-
* const
|
|
484
|
+
* const T = type("boolean | 0 | 'one' | 2 | bigint").extract("number | 0n | true")
|
|
485
485
|
*/
|
|
486
486
|
extract<const def, r = instantiateType<t extends type.infer<def, $> ? t : never, $>>(r: type.validate<def, $>): r extends infer _ extends r ? _ : never;
|
|
487
487
|
/**
|
|
@@ -490,7 +490,7 @@ interface Type<out t = unknown, $ = {}> extends Callable<(data: unknown) => dist
|
|
|
490
490
|
* @example
|
|
491
491
|
*
|
|
492
492
|
* // Type<false | 'one' | bigint>
|
|
493
|
-
* const
|
|
493
|
+
* const T = type("boolean | 0 | 'one' | 2 | bigint").exclude("number | 0n | true")
|
|
494
494
|
*/
|
|
495
495
|
exclude<const def, r = instantiateType<t extends type.infer<def, $> ? never : t, $>>(r: type.validate<def, $>): r extends infer _ ? _ : never;
|
|
496
496
|
/**
|
package/out/type.d.ts
CHANGED
|
@@ -16,7 +16,7 @@ export interface TypeParser<$ = {}> extends Ark.boundTypeAttachments<$> {
|
|
|
16
16
|
/**
|
|
17
17
|
* Create a {@link Type} from your definition.
|
|
18
18
|
*
|
|
19
|
-
* @example const
|
|
19
|
+
* @example const Person = type({ name: "string" })
|
|
20
20
|
*/
|
|
21
21
|
<const def, r = type.instantiate<def, $>>(def: type.validate<def, $>): r extends infer _ ? _ : never;
|
|
22
22
|
/**
|
|
@@ -29,7 +29,7 @@ export interface TypeParser<$ = {}> extends Ark.boundTypeAttachments<$> {
|
|
|
29
29
|
* parameter names specified in the previous argument in addition to aliases
|
|
30
30
|
* from its {@link Scope}.
|
|
31
31
|
*
|
|
32
|
-
* @example const
|
|
32
|
+
* @example const BoxOf = type("<t extends string | number>", { contents: "t" })
|
|
33
33
|
*/
|
|
34
34
|
<const params extends ParameterString, const def, r = Generic<parseValidGenericParams<params, $>, def, $>>(params: validateParameterString<params, $>, def: type.validate<def, $, baseGenericConstraints<parseValidGenericParams<params, $>>>): r extends infer _ ? _ : never;
|
|
35
35
|
/**
|
|
@@ -77,8 +77,8 @@ export interface TypeParser<$ = {}> extends Ark.boundTypeAttachments<$> {
|
|
|
77
77
|
/**
|
|
78
78
|
* Create a {@link Type} that is satisfied only by a value strictly equal (`===`) to one of the arguments passed to this function.
|
|
79
79
|
* @example const enum = type.enumerated('foo', 'bar', obj) // obj is a by-reference object
|
|
80
|
-
* @example const
|
|
81
|
-
* @example const
|
|
80
|
+
* @example const TupleForm = type(['===', 'foo', 'bar', obj])
|
|
81
|
+
* @example const ArgsForm = type('===', 'foo', 'bar', obj)
|
|
82
82
|
*/
|
|
83
83
|
enumerated: EnumeratedTypeParser<$>;
|
|
84
84
|
/**
|
|
@@ -95,12 +95,12 @@ export interface TypeParser<$ = {}> extends Ark.boundTypeAttachments<$> {
|
|
|
95
95
|
instanceOf: InstanceOfTypeParser<$>;
|
|
96
96
|
/**
|
|
97
97
|
* Create a {@link Type} from a union of definitions
|
|
98
|
-
* @example const
|
|
98
|
+
* @example const T = type.or("string", "number")
|
|
99
99
|
*/
|
|
100
100
|
or: NaryUnionParser<$>;
|
|
101
101
|
/**
|
|
102
102
|
* Create a {@link Type} from an intersection of definitions
|
|
103
|
-
* @example const
|
|
103
|
+
* @example const T = type.and({ a: "1" }, { b: "2" })
|
|
104
104
|
*/
|
|
105
105
|
and: NaryIntersectionParser<$>;
|
|
106
106
|
/**
|
|
@@ -108,14 +108,14 @@ export interface TypeParser<$ = {}> extends Ark.boundTypeAttachments<$> {
|
|
|
108
108
|
* definitions having precedence for overlapping keys
|
|
109
109
|
* @example
|
|
110
110
|
* // Type<{ a: "3", b: "2", c: "4" }>
|
|
111
|
-
* const
|
|
111
|
+
* const T = type.merge({ a: "1", b: "2" }, { a: "3", c: "4" })
|
|
112
112
|
*/
|
|
113
113
|
merge: NaryMergeParser<$>;
|
|
114
114
|
/**
|
|
115
115
|
* Create a {@link Type} from a set of morphs (including Types)
|
|
116
116
|
* @example
|
|
117
117
|
* // Type<(In: string) => To<object>>
|
|
118
|
-
* const
|
|
118
|
+
* const T = type.pipe(type.string, s => JSON.parse(s), type.object)
|
|
119
119
|
*/
|
|
120
120
|
pipe: NaryPipeParser<$>;
|
|
121
121
|
}
|
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.1.
|
|
4
|
+
"version": "2.1.17",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
@@ -34,8 +34,8 @@
|
|
|
34
34
|
"out"
|
|
35
35
|
],
|
|
36
36
|
"dependencies": {
|
|
37
|
-
"@ark/util": "0.45.
|
|
38
|
-
"@ark/schema": "0.45.
|
|
37
|
+
"@ark/util": "0.45.7",
|
|
38
|
+
"@ark/schema": "0.45.7"
|
|
39
39
|
},
|
|
40
40
|
"publishConfig": {
|
|
41
41
|
"access": "public"
|