@evolu/common 7.4.1 → 8.0.0-next.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -61
- package/dist/src/Array.d.ts +343 -102
- package/dist/src/Array.d.ts.map +1 -1
- package/dist/src/Array.js +181 -85
- package/dist/src/Assert.d.ts +57 -11
- package/dist/src/Assert.d.ts.map +1 -1
- package/dist/src/Assert.js +47 -11
- package/dist/src/BigInt.d.ts +6 -1
- package/dist/src/BigInt.d.ts.map +1 -1
- package/dist/src/BigInt.js +5 -0
- package/dist/src/Brand.d.ts +40 -12
- package/dist/src/Brand.d.ts.map +1 -1
- package/dist/src/Brand.js +5 -0
- package/dist/src/Buffer.d.ts +6 -1
- package/dist/src/Buffer.d.ts.map +1 -1
- package/dist/src/Buffer.js +7 -2
- package/dist/src/Cache.d.ts +10 -2
- package/dist/src/Cache.d.ts.map +1 -1
- package/dist/src/Cache.js +6 -0
- package/dist/src/Callbacks.d.ts +10 -7
- package/dist/src/Callbacks.d.ts.map +1 -1
- package/dist/src/Callbacks.js +23 -13
- package/dist/src/Console.d.ts +361 -69
- package/dist/src/Console.d.ts.map +1 -1
- package/dist/src/Console.js +217 -117
- package/dist/src/Crypto.d.ts +70 -21
- package/dist/src/Crypto.d.ts.map +1 -1
- package/dist/src/Crypto.js +72 -27
- package/dist/src/Eq.d.ts +22 -2
- package/dist/src/Eq.d.ts.map +1 -1
- package/dist/src/Eq.js +21 -1
- package/dist/src/Error.d.ts +23 -12
- package/dist/src/Error.d.ts.map +1 -1
- package/dist/src/Error.js +27 -11
- package/dist/src/Function.d.ts +128 -56
- package/dist/src/Function.d.ts.map +1 -1
- package/dist/src/Function.js +118 -14
- package/dist/src/Identicon.d.ts +5 -0
- package/dist/src/Identicon.d.ts.map +1 -1
- package/dist/src/Identicon.js +6 -1
- package/dist/src/Lookup.d.ts +162 -0
- package/dist/src/Lookup.d.ts.map +1 -0
- package/dist/src/Lookup.js +200 -0
- package/dist/src/Microtask.d.ts +21 -0
- package/dist/src/Microtask.d.ts.map +1 -0
- package/dist/src/Microtask.js +37 -0
- package/dist/src/Number.d.ts +18 -4
- package/dist/src/Number.d.ts.map +1 -1
- package/dist/src/Number.js +23 -1
- package/dist/src/Object.d.ts +110 -5
- package/dist/src/Object.d.ts.map +1 -1
- package/dist/src/Object.js +108 -5
- package/dist/src/Option.d.ts +58 -0
- package/dist/src/Option.d.ts.map +1 -0
- package/dist/src/Option.js +43 -0
- package/dist/src/Order.d.ts +5 -0
- package/dist/src/Order.d.ts.map +1 -1
- package/dist/src/Order.js +8 -3
- package/dist/src/Platform.d.ts +36 -2
- package/dist/src/Platform.d.ts.map +1 -1
- package/dist/src/Platform.js +10 -6
- package/dist/src/Polyfills.d.ts +29 -0
- package/dist/src/Polyfills.d.ts.map +1 -0
- package/dist/src/Polyfills.js +329 -0
- package/dist/src/Random.d.ts +18 -7
- package/dist/src/Random.d.ts.map +1 -1
- package/dist/src/Random.js +9 -7
- package/dist/src/Redacted.d.ts +7 -4
- package/dist/src/Redacted.d.ts.map +1 -1
- package/dist/src/Redacted.js +5 -0
- package/dist/src/Ref.d.ts +31 -16
- package/dist/src/Ref.d.ts.map +1 -1
- package/dist/src/Ref.js +35 -7
- package/dist/src/RefCount.d.ts +62 -0
- package/dist/src/RefCount.d.ts.map +1 -0
- package/dist/src/RefCount.js +83 -0
- package/dist/src/Relation.d.ts +84 -29
- package/dist/src/Relation.d.ts.map +1 -1
- package/dist/src/Relation.js +83 -105
- package/dist/src/Resource.d.ts +263 -0
- package/dist/src/Resource.d.ts.map +1 -0
- package/dist/src/Resource.js +389 -0
- package/dist/src/Result.d.ts +390 -374
- package/dist/src/Result.d.ts.map +1 -1
- package/dist/src/Result.js +142 -70
- package/dist/src/Schedule.d.ts +953 -0
- package/dist/src/Schedule.d.ts.map +1 -0
- package/dist/src/Schedule.js +1199 -0
- package/dist/src/Set.d.ts +181 -0
- package/dist/src/Set.d.ts.map +1 -0
- package/dist/src/Set.js +137 -0
- package/dist/src/Skiplist.d.ts +6 -1
- package/dist/src/Skiplist.d.ts.map +1 -1
- package/dist/src/Skiplist.js +5 -0
- package/dist/src/Sqlite.d.ts +160 -60
- package/dist/src/Sqlite.d.ts.map +1 -1
- package/dist/src/Sqlite.js +290 -190
- package/dist/src/Store.d.ts +36 -28
- package/dist/src/Store.d.ts.map +1 -1
- package/dist/src/Store.js +55 -15
- package/dist/src/String.d.ts +5 -0
- package/dist/src/String.d.ts.map +1 -1
- package/dist/src/String.js +5 -0
- package/dist/src/Task.d.ts +2274 -423
- package/dist/src/Task.d.ts.map +1 -1
- package/dist/src/Task.js +1499 -348
- package/dist/src/Test.d.ts +150 -0
- package/dist/src/Test.d.ts.map +1 -0
- package/dist/src/Test.js +97 -0
- package/dist/src/Time.d.ts +202 -48
- package/dist/src/Time.d.ts.map +1 -1
- package/dist/src/Time.js +199 -88
- package/dist/src/Tracer.d.ts +48 -0
- package/dist/src/Tracer.d.ts.map +1 -0
- package/dist/src/Tracer.js +6 -0
- package/dist/src/Type.d.ts +587 -209
- package/dist/src/Type.d.ts.map +1 -1
- package/dist/src/Type.js +370 -240
- package/dist/src/Types.d.ts +160 -6
- package/dist/src/Types.d.ts.map +1 -1
- package/dist/src/Types.js +22 -2
- package/dist/src/WebSocket.d.ts +130 -58
- package/dist/src/WebSocket.d.ts.map +1 -1
- package/dist/src/WebSocket.js +274 -113
- package/dist/src/Worker.d.ts +245 -60
- package/dist/src/Worker.d.ts.map +1 -1
- package/dist/src/Worker.js +220 -64
- package/dist/src/index.d.ts +25 -3
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +19 -3
- package/dist/src/local-first/Db.d.ts +24 -213
- package/dist/src/local-first/Db.d.ts.map +1 -1
- package/dist/src/local-first/Db.js +621 -293
- package/dist/src/local-first/Error.d.ts +12 -0
- package/dist/src/local-first/Error.d.ts.map +1 -0
- package/dist/src/local-first/Error.js +6 -0
- package/dist/src/local-first/Evolu.d.ts +330 -285
- package/dist/src/local-first/Evolu.d.ts.map +1 -1
- package/dist/src/local-first/Evolu.js +472 -402
- package/dist/src/local-first/LocalAuth.d.ts +10 -10
- package/dist/src/local-first/LocalAuth.d.ts.map +1 -1
- package/dist/src/local-first/LocalAuth.js +6 -1
- package/dist/src/local-first/Owner.d.ts +70 -27
- package/dist/src/local-first/Owner.d.ts.map +1 -1
- package/dist/src/local-first/Owner.js +27 -14
- package/dist/src/local-first/Protocol.d.ts +58 -50
- package/dist/src/local-first/Protocol.d.ts.map +1 -1
- package/dist/src/local-first/Protocol.js +253 -155
- package/dist/src/local-first/Query.d.ts +133 -43
- package/dist/src/local-first/Query.d.ts.map +1 -1
- package/dist/src/local-first/Query.js +139 -90
- package/dist/src/local-first/Relay.d.ts +34 -49
- package/dist/src/local-first/Relay.d.ts.map +1 -1
- package/dist/src/local-first/Relay.js +59 -177
- package/dist/src/local-first/Schema.d.ts +139 -144
- package/dist/src/local-first/Schema.d.ts.map +1 -1
- package/dist/src/local-first/Schema.js +78 -163
- package/dist/src/local-first/Shared.d.ts +125 -0
- package/dist/src/local-first/Shared.d.ts.map +1 -0
- package/dist/src/local-first/Shared.js +569 -0
- package/dist/src/local-first/Storage.d.ts +54 -58
- package/dist/src/local-first/Storage.d.ts.map +1 -1
- package/dist/src/local-first/Storage.js +127 -179
- package/dist/src/local-first/Timestamp.d.ts +28 -35
- package/dist/src/local-first/Timestamp.d.ts.map +1 -1
- package/dist/src/local-first/Timestamp.js +24 -25
- package/dist/src/local-first/index.d.ts +2 -15
- package/dist/src/local-first/index.d.ts.map +1 -1
- package/dist/src/local-first/index.js +2 -15
- package/package.json +37 -24
- package/src/Array.ts +467 -106
- package/src/Assert.ts +87 -12
- package/src/BigInt.ts +7 -1
- package/src/Brand.ts +41 -12
- package/src/Buffer.ts +10 -4
- package/src/Cache.ts +11 -2
- package/src/Callbacks.ts +33 -23
- package/src/Console.ts +567 -178
- package/src/Crypto.ts +111 -62
- package/src/Eq.ts +30 -3
- package/src/Error.ts +50 -34
- package/src/Function.ts +140 -68
- package/src/Identicon.ts +7 -1
- package/src/Lookup.ts +429 -0
- package/src/Microtask.ts +58 -0
- package/src/Number.ts +40 -7
- package/src/Object.ts +155 -16
- package/src/Option.ts +74 -0
- package/src/Order.ts +11 -3
- package/src/Platform.ts +43 -7
- package/src/Polyfills.ts +520 -0
- package/src/Random.ts +25 -11
- package/src/Redacted.ts +8 -4
- package/src/Ref.ts +72 -21
- package/src/RefCount.ts +170 -0
- package/src/Relation.ts +188 -136
- package/src/Resource.ts +864 -0
- package/src/Result.ts +548 -381
- package/src/Schedule.ts +1471 -0
- package/src/Set.ts +247 -0
- package/src/Skiplist.ts +7 -1
- package/src/Sqlite.ts +409 -304
- package/src/Store.ts +94 -48
- package/src/String.ts +6 -0
- package/src/Task.ts +3696 -740
- package/src/Test.ts +202 -0
- package/src/Time.ts +357 -147
- package/src/Tracer.ts +54 -0
- package/src/Type.ts +866 -430
- package/src/Types.ts +202 -7
- package/src/WebSocket.ts +404 -167
- package/src/Worker.ts +527 -147
- package/src/index.ts +74 -3
- package/src/local-first/Db.ts +867 -597
- package/src/local-first/Error.ts +17 -0
- package/src/local-first/Evolu.ts +884 -824
- package/src/local-first/LocalAuth.ts +10 -10
- package/src/local-first/Owner.ts +94 -42
- package/src/local-first/Protocol.ts +441 -307
- package/src/local-first/Query.ts +243 -194
- package/src/local-first/Relay.ts +119 -273
- package/src/local-first/Schema.ts +284 -394
- package/src/local-first/Shared.ts +932 -0
- package/src/local-first/Storage.ts +211 -283
- package/src/local-first/Timestamp.ts +38 -45
- package/src/local-first/index.ts +2 -16
- package/dist/src/Instances.d.ts +0 -34
- package/dist/src/Instances.d.ts.map +0 -1
- package/dist/src/Instances.js +0 -44
- package/dist/src/Resources.d.ts +0 -118
- package/dist/src/Resources.d.ts.map +0 -1
- package/dist/src/Resources.js +0 -197
- package/dist/src/local-first/Platform.d.ts +0 -25
- package/dist/src/local-first/Platform.d.ts.map +0 -1
- package/dist/src/local-first/Platform.js +0 -1
- package/dist/src/local-first/Public.d.ts +0 -22
- package/dist/src/local-first/Public.d.ts.map +0 -1
- package/dist/src/local-first/Public.js +0 -15
- package/dist/src/local-first/PublicKysely.d.ts +0 -148
- package/dist/src/local-first/PublicKysely.d.ts.map +0 -1
- package/dist/src/local-first/PublicKysely.js +0 -184
- package/dist/src/local-first/Sync.d.ts +0 -112
- package/dist/src/local-first/Sync.d.ts.map +0 -1
- package/dist/src/local-first/Sync.js +0 -526
- package/src/Instances.ts +0 -92
- package/src/Resources.ts +0 -367
- package/src/local-first/Platform.ts +0 -27
- package/src/local-first/Public.ts +0 -42
- package/src/local-first/PublicKysely.ts +0 -239
- package/src/local-first/Sync.ts +0 -949
package/dist/src/Type.d.ts
CHANGED
|
@@ -1,14 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Runtime type validation, parsing, and branded types.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
1
6
|
import type { Brand } from "./Brand.js";
|
|
2
7
|
import type { RandomBytesDep } from "./Crypto.js";
|
|
3
|
-
import { Result } from "./Result.js";
|
|
8
|
+
import type { Result } from "./Result.js";
|
|
4
9
|
import type { TimeDep } from "./Time.js";
|
|
5
10
|
import type { Literal, Refinement, Simplify, WidenLiteral } from "./Types.js";
|
|
6
11
|
/**
|
|
7
12
|
* Evolu {@link Type} is like a type guard that returns typed errors (via
|
|
8
|
-
* {@link Result}) instead of throwing. We either
|
|
9
|
-
* composable typed error
|
|
13
|
+
* {@link Result}) instead of throwing. We either receive a safely typed value or
|
|
14
|
+
* a composable typed error that tells us exactly why the validation failed.
|
|
10
15
|
*
|
|
11
|
-
*
|
|
16
|
+
* The reason why Evolu Type exists is that no other TypeScript validation
|
|
17
|
+
* library met Evolu's requirements. A distinctive feature of Evolu Type
|
|
18
|
+
* compared to other validation libraries is that it returns typed errors rather
|
|
19
|
+
* than string messages. This allows TypeScript to enforce that all validation
|
|
20
|
+
* errors are handled via {@link exhaustiveCheck}, significantly improving the
|
|
21
|
+
* developer experience. Those requirements are:
|
|
12
22
|
*
|
|
13
23
|
* - **Result-based error handling** – no exceptions for normal control flow.
|
|
14
24
|
* - **Typed errors with decoupled formatters** – validation logic ≠ user
|
|
@@ -21,15 +31,10 @@ import type { Literal, Refinement, Simplify, WidenLiteral } from "./Types.js";
|
|
|
21
31
|
* bottom.
|
|
22
32
|
* - **No user-land chaining DSL** – prepared for TC39 Hack pipes.
|
|
23
33
|
*
|
|
24
|
-
* A distinctive feature of Evolu Type compared to other validation libraries is
|
|
25
|
-
* that it returns typed errors rather than string messages. This allows
|
|
26
|
-
* TypeScript to enforce that all validation errors are handled by type
|
|
27
|
-
* checking, significantly improving the developer experience.
|
|
28
|
-
*
|
|
29
34
|
* Evolu Type supports [Standard Schema](https://standardschema.dev/) for
|
|
30
35
|
* interoperability with 40+ validation-compatible tools and frameworks.
|
|
31
36
|
*
|
|
32
|
-
*
|
|
37
|
+
* ## Base Types
|
|
33
38
|
*
|
|
34
39
|
* ```ts
|
|
35
40
|
* // Validate unknown values
|
|
@@ -37,7 +42,7 @@ import type { Literal, Refinement, Simplify, WidenLiteral } from "./Types.js";
|
|
|
37
42
|
* const stringResult = String.fromUnknown(value);
|
|
38
43
|
* if (!stringResult.ok) {
|
|
39
44
|
* // console.error(formatStringError(stringResult.error));
|
|
40
|
-
* return stringResult;
|
|
45
|
+
* return stringResult;
|
|
41
46
|
* }
|
|
42
47
|
* // Safe branch: value is now string
|
|
43
48
|
* const upper = stringResult.value.toUpperCase();
|
|
@@ -56,10 +61,12 @@ import type { Literal, Refinement, Simplify, WidenLiteral } from "./Types.js";
|
|
|
56
61
|
* Point.from({ x: 1, y: "2" }); // err -> nested Number error
|
|
57
62
|
* ```
|
|
58
63
|
*
|
|
59
|
-
*
|
|
64
|
+
* ## Branded types
|
|
60
65
|
*
|
|
61
|
-
* Branding
|
|
62
|
-
*
|
|
66
|
+
* Branding is the recommended way to define types in Evolu. Instead of using
|
|
67
|
+
* primitive types like `string` or `number` directly, wrap them with
|
|
68
|
+
* {@link brand} to create semantically meaningful types. See {@link Brand} for
|
|
69
|
+
* why this matters.
|
|
63
70
|
*
|
|
64
71
|
* ```ts
|
|
65
72
|
* const CurrencyCode = brand("CurrencyCode", String, (value) =>
|
|
@@ -83,20 +90,25 @@ import type { Literal, Refinement, Simplify, WidenLiteral } from "./Types.js";
|
|
|
83
90
|
* See also reusable brand factories like `minLength`, `maxLength`, `trimmed`,
|
|
84
91
|
* `positive`, `between`, etc.
|
|
85
92
|
*
|
|
86
|
-
*
|
|
93
|
+
* ## Object types
|
|
87
94
|
*
|
|
88
95
|
* ```ts
|
|
89
96
|
* const User = object({
|
|
90
97
|
* name: NonEmptyTrimmedString100,
|
|
91
98
|
* age: optional(PositiveInt),
|
|
92
99
|
* });
|
|
93
|
-
*
|
|
100
|
+
*
|
|
101
|
+
* // Use interface for objects. TypeScript displays the interface name
|
|
102
|
+
* // instead of expanding all properties.
|
|
103
|
+
* interface User extends InferType<typeof User> {}
|
|
94
104
|
*
|
|
95
105
|
* User.from({ name: "Alice" }); // ok
|
|
96
106
|
* User.from({ name: "Alice", age: -1 }); // err(PositiveInt)
|
|
107
|
+
*
|
|
108
|
+
* // TODO: Add `record`
|
|
97
109
|
* ```
|
|
98
110
|
*
|
|
99
|
-
*
|
|
111
|
+
* ## JSON type
|
|
100
112
|
*
|
|
101
113
|
* ```ts
|
|
102
114
|
* const Person = object({
|
|
@@ -105,7 +117,7 @@ import type { Literal, Refinement, Simplify, WidenLiteral } from "./Types.js";
|
|
|
105
117
|
* // To prevent this, use FiniteNumber.
|
|
106
118
|
* age: FiniteNumber,
|
|
107
119
|
* });
|
|
108
|
-
*
|
|
120
|
+
* interface Person extends InferType<typeof Person> {}
|
|
109
121
|
*
|
|
110
122
|
* const [PersonJson, personToPersonJson, personJsonToPerson] = json(
|
|
111
123
|
* Person,
|
|
@@ -123,7 +135,7 @@ import type { Literal, Refinement, Simplify, WidenLiteral } from "./Types.js";
|
|
|
123
135
|
* expect(personJsonToPerson(personJson)).toEqual(person);
|
|
124
136
|
* ```
|
|
125
137
|
*
|
|
126
|
-
*
|
|
138
|
+
* ## Error Formatting
|
|
127
139
|
*
|
|
128
140
|
* Evolu separates validation logic from human-readable messages. There are two
|
|
129
141
|
* layers:
|
|
@@ -134,14 +146,14 @@ import type { Literal, Refinement, Simplify, WidenLiteral } from "./Types.js";
|
|
|
134
146
|
* custom errors (including nested composite types) and lets us override
|
|
135
147
|
* selected messages.
|
|
136
148
|
*
|
|
137
|
-
*
|
|
149
|
+
* ### 1. Per-Type formatter
|
|
138
150
|
*
|
|
139
151
|
* ```ts
|
|
140
152
|
* const r = String.fromUnknown(42);
|
|
141
153
|
* if (!r.ok) console.error(formatStringError(r.error));
|
|
142
154
|
* ```
|
|
143
155
|
*
|
|
144
|
-
*
|
|
156
|
+
* ### 2. Unified formatter with overrides
|
|
145
157
|
*
|
|
146
158
|
* ```ts
|
|
147
159
|
* // Override only what we care about; fall back to built-ins for the rest.
|
|
@@ -164,12 +176,14 @@ import type { Literal, Refinement, Simplify, WidenLiteral } from "./Types.js";
|
|
|
164
176
|
* tuple / union) and applies overrides only where specified, greatly reducing
|
|
165
177
|
* boilerplate when formatting complex validation errors.
|
|
166
178
|
*
|
|
167
|
-
*
|
|
179
|
+
* ## Naming
|
|
168
180
|
*
|
|
169
|
-
*
|
|
170
|
-
*
|
|
181
|
+
* Evolu Types intentionally use the same names as native JavaScript types
|
|
182
|
+
* (String, Number, Boolean, etc.). When you need to distinguish between an
|
|
183
|
+
* Evolu Type and the native type, use `globalThis` to reference the native one
|
|
184
|
+
* (e.g., `globalThis.String`, `globalThis.Number`).
|
|
171
185
|
*
|
|
172
|
-
*
|
|
186
|
+
* ## Design decision
|
|
173
187
|
*
|
|
174
188
|
* Evolu Type intentionally does not support bidirectional transformations. It
|
|
175
189
|
* previously did, but supporting that while keeping typed error fidelity added
|
|
@@ -178,28 +192,62 @@ import type { Literal, Refinement, Simplify, WidenLiteral } from "./Types.js";
|
|
|
178
192
|
* reverse transforms would not buy much. We may revisit this if we can design a
|
|
179
193
|
* minimal, 100% safe API that preserves simplicity.
|
|
180
194
|
*
|
|
181
|
-
*
|
|
195
|
+
* ## Composition without pipe
|
|
182
196
|
*
|
|
183
197
|
* Take a look how `SimplePassword` is defined:
|
|
184
198
|
*
|
|
185
199
|
* ```ts
|
|
186
|
-
*
|
|
200
|
+
* const SimplePassword = brand(
|
|
187
201
|
* "SimplePassword",
|
|
188
202
|
* minLength(8)(maxLength(64)(TrimmedString)),
|
|
189
203
|
* );
|
|
190
204
|
* ```
|
|
191
205
|
*
|
|
192
|
-
*
|
|
193
|
-
* TC39 Hack pipes it would be clearer:
|
|
206
|
+
* Shallow nesting often fits one line. If it doesn't, split into named parts:
|
|
194
207
|
*
|
|
195
208
|
* ```ts
|
|
196
|
-
*
|
|
197
|
-
*
|
|
198
|
-
* // |> maxLength(64)(%)
|
|
199
|
-
* // |> brand("SimplePassword", %)
|
|
209
|
+
* const Min8TrimmedString64 = minLength(8)(maxLength(64)(TrimmedString));
|
|
210
|
+
* const SimplePassword = brand("SimplePassword", Min8TrimmedString64);
|
|
200
211
|
* ```
|
|
201
212
|
*
|
|
202
|
-
*
|
|
213
|
+
* ## FAQ
|
|
214
|
+
*
|
|
215
|
+
* ### How do I create a generic interface like `FooState<T>`?
|
|
216
|
+
*
|
|
217
|
+
* TypeScript's {@link InferType} extracts a concrete type, not a generic one. We
|
|
218
|
+
* cannot write `interface FooState<T> extends InferType<typeof fooState<T>>`
|
|
219
|
+
* because `InferType` needs a concrete Type instance.
|
|
220
|
+
*
|
|
221
|
+
* The recommended approach is to define the generic interface manually, then
|
|
222
|
+
* create a Type factory that produces structurally compatible Types:
|
|
223
|
+
*
|
|
224
|
+
* ```ts
|
|
225
|
+
* // Define the generic interface manually
|
|
226
|
+
* interface FooState<T> {
|
|
227
|
+
* readonly value: T;
|
|
228
|
+
* readonly loading: boolean;
|
|
229
|
+
* }
|
|
230
|
+
*
|
|
231
|
+
* // Create a Type factory that produces Types matching the interface
|
|
232
|
+
* const fooState = <T extends AnyType>(valueType: T) =>
|
|
233
|
+
* object({
|
|
234
|
+
* value: valueType,
|
|
235
|
+
* loading: Boolean,
|
|
236
|
+
* });
|
|
237
|
+
*
|
|
238
|
+
* // Usage
|
|
239
|
+
* const StringFooState = fooState(String);
|
|
240
|
+
* type StringFooState = InferType<typeof StringFooState>;
|
|
241
|
+
*
|
|
242
|
+
* // The interface and inferred type are structurally compatible
|
|
243
|
+
* const state: FooState<string> = StringFooState.orThrow({
|
|
244
|
+
* value: "hi",
|
|
245
|
+
* loading: false,
|
|
246
|
+
* });
|
|
247
|
+
* ```
|
|
248
|
+
*
|
|
249
|
+
* This keeps the interface generic while having type-safe runtime validation
|
|
250
|
+
* for each concrete use.
|
|
203
251
|
*/
|
|
204
252
|
export interface Type<Name extends TypeName,
|
|
205
253
|
/** The type this Type resolves to. */
|
|
@@ -224,6 +272,9 @@ ParentError extends TypeError = Error> extends StandardSchemaV1<Input, T> {
|
|
|
224
272
|
/**
|
|
225
273
|
* Creates `T` from an `Input` value, throwing an error if validation fails.
|
|
226
274
|
*
|
|
275
|
+
* Use this where failure should crash the current flow instead of being
|
|
276
|
+
* handled locally.
|
|
277
|
+
*
|
|
227
278
|
* Throws an Error with the Type validation error in its `cause` property,
|
|
228
279
|
* making it debuggable while avoiding the need for custom error messages.
|
|
229
280
|
*
|
|
@@ -231,36 +282,41 @@ ParentError extends TypeError = Error> extends StandardSchemaV1<Input, T> {
|
|
|
231
282
|
*
|
|
232
283
|
* **When to use:**
|
|
233
284
|
*
|
|
234
|
-
* -
|
|
235
|
-
*
|
|
236
|
-
*
|
|
285
|
+
* - Application startup or composition-root setup where errors must stop the
|
|
286
|
+
* program immediately. In Evolu apps, errors are handled by
|
|
287
|
+
* platform-specific `createRun` adapters at the app boundary.
|
|
288
|
+
* - Module-level constants
|
|
289
|
+
* - Test setup with values that are expected to be valid
|
|
237
290
|
* - As an alternative to assertions when the Type error in the thrown Error's
|
|
238
291
|
* `cause` provides sufficient debugging information
|
|
239
|
-
*
|
|
240
|
-
*
|
|
241
|
-
*
|
|
292
|
+
*
|
|
293
|
+
* Prefer `from` in ordinary application logic where the caller can recover,
|
|
294
|
+
* show validation errors, or choose a different flow.
|
|
295
|
+
*
|
|
296
|
+
* For clearer test failure messages on invalid input, use Vitest
|
|
297
|
+
* `schemaMatching` + `assert` with `.is()`.
|
|
242
298
|
*
|
|
243
299
|
* ### Example
|
|
244
300
|
*
|
|
245
301
|
* ```ts
|
|
246
|
-
* //
|
|
302
|
+
* // Good: Known valid constant
|
|
247
303
|
* const maxRetries = PositiveInt.orThrow(3);
|
|
248
304
|
*
|
|
249
|
-
* //
|
|
250
|
-
* const appName =
|
|
305
|
+
* // Good: App configuration that should crash on invalid values
|
|
306
|
+
* const appName = Name.orThrow("MyApp");
|
|
251
307
|
*
|
|
252
|
-
* //
|
|
308
|
+
* // Good: Instead of assert when Type error is clear enough
|
|
253
309
|
* // Context makes it obvious: count increments from non-negative value
|
|
254
310
|
* const currentCount = counts.get(id) ?? 0;
|
|
255
311
|
* const newCount = PositiveInt.orThrow(currentCount + 1);
|
|
256
312
|
*
|
|
257
|
-
* //
|
|
313
|
+
* // Good: Test setup with known valid values
|
|
258
314
|
* const testUser = User.orThrow({ name: "Alice", age: 30 });
|
|
259
315
|
*
|
|
260
|
-
* //
|
|
316
|
+
* // Avoid: User input (use `from` instead)
|
|
261
317
|
* const userAge = PositiveInt.orThrow(userInput); // Could crash!
|
|
262
318
|
*
|
|
263
|
-
* //
|
|
319
|
+
* // Better: Handle user input gracefully
|
|
264
320
|
* const ageResult = PositiveInt.from(userInput);
|
|
265
321
|
* if (!ageResult.ok) {
|
|
266
322
|
* // Handle validation error
|
|
@@ -281,16 +337,16 @@ ParentError extends TypeError = Error> extends StandardSchemaV1<Input, T> {
|
|
|
281
337
|
* ### Example
|
|
282
338
|
*
|
|
283
339
|
* ```ts
|
|
284
|
-
* //
|
|
340
|
+
* // Good: Optional user input
|
|
285
341
|
* const age = PositiveInt.orNull(userInput);
|
|
286
342
|
* if (age != null) {
|
|
287
343
|
* console.log("Valid age:", age);
|
|
288
344
|
* }
|
|
289
345
|
*
|
|
290
|
-
* //
|
|
346
|
+
* // Good: Default fallback
|
|
291
347
|
* const maxRetries = PositiveInt.orNull(config.retries) ?? 3;
|
|
292
348
|
*
|
|
293
|
-
* //
|
|
349
|
+
* // Avoid: When you need to know why validation failed (use `from` instead)
|
|
294
350
|
* const result = PositiveInt.from(userInput);
|
|
295
351
|
* if (!result.ok) {
|
|
296
352
|
* console.error(formatPositiveError(result.error));
|
|
@@ -394,7 +450,7 @@ ParentError extends TypeError = Error> extends StandardSchemaV1<Input, T> {
|
|
|
394
450
|
/**
|
|
395
451
|
* Unique identifier for a {@link Type}.
|
|
396
452
|
*
|
|
397
|
-
* @
|
|
453
|
+
* @group Utilities
|
|
398
454
|
*/
|
|
399
455
|
export type TypeName = Capitalize<string>;
|
|
400
456
|
export interface TypeError<Name extends TypeName = TypeName> {
|
|
@@ -413,54 +469,72 @@ export interface TypeErrorWithReason<Name extends TypeName = TypeName, Reason ex
|
|
|
413
469
|
/** The detailed reason for the error, represented as a tagged union. */
|
|
414
470
|
readonly reason: Reason;
|
|
415
471
|
}
|
|
472
|
+
/**
|
|
473
|
+
* A {@link Type} with all type parameters set to `any`.
|
|
474
|
+
*
|
|
475
|
+
* @group Utilities
|
|
476
|
+
*/
|
|
416
477
|
export type AnyType = Type<any, any, any, any, any, any>;
|
|
417
478
|
/**
|
|
418
479
|
* Extracts the name from a {@link Type}.
|
|
419
480
|
*
|
|
420
|
-
* @
|
|
481
|
+
* @group Utilities
|
|
421
482
|
*/
|
|
422
483
|
export type InferName<A extends AnyType> = A extends Type<infer Name, any, any, any, any, any> ? Name : never;
|
|
423
484
|
/**
|
|
424
485
|
* Extracts the type from a {@link Type}.
|
|
425
486
|
*
|
|
426
|
-
*
|
|
487
|
+
* ### Example
|
|
488
|
+
*
|
|
489
|
+
* ```ts
|
|
490
|
+
* const User = object({
|
|
491
|
+
* name: NonEmptyTrimmedString100,
|
|
492
|
+
* age: optional(PositiveInt),
|
|
493
|
+
* });
|
|
494
|
+
*
|
|
495
|
+
* // Use interface for objects. TypeScript displays the interface name
|
|
496
|
+
* // instead of expanding all properties.
|
|
497
|
+
* interface User extends InferType<typeof User> {}
|
|
498
|
+
* ```
|
|
499
|
+
*
|
|
500
|
+
* @group Utilities
|
|
427
501
|
*/
|
|
428
502
|
export type InferType<A extends AnyType> = A extends Type<any, infer T, any, any, any, any> ? T : never;
|
|
429
503
|
/**
|
|
430
504
|
* Extracts the input type from a {@link Type}.
|
|
431
505
|
*
|
|
432
|
-
* @
|
|
506
|
+
* @group Utilities
|
|
433
507
|
*/
|
|
434
508
|
export type InferInput<A extends AnyType> = A extends Type<any, any, infer Input, any, any, any> ? Input : never;
|
|
435
509
|
/**
|
|
436
510
|
* Extracts the specific error type from a {@link Type}.
|
|
437
511
|
*
|
|
438
|
-
* @
|
|
512
|
+
* @group Utilities
|
|
439
513
|
*/
|
|
440
514
|
export type InferError<A extends AnyType> = A extends Type<any, any, any, infer Error, any, any> ? Error : never;
|
|
441
515
|
/**
|
|
442
516
|
* Extracts the parent type from a {@link Type}.
|
|
443
517
|
*
|
|
444
|
-
* @
|
|
518
|
+
* @group Utilities
|
|
445
519
|
*/
|
|
446
520
|
export type InferParent<A extends AnyType> = A extends Type<any, any, any, any, infer Parent, any> ? Parent : never;
|
|
447
521
|
/**
|
|
448
522
|
* Extracts the parent error type from a {@link Type}.
|
|
449
523
|
*
|
|
450
|
-
* @
|
|
524
|
+
* @group Utilities
|
|
451
525
|
*/
|
|
452
526
|
export type InferParentError<A extends AnyType> = A extends Type<any, any, any, any, any, infer ParentError> ? ParentError : never;
|
|
453
527
|
/**
|
|
454
528
|
* Extracts all error types from a {@link Type}.
|
|
455
529
|
*
|
|
456
|
-
* @
|
|
530
|
+
* @group Utilities
|
|
457
531
|
*/
|
|
458
532
|
export type InferErrors<T extends AnyType> = T extends Type<any, any, any, infer Error, any, infer ParentError> ? Error | ParentError : never;
|
|
459
533
|
declare const EvoluTypeSymbol: unique symbol;
|
|
460
534
|
/**
|
|
461
535
|
* Checks if the given value is an {@link Type}.
|
|
462
536
|
*
|
|
463
|
-
* @
|
|
537
|
+
* @group Utilities
|
|
464
538
|
*/
|
|
465
539
|
export declare const isType: (value: unknown) => value is AnyType;
|
|
466
540
|
/**
|
|
@@ -477,7 +551,7 @@ export declare const isType: (value: unknown) => value is AnyType;
|
|
|
477
551
|
* );
|
|
478
552
|
* ```
|
|
479
553
|
*
|
|
480
|
-
* @
|
|
554
|
+
* @group Utilities
|
|
481
555
|
*/
|
|
482
556
|
export declare const createTypeErrorFormatter: <Error extends TypeError>(format: (error: Simplify<Omit<Error, "value"> & {
|
|
483
557
|
value: string;
|
|
@@ -504,7 +578,7 @@ export type TypeErrorFormatter<Error extends TypeError> = (error: Error) => stri
|
|
|
504
578
|
* );
|
|
505
579
|
* ```
|
|
506
580
|
*
|
|
507
|
-
* @
|
|
581
|
+
* @group Base factories
|
|
508
582
|
*/
|
|
509
583
|
export declare const base: <Name extends TypeName, T, Error extends TypeError>(name: Name, fromUnknown: (value: unknown) => Result<T, Error>) => Type<Name, T, T, Error>;
|
|
510
584
|
/**
|
|
@@ -520,54 +594,59 @@ export declare const base: <Name extends TypeName, T, Error extends TypeError>(n
|
|
|
520
594
|
* createBaseTypeErrorFormatter<StringError>();
|
|
521
595
|
* ```
|
|
522
596
|
*
|
|
523
|
-
* @
|
|
597
|
+
* @group Utilities
|
|
524
598
|
*/
|
|
525
599
|
export declare const createBaseTypeErrorFormatter: <Error extends TypeError>() => TypeErrorFormatter<Error>;
|
|
526
|
-
/** @
|
|
600
|
+
/** @group Base Types */
|
|
527
601
|
export declare const Unknown: Type<"Unknown", unknown, unknown, never, unknown, never>;
|
|
528
602
|
/**
|
|
529
|
-
* @
|
|
530
|
-
* @
|
|
603
|
+
* @group Base Types
|
|
604
|
+
* @group String
|
|
531
605
|
*/
|
|
532
606
|
export declare const String: Type<"String", string, string, StringError, string, StringError>;
|
|
533
607
|
export interface StringError extends TypeError<"String"> {
|
|
534
608
|
}
|
|
535
609
|
export declare const formatStringError: TypeErrorFormatter<StringError>;
|
|
536
|
-
/** @
|
|
610
|
+
/** @group Base Types */
|
|
537
611
|
export declare const Number: Type<"Number", number, number, NumberError, number, NumberError>;
|
|
538
612
|
export interface NumberError extends TypeError<"Number"> {
|
|
539
613
|
}
|
|
540
614
|
export declare const formatNumberError: TypeErrorFormatter<NumberError>;
|
|
541
|
-
/** @
|
|
615
|
+
/** @group Base Types */
|
|
542
616
|
export declare const BigInt: Type<"BigInt", bigint, bigint, BigIntError, bigint, BigIntError>;
|
|
543
617
|
export interface BigIntError extends TypeError<"BigInt"> {
|
|
544
618
|
}
|
|
545
619
|
export declare const formatBigIntError: TypeErrorFormatter<BigIntError>;
|
|
546
|
-
/** @
|
|
620
|
+
/** @group Base Types */
|
|
547
621
|
export declare const Boolean: Type<"Boolean", boolean, boolean, BooleanError, boolean, BooleanError>;
|
|
548
622
|
export interface BooleanError extends TypeError<"Boolean"> {
|
|
549
623
|
}
|
|
550
624
|
export declare const formatBooleanError: TypeErrorFormatter<BooleanError>;
|
|
551
|
-
/** @
|
|
625
|
+
/** @group Base Types */
|
|
552
626
|
export declare const Undefined: Type<"Undefined", undefined, undefined, UndefinedError, undefined, UndefinedError>;
|
|
553
627
|
export interface UndefinedError extends TypeError<"Undefined"> {
|
|
554
628
|
}
|
|
555
629
|
export declare const formatUndefinedError: TypeErrorFormatter<UndefinedError>;
|
|
556
|
-
/** @
|
|
630
|
+
/** @group Base Types */
|
|
557
631
|
export declare const Null: Type<"Null", null, null, NullError, null, NullError>;
|
|
558
632
|
export interface NullError extends TypeError<"Null"> {
|
|
559
633
|
}
|
|
560
634
|
export declare const formatNullError: TypeErrorFormatter<NullError>;
|
|
561
|
-
/** @
|
|
635
|
+
/** @group Base Types */
|
|
562
636
|
export declare const Function: Type<"Function", Function, Function, FunctionError, Function, FunctionError>;
|
|
563
637
|
export interface FunctionError extends TypeError<"Function"> {
|
|
564
638
|
}
|
|
565
639
|
export declare const formatFunctionError: TypeErrorFormatter<FunctionError>;
|
|
566
|
-
/** @
|
|
640
|
+
/** @group Base Types */
|
|
567
641
|
export declare const Uint8Array: Type<"Uint8Array", Uint8Array<ArrayBufferLike>, Uint8Array<ArrayBufferLike>, Uint8ArrayError, Uint8Array<ArrayBufferLike>, Uint8ArrayError>;
|
|
568
642
|
export interface Uint8ArrayError extends TypeError<"Uint8Array"> {
|
|
569
643
|
}
|
|
570
644
|
export declare const formatUint8ArrayError: TypeErrorFormatter<Uint8ArrayError>;
|
|
645
|
+
/** @group Base Types */
|
|
646
|
+
export declare const ArrayBuffer: Type<"ArrayBuffer", ArrayBuffer, ArrayBuffer, ArrayBufferError, ArrayBuffer, ArrayBufferError>;
|
|
647
|
+
export interface ArrayBufferError extends TypeError<"ArrayBuffer"> {
|
|
648
|
+
}
|
|
649
|
+
export declare const formatArrayBufferError: TypeErrorFormatter<ArrayBufferError>;
|
|
571
650
|
/**
|
|
572
651
|
* `instanceof` {@link Type}.
|
|
573
652
|
*
|
|
@@ -586,7 +665,7 @@ export declare const formatUint8ArrayError: TypeErrorFormatter<Uint8ArrayError>;
|
|
|
586
665
|
* const error = UserInstance.from({}); // err
|
|
587
666
|
* ```
|
|
588
667
|
*
|
|
589
|
-
* @
|
|
668
|
+
* @group Base factories
|
|
590
669
|
*/
|
|
591
670
|
export declare const instanceOf: <T extends abstract new (...args: any) => any>(ctor: T) => InstanceOfType<T>;
|
|
592
671
|
export interface InstanceOfError extends TypeError<"InstanceOf"> {
|
|
@@ -599,7 +678,7 @@ export declare const formatInstanceOfError: TypeErrorFormatter<InstanceOfError>;
|
|
|
599
678
|
/**
|
|
600
679
|
* JavaScript Date.
|
|
601
680
|
*
|
|
602
|
-
* @
|
|
681
|
+
* @group Base Types
|
|
603
682
|
*/
|
|
604
683
|
export declare const Date: InstanceOfType<DateConstructor>;
|
|
605
684
|
/**
|
|
@@ -620,12 +699,16 @@ export declare const formatIsTypeError: TypeErrorFormatter<EvoluTypeError>;
|
|
|
620
699
|
/**
|
|
621
700
|
* Branded {@link Type}.
|
|
622
701
|
*
|
|
702
|
+
* Branding is the recommended way to define types in Evolu. Instead of using
|
|
703
|
+
* primitive types like `string` or `number` directly, wrap them with `brand` to
|
|
704
|
+
* create semantically meaningful types. See {@link Brand} for why this matters.
|
|
705
|
+
*
|
|
623
706
|
* The `brand` Type Factory takes the name of a new {@link Brand}, a parent Type
|
|
624
707
|
* to be branded, and the optional `refine` function for additional constraint.
|
|
625
708
|
*
|
|
626
709
|
* The `refine` function can be omitted if we only want to add a brand.
|
|
627
710
|
*
|
|
628
|
-
* ###
|
|
711
|
+
* ### Example
|
|
629
712
|
*
|
|
630
713
|
* A simple `CurrencyCode` Type:
|
|
631
714
|
*
|
|
@@ -744,9 +827,10 @@ export declare const formatIsTypeError: TypeErrorFormatter<EvoluTypeError>;
|
|
|
744
827
|
* );
|
|
745
828
|
* ```
|
|
746
829
|
*
|
|
747
|
-
* @
|
|
830
|
+
* @group Base factories
|
|
748
831
|
*/
|
|
749
832
|
export declare function brand<Name extends TypeName, ParentType extends AnyType, Parent = InferType<ParentType>, RefineError extends TypeError = never>(name: Name, parent: ParentType, refine: (value: Parent) => Result<Parent, RefineError>): BrandType<ParentType, Name, RefineError, InferErrors<ParentType>>;
|
|
833
|
+
/** Without refine function. */
|
|
750
834
|
export declare function brand<Name extends TypeName, ParentType extends AnyType>(name: Name, parent: ParentType): BrandType<ParentType, Name, BrandWithoutRefineError<Name, InferErrors<ParentType>>>;
|
|
751
835
|
export interface BrandType<ParentType extends AnyType, Name extends TypeName, Error extends TypeError = never, ParentError extends TypeError = never> extends Type<"Brand", InferType<ParentType> & Brand<Name>, InferInput<ParentType>, Error, InferType<ParentType>, ParentError> {
|
|
752
836
|
readonly brand: Name;
|
|
@@ -758,7 +842,7 @@ export interface BrandWithoutRefineError<Name extends TypeName, ParentError exte
|
|
|
758
842
|
/**
|
|
759
843
|
* A three-letter ISO 4217 currency code (e.g., USD, EUR).
|
|
760
844
|
*
|
|
761
|
-
* @
|
|
845
|
+
* @group String
|
|
762
846
|
*/
|
|
763
847
|
export declare const CurrencyCode: BrandType<Type<"String", string, string, StringError, string, StringError>, "CurrencyCode", CurrencyCodeError, StringError>;
|
|
764
848
|
export type CurrencyCode = typeof CurrencyCode.Type;
|
|
@@ -784,7 +868,7 @@ export declare const formatCurrencyCodeError: TypeErrorFormatter<CurrencyCodeErr
|
|
|
784
868
|
* const error = DateIso.from("10000-01-01T00:00:00.000Z"); // err
|
|
785
869
|
* ```
|
|
786
870
|
*
|
|
787
|
-
* @
|
|
871
|
+
* @group String
|
|
788
872
|
*/
|
|
789
873
|
export declare const DateIso: BrandType<Type<"String", string, string, StringError, string, StringError>, "DateIso", DateIsoError, StringError>;
|
|
790
874
|
export type DateIso = typeof DateIso.Type;
|
|
@@ -809,7 +893,18 @@ export declare const dateIsoToDate: (value: DateIso) => Date;
|
|
|
809
893
|
* );
|
|
810
894
|
* ```
|
|
811
895
|
*
|
|
812
|
-
*
|
|
896
|
+
* ### Numeric literal inference
|
|
897
|
+
*
|
|
898
|
+
* When using factories with numeric parameters (like `lessThan`, `maxLength`,
|
|
899
|
+
* `between`), use numeric literals instead of expressions. TypeScript widens
|
|
900
|
+
* expressions to `number`, losing the literal type in the brand name:
|
|
901
|
+
*
|
|
902
|
+
* ```ts
|
|
903
|
+
* lessThan(100)(Number); // Brand<"LessThan100"> ✓
|
|
904
|
+
* lessThan(100 - 1)(Number); // Brand<"LessThan" + number> ✗
|
|
905
|
+
* ```
|
|
906
|
+
*
|
|
907
|
+
* @group Utilities
|
|
813
908
|
*/
|
|
814
909
|
export type BrandFactory<Name extends TypeName, Input, RefineError extends TypeError> = <PName extends TypeName, P extends Input, PInput, PParent, PError extends TypeError = never, PParentError extends TypeError = never>(parent: Type<PName, P, PInput, PError, PParent, PParentError>) => BrandType<Type<PName, P, PInput, PError, PParent, PParentError>, Name, RefineError, PError | PParentError>;
|
|
815
910
|
/**
|
|
@@ -826,7 +921,7 @@ export type BrandFactory<Name extends TypeName, Input, RefineError extends TypeE
|
|
|
826
921
|
* type TrimmedNonEmptyString = typeof TrimmedNonEmptyString.Type;
|
|
827
922
|
* ```
|
|
828
923
|
*
|
|
829
|
-
* @
|
|
924
|
+
* @group String
|
|
830
925
|
*/
|
|
831
926
|
export declare const trimmed: BrandFactory<"Trimmed", string, TrimmedError>;
|
|
832
927
|
export interface TrimmedError extends TypeError<"Trimmed"> {
|
|
@@ -838,7 +933,7 @@ export declare const formatTrimmedError: TypeErrorFormatter<TrimmedError>;
|
|
|
838
933
|
* - Use `TrimmedString.is` to check if an unknown value is trimmed.
|
|
839
934
|
* - Use `TrimmedString.from` to check if a string is trimmed.
|
|
840
935
|
*
|
|
841
|
-
* @
|
|
936
|
+
* @group String
|
|
842
937
|
*/
|
|
843
938
|
export declare const TrimmedString: BrandType<Type<"String", string, string, StringError, string, StringError>, "Trimmed", TrimmedError, StringError>;
|
|
844
939
|
export type TrimmedString = typeof TrimmedString.Type;
|
|
@@ -846,6 +941,8 @@ export declare const trim: (value: string) => TrimmedString;
|
|
|
846
941
|
/**
|
|
847
942
|
* Minimum length.
|
|
848
943
|
*
|
|
944
|
+
* Use numeric literal, not expression. See {@link BrandFactory}.
|
|
945
|
+
*
|
|
849
946
|
* ### Example
|
|
850
947
|
*
|
|
851
948
|
* ```ts
|
|
@@ -853,8 +950,8 @@ export declare const trim: (value: string) => TrimmedString;
|
|
|
853
950
|
* const NonEmptyString = minLength(1)(String);
|
|
854
951
|
* ```
|
|
855
952
|
*
|
|
856
|
-
* @
|
|
857
|
-
* @
|
|
953
|
+
* @group String
|
|
954
|
+
* @group Array
|
|
858
955
|
*/
|
|
859
956
|
export declare const minLength: <Min extends number>(min: Min) => BrandFactory<`MinLength${Min}`, {
|
|
860
957
|
length: number;
|
|
@@ -866,6 +963,8 @@ export declare const formatMinLengthError: TypeErrorFormatter<MinLengthError<num
|
|
|
866
963
|
/**
|
|
867
964
|
* Maximum length.
|
|
868
965
|
*
|
|
966
|
+
* Use numeric literal, not expression. See {@link BrandFactory}.
|
|
967
|
+
*
|
|
869
968
|
* ### Example
|
|
870
969
|
*
|
|
871
970
|
* ```ts
|
|
@@ -873,8 +972,8 @@ export declare const formatMinLengthError: TypeErrorFormatter<MinLengthError<num
|
|
|
873
972
|
* const String100 = maxLength(100)(String);
|
|
874
973
|
* ```
|
|
875
974
|
*
|
|
876
|
-
* @
|
|
877
|
-
* @
|
|
975
|
+
* @group String
|
|
976
|
+
* @group Array
|
|
878
977
|
*/
|
|
879
978
|
export declare const maxLength: <Max extends number>(max: Max) => BrandFactory<`MaxLength${Max}`, {
|
|
880
979
|
length: number;
|
|
@@ -886,6 +985,8 @@ export declare const formatMaxLengthError: TypeErrorFormatter<MaxLengthError<num
|
|
|
886
985
|
/**
|
|
887
986
|
* Exact length.
|
|
888
987
|
*
|
|
988
|
+
* Use numeric literal, not expression. See {@link BrandFactory}.
|
|
989
|
+
*
|
|
889
990
|
* ### Example
|
|
890
991
|
*
|
|
891
992
|
* ```ts
|
|
@@ -893,8 +994,8 @@ export declare const formatMaxLengthError: TypeErrorFormatter<MaxLengthError<num
|
|
|
893
994
|
* const Length1String = length(1)(String);
|
|
894
995
|
* ```
|
|
895
996
|
*
|
|
896
|
-
* @
|
|
897
|
-
* @
|
|
997
|
+
* @group String
|
|
998
|
+
* @group Array
|
|
898
999
|
*/
|
|
899
1000
|
export declare const length: <Exact extends number>(exact: Exact) => BrandFactory<`Length${Exact}`, {
|
|
900
1001
|
length: number;
|
|
@@ -903,34 +1004,34 @@ export interface LengthError<Exact extends number = number> extends TypeError<"L
|
|
|
903
1004
|
readonly exact: Exact;
|
|
904
1005
|
}
|
|
905
1006
|
export declare const formatLengthError: TypeErrorFormatter<LengthError<number>>;
|
|
906
|
-
/** @
|
|
1007
|
+
/** @group String */
|
|
907
1008
|
export declare const NonEmptyString: BrandType<Type<"String", string, string, StringError, string, StringError>, "MinLength1", MinLengthError<1>, StringError>;
|
|
908
1009
|
export type NonEmptyString = typeof NonEmptyString.Type;
|
|
909
|
-
/** @
|
|
1010
|
+
/** @group String */
|
|
910
1011
|
export declare const String100: BrandType<Type<"String", string, string, StringError, string, StringError>, "MaxLength100", MaxLengthError<100>, StringError>;
|
|
911
1012
|
export type String100 = typeof String100.Type;
|
|
912
|
-
/** @
|
|
1013
|
+
/** @group String */
|
|
913
1014
|
export declare const String1000: BrandType<Type<"String", string, string, StringError, string, StringError>, "MaxLength1000", MaxLengthError<1000>, StringError>;
|
|
914
1015
|
export type String1000 = typeof String1000.Type;
|
|
915
|
-
/** @
|
|
1016
|
+
/** @group String */
|
|
916
1017
|
export declare const NonEmptyString100: BrandType<Type<"Brand", string & Brand<"MaxLength100">, string, MaxLengthError<100>, string, StringError>, "MinLength1", MinLengthError<1>, StringError | MaxLengthError<100>>;
|
|
917
1018
|
export type NonEmptyString100 = typeof NonEmptyString100.Type;
|
|
918
|
-
/** @
|
|
1019
|
+
/** @group String */
|
|
919
1020
|
export declare const NonEmptyString1000: BrandType<Type<"Brand", string & Brand<"MaxLength1000">, string, MaxLengthError<1000>, string, StringError>, "MinLength1", MinLengthError<1>, StringError | MaxLengthError<1000>>;
|
|
920
1021
|
export type NonEmptyString1000 = typeof NonEmptyString1000.Type;
|
|
921
|
-
/** @
|
|
1022
|
+
/** @group String */
|
|
922
1023
|
export declare const NonEmptyTrimmedString: BrandType<Type<"Brand", string & Brand<"Trimmed">, string, TrimmedError, string, StringError>, "MinLength1", MinLengthError<1>, StringError | TrimmedError>;
|
|
923
1024
|
export type NonEmptyTrimmedString = typeof NonEmptyTrimmedString.Type;
|
|
924
|
-
/** @
|
|
1025
|
+
/** @group String */
|
|
925
1026
|
export declare const TrimmedString100: BrandType<Type<"Brand", string & Brand<"Trimmed">, string, TrimmedError, string, StringError>, "MaxLength100", MaxLengthError<100>, StringError | TrimmedError>;
|
|
926
1027
|
export type TrimmedString100 = typeof TrimmedString100.Type;
|
|
927
|
-
/** @
|
|
1028
|
+
/** @group String */
|
|
928
1029
|
export declare const TrimmedString1000: BrandType<Type<"Brand", string & Brand<"Trimmed">, string, TrimmedError, string, StringError>, "MaxLength1000", MaxLengthError<1000>, StringError | TrimmedError>;
|
|
929
1030
|
export type TrimmedString1000 = typeof TrimmedString1000.Type;
|
|
930
|
-
/** @
|
|
1031
|
+
/** @group String */
|
|
931
1032
|
export declare const NonEmptyTrimmedString100: BrandType<Type<"Brand", string & Brand<"Trimmed"> & Brand<"MaxLength100">, string, MaxLengthError<100>, string & Brand<"Trimmed">, StringError | TrimmedError>, "MinLength1", MinLengthError<1>, StringError | TrimmedError | MaxLengthError<100>>;
|
|
932
1033
|
export type NonEmptyTrimmedString100 = typeof NonEmptyTrimmedString100.Type;
|
|
933
|
-
/** @
|
|
1034
|
+
/** @group String */
|
|
934
1035
|
export declare const NonEmptyTrimmedString1000: BrandType<Type<"Brand", string & Brand<"Trimmed"> & Brand<"MaxLength1000">, string, MaxLengthError<1000>, string & Brand<"Trimmed">, StringError | TrimmedError>, "MinLength1", MinLengthError<1>, StringError | TrimmedError | MaxLengthError<1000>>;
|
|
935
1036
|
export type NonEmptyTrimmedString1000 = typeof NonEmptyTrimmedString1000.Type;
|
|
936
1037
|
/**
|
|
@@ -940,7 +1041,7 @@ export type NonEmptyTrimmedString1000 = typeof NonEmptyTrimmedString1000.Type;
|
|
|
940
1041
|
* safely on the user's device using cryptographically secure random number
|
|
941
1042
|
* generation, ensuring it remains private and unique.
|
|
942
1043
|
*
|
|
943
|
-
* @
|
|
1044
|
+
* @group String
|
|
944
1045
|
*/
|
|
945
1046
|
export declare const Mnemonic: BrandType<BrandType<Type<"Brand", string & Brand<"Trimmed">, string, TrimmedError, string, StringError>, "MinLength1", MinLengthError<1>, StringError | TrimmedError>, "Mnemonic", MnemonicError, StringError | TrimmedError | MinLengthError<1>>;
|
|
946
1047
|
export type Mnemonic = typeof Mnemonic.Type;
|
|
@@ -956,7 +1057,7 @@ export declare const formatMnemonicError: TypeErrorFormatter<MnemonicError>;
|
|
|
956
1057
|
* const Alphanumeric = regex("Alphanumeric", /^[a-z0-9]+$/i)(String);
|
|
957
1058
|
* ```
|
|
958
1059
|
*
|
|
959
|
-
* @
|
|
1060
|
+
* @group String
|
|
960
1061
|
*/
|
|
961
1062
|
export declare const regex: <Name extends TypeName>(name: Name, pattern: RegExp) => BrandFactory<Name, string, RegexError<Name>>;
|
|
962
1063
|
export interface RegexError<Name extends TypeName = TypeName> extends TypeError<"Regex"> {
|
|
@@ -989,7 +1090,7 @@ export declare const formatRegexError: TypeErrorFormatter<RegexError<Capitalize<
|
|
|
989
1090
|
* }
|
|
990
1091
|
* ```
|
|
991
1092
|
*
|
|
992
|
-
* @
|
|
1093
|
+
* @group String
|
|
993
1094
|
*/
|
|
994
1095
|
export declare const UrlSafeString: BrandType<Type<"String", string, string, StringError, string, StringError>, "UrlSafeString", RegexError<"UrlSafeString">, StringError>;
|
|
995
1096
|
export type UrlSafeString = typeof UrlSafeString.Type;
|
|
@@ -1000,7 +1101,7 @@ export type UrlSafeStringError = typeof UrlSafeString.Error;
|
|
|
1000
1101
|
* Encode with {@link uint8ArrayToBase64Url}, decode with
|
|
1001
1102
|
* {@link base64UrlToUint8Array}.
|
|
1002
1103
|
*
|
|
1003
|
-
* @
|
|
1104
|
+
* @group String
|
|
1004
1105
|
*/
|
|
1005
1106
|
export declare const Base64Url: BrandType<Type<"String", string, string, StringError, string, StringError>, "Base64Url", Base64UrlError, StringError>;
|
|
1006
1107
|
export type Base64Url = typeof Base64Url.Type;
|
|
@@ -1012,30 +1113,31 @@ export declare const uint8ArrayToBase64Url: (bytes: Uint8Array) => Base64Url;
|
|
|
1012
1113
|
/** Decodes a {@link Base64Url} string to a Uint8Array. */
|
|
1013
1114
|
export declare const base64UrlToUint8Array: (str: Base64Url) => Uint8Array;
|
|
1014
1115
|
/**
|
|
1015
|
-
*
|
|
1116
|
+
* Alphanumeric string for naming in file systems, URLs, and identifiers.
|
|
1016
1117
|
*
|
|
1017
1118
|
* Uses the same safe alphabet as {@link UrlSafeString} (letters, digits, `-`,
|
|
1018
|
-
* `_`).
|
|
1119
|
+
* `_`).
|
|
1019
1120
|
*
|
|
1020
1121
|
* The string must be between 1 and 64 characters.
|
|
1021
1122
|
*
|
|
1022
1123
|
* ### Example
|
|
1023
1124
|
*
|
|
1024
1125
|
* ```ts
|
|
1025
|
-
* const result =
|
|
1126
|
+
* const result = Name.from("data-report-123");
|
|
1026
1127
|
* if (result.ok) {
|
|
1027
|
-
* console.log("Valid
|
|
1128
|
+
* console.log("Valid Name string:", result.value);
|
|
1028
1129
|
* } else {
|
|
1029
|
-
* console.error("Invalid
|
|
1130
|
+
* console.error("Invalid Name string:", result.error);
|
|
1030
1131
|
* }
|
|
1031
1132
|
* ```
|
|
1032
1133
|
*
|
|
1033
|
-
* @
|
|
1134
|
+
* @group String
|
|
1034
1135
|
*/
|
|
1035
|
-
export declare const
|
|
1036
|
-
export type
|
|
1037
|
-
export interface
|
|
1136
|
+
export declare const Name: BrandType<BrandType<Type<"String", string, string, StringError, string, StringError>, "UrlSafeString", RegexError<"UrlSafeString">, StringError>, "Name", NameError, StringError | RegexError<"UrlSafeString">>;
|
|
1137
|
+
export type Name = typeof Name.Type;
|
|
1138
|
+
export interface NameError extends TypeError<"Name"> {
|
|
1038
1139
|
}
|
|
1140
|
+
export declare const testName: string & Brand<"UrlSafeString"> & Brand<"Name">;
|
|
1039
1141
|
/**
|
|
1040
1142
|
* Trimmed string between 8 and 64 characters, branded as `SimplePassword`.
|
|
1041
1143
|
*
|
|
@@ -1058,7 +1160,7 @@ export interface SimpleNameError extends TypeError<"SimpleName"> {
|
|
|
1058
1160
|
* // |> brand("SimplePassword", %)
|
|
1059
1161
|
* ```
|
|
1060
1162
|
*
|
|
1061
|
-
* @
|
|
1163
|
+
* @group String
|
|
1062
1164
|
*/
|
|
1063
1165
|
export declare const SimplePassword: BrandType<BrandType<Type<"Brand", string & Brand<"Trimmed"> & Brand<"MaxLength64">, string, MaxLengthError<64>, string & Brand<"Trimmed">, StringError | TrimmedError>, "MinLength8", MinLengthError<8>, StringError | TrimmedError | MaxLengthError<64>>, "SimplePassword", BrandWithoutRefineError<"SimplePassword", StringError | TrimmedError | MinLengthError<8> | MaxLengthError<64>>, never>;
|
|
1064
1166
|
export type SimplePassword = typeof SimplePassword.Type;
|
|
@@ -1080,14 +1182,14 @@ export declare const formatSimplePasswordError: (formatTypeError: TypeErrorForma
|
|
|
1080
1182
|
* copied (logs, URLs, exports); only use it when you explicitly want insertion
|
|
1081
1183
|
* locality for very large write‑heavy tables and accept timestamp exposure.
|
|
1082
1184
|
*
|
|
1083
|
-
*
|
|
1185
|
+
* ## Future
|
|
1084
1186
|
*
|
|
1085
1187
|
* A possible hybrid masked‑time approach (`timestamp ^ H(cluster_id, timestamp
|
|
1086
1188
|
*
|
|
1087
1189
|
* > > N)`) could provide locality without exposing raw creation time. See
|
|
1088
1190
|
* > > https://brooker.co.za/blog/2025/10/22/uuidv7.html
|
|
1089
1191
|
*
|
|
1090
|
-
* @
|
|
1192
|
+
* @group String
|
|
1091
1193
|
*/
|
|
1092
1194
|
export declare const Id: BrandType<Type<"String", string, string, StringError, string, StringError>, "Id", IdError, StringError>;
|
|
1093
1195
|
export type Id = typeof Id.Type;
|
|
@@ -1139,7 +1241,7 @@ export declare const createId: <B extends string = never>(deps: RandomBytesDep)
|
|
|
1139
1241
|
* external string from the generated {@link Id}. If you need to preserve the
|
|
1140
1242
|
* original external ID, store it in a separate column.
|
|
1141
1243
|
*
|
|
1142
|
-
* @
|
|
1244
|
+
* @group String
|
|
1143
1245
|
*/
|
|
1144
1246
|
export declare const createIdFromString: <B extends string = never>(value: string) => [B] extends [never] ? Id : Id & Brand<B>;
|
|
1145
1247
|
/**
|
|
@@ -1172,7 +1274,7 @@ export declare const createIdAsUuidv7: <B extends string = never>(deps: RandomBy
|
|
|
1172
1274
|
* type TodoId = typeof TodoId.Type;
|
|
1173
1275
|
* ```
|
|
1174
1276
|
*
|
|
1175
|
-
* @
|
|
1277
|
+
* @group String
|
|
1176
1278
|
*/
|
|
1177
1279
|
export declare const id: <Table extends TypeName>(table: Table) => TableId<Table>;
|
|
1178
1280
|
export interface TableId<Table extends TypeName> extends Type<"Id", string & Brand<"Id"> & Brand<Table>, string, TableIdError<Table>, string, StringError> {
|
|
@@ -1183,7 +1285,7 @@ export interface TableIdError<Table extends TypeName = TypeName> extends TypeErr
|
|
|
1183
1285
|
}
|
|
1184
1286
|
export declare const formatTableIdError: TypeErrorFormatter<TableIdError<Capitalize<string>>>;
|
|
1185
1287
|
/** Binary representation of an {@link Id}. */
|
|
1186
|
-
export declare const IdBytes: BrandType<BrandType<Type<"Uint8Array", Uint8Array<ArrayBufferLike>, Uint8Array<ArrayBufferLike>, Uint8ArrayError, Uint8Array<ArrayBufferLike>, Uint8ArrayError>, "Length16", LengthError<16>, Uint8ArrayError>, "IdBytes", BrandWithoutRefineError<"IdBytes", LengthError<16
|
|
1288
|
+
export declare const IdBytes: BrandType<BrandType<Type<"Uint8Array", Uint8Array<ArrayBufferLike>, Uint8Array<ArrayBufferLike>, Uint8ArrayError, Uint8Array<ArrayBufferLike>, Uint8ArrayError>, "Length16", LengthError<16>, Uint8ArrayError>, "IdBytes", BrandWithoutRefineError<"IdBytes", Uint8ArrayError | LengthError<16>>, never>;
|
|
1187
1289
|
export type IdBytes = typeof IdBytes.Type;
|
|
1188
1290
|
export declare const idBytesTypeValueLength: NonNegativeInt;
|
|
1189
1291
|
export declare const idToIdBytes: (id: Id) => IdBytes;
|
|
@@ -1200,7 +1302,7 @@ export declare const idBytesToId: (idBytes: IdBytes) => Id;
|
|
|
1200
1302
|
* const errorResult = PositiveNumber.from(-5); // err
|
|
1201
1303
|
* ```
|
|
1202
1304
|
*
|
|
1203
|
-
* @
|
|
1305
|
+
* @group Number
|
|
1204
1306
|
*/
|
|
1205
1307
|
export declare const positive: BrandFactory<"Positive", number, PositiveError>;
|
|
1206
1308
|
export interface PositiveError extends TypeError<"Positive"> {
|
|
@@ -1215,7 +1317,7 @@ export declare const formatPositiveError: TypeErrorFormatter<PositiveError>;
|
|
|
1215
1317
|
* const NegativeNumber = negative(Number);
|
|
1216
1318
|
* ```
|
|
1217
1319
|
*
|
|
1218
|
-
* @
|
|
1320
|
+
* @group Number
|
|
1219
1321
|
*/
|
|
1220
1322
|
export declare const negative: BrandFactory<"Negative", number, NegativeError>;
|
|
1221
1323
|
export interface NegativeError extends TypeError<"Negative"> {
|
|
@@ -1230,7 +1332,7 @@ export declare const formatNegativeError: TypeErrorFormatter<NegativeError>;
|
|
|
1230
1332
|
* const NonPositiveNumber = nonPositive(Number);
|
|
1231
1333
|
* ```
|
|
1232
1334
|
*
|
|
1233
|
-
* @
|
|
1335
|
+
* @group Number
|
|
1234
1336
|
*/
|
|
1235
1337
|
export declare const nonPositive: BrandFactory<"NonPositive", number, NonPositiveError>;
|
|
1236
1338
|
export interface NonPositiveError extends TypeError<"NonPositive"> {
|
|
@@ -1245,7 +1347,7 @@ export declare const formatNonPositiveError: TypeErrorFormatter<NonPositiveError
|
|
|
1245
1347
|
* const NonNegativeNumber = nonNegative(Number);
|
|
1246
1348
|
* ```
|
|
1247
1349
|
*
|
|
1248
|
-
* @
|
|
1350
|
+
* @group Number
|
|
1249
1351
|
*/
|
|
1250
1352
|
export declare const nonNegative: BrandFactory<"NonNegative", number, NonNegativeError>;
|
|
1251
1353
|
export interface NonNegativeError extends TypeError<"NonNegative"> {
|
|
@@ -1254,87 +1356,81 @@ export declare const formatNonNegativeError: TypeErrorFormatter<NonNegativeError
|
|
|
1254
1356
|
/**
|
|
1255
1357
|
* Non-negative number (≥ 0).
|
|
1256
1358
|
*
|
|
1257
|
-
* @
|
|
1359
|
+
* @group Number
|
|
1258
1360
|
*/
|
|
1259
1361
|
export declare const NonNegativeNumber: BrandType<Type<"Number", number, number, NumberError, number, NumberError>, "NonNegative", NonNegativeError, NumberError>;
|
|
1260
1362
|
export type NonNegativeNumber = typeof NonNegativeNumber.Type;
|
|
1261
1363
|
/**
|
|
1262
1364
|
* Positive number (> 0).
|
|
1263
1365
|
*
|
|
1264
|
-
* @
|
|
1366
|
+
* @group Number
|
|
1265
1367
|
*/
|
|
1266
|
-
export declare const PositiveNumber: BrandType<Type<"Brand", number & Brand<"NonNegative">, number, NonNegativeError, number, NumberError>, "Positive", PositiveError,
|
|
1368
|
+
export declare const PositiveNumber: BrandType<Type<"Brand", number & Brand<"NonNegative">, number, NonNegativeError, number, NumberError>, "Positive", PositiveError, NumberError | NonNegativeError>;
|
|
1267
1369
|
export type PositiveNumber = typeof PositiveNumber.Type;
|
|
1268
1370
|
/**
|
|
1269
1371
|
* Non-positive number (≤ 0).
|
|
1270
1372
|
*
|
|
1271
|
-
* @
|
|
1373
|
+
* @group Number
|
|
1272
1374
|
*/
|
|
1273
1375
|
export declare const NonPositiveNumber: BrandType<Type<"Number", number, number, NumberError, number, NumberError>, "NonPositive", NonPositiveError, NumberError>;
|
|
1274
1376
|
export type NonPositiveNumber = typeof NonPositiveNumber.Type;
|
|
1275
1377
|
/**
|
|
1276
1378
|
* Negative number (< 0).
|
|
1277
1379
|
*
|
|
1278
|
-
* @
|
|
1380
|
+
* @group Number
|
|
1279
1381
|
*/
|
|
1280
1382
|
export declare const NegativeNumber: BrandType<Type<"Brand", number & Brand<"NonPositive">, number, NonPositiveError, number, NumberError>, "Negative", NegativeError, NumberError | NonPositiveError>;
|
|
1281
1383
|
export type NegativeNumber = typeof NegativeNumber.Type;
|
|
1282
|
-
/**
|
|
1283
|
-
* Integer within the safe range of JavaScript numbers.
|
|
1284
|
-
*
|
|
1285
|
-
* ### Example
|
|
1286
|
-
*
|
|
1287
|
-
* ```ts
|
|
1288
|
-
* const Int = int(Number);
|
|
1289
|
-
* ```
|
|
1290
|
-
*
|
|
1291
|
-
* @category Number
|
|
1292
|
-
*/
|
|
1293
|
-
export declare const int: BrandFactory<"Int", number, IntError>;
|
|
1294
1384
|
export interface IntError extends TypeError<"Int"> {
|
|
1295
1385
|
}
|
|
1296
1386
|
export declare const formatIntError: TypeErrorFormatter<IntError>;
|
|
1297
1387
|
/**
|
|
1298
1388
|
* Integer within the safe range of JavaScript numbers.
|
|
1299
1389
|
*
|
|
1300
|
-
* @
|
|
1390
|
+
* @group Number
|
|
1301
1391
|
*/
|
|
1302
1392
|
export declare const Int: BrandType<Type<"Number", number, number, NumberError, number, NumberError>, "Int", IntError, NumberError>;
|
|
1303
1393
|
export type Int = typeof Int.Type;
|
|
1304
1394
|
/**
|
|
1305
1395
|
* Non-negative integer (≥ 0).
|
|
1306
1396
|
*
|
|
1307
|
-
* @
|
|
1397
|
+
* @group Number
|
|
1308
1398
|
*/
|
|
1309
|
-
export declare const NonNegativeInt: BrandType<Type<"Brand", number & Brand<"Int">, number, IntError, number, NumberError>, "NonNegative", NonNegativeError,
|
|
1399
|
+
export declare const NonNegativeInt: BrandType<Type<"Brand", number & Brand<"Int">, number, IntError, number, NumberError>, "NonNegative", NonNegativeError, NumberError | IntError>;
|
|
1310
1400
|
export type NonNegativeInt = typeof NonNegativeInt.Type;
|
|
1401
|
+
/** Minimum {@link NonNegativeInt} value (0). */
|
|
1402
|
+
export declare const zeroNonNegativeInt: number & Brand<"Int"> & Brand<"NonNegative">;
|
|
1311
1403
|
/**
|
|
1312
1404
|
* Positive integer (> 0).
|
|
1313
1405
|
*
|
|
1314
|
-
* @
|
|
1406
|
+
* @group Number
|
|
1315
1407
|
*/
|
|
1316
|
-
export declare const PositiveInt: BrandType<Type<"Brand", number & Brand<"Int"> & Brand<"NonNegative">, number, NonNegativeError, number & Brand<"Int">,
|
|
1408
|
+
export declare const PositiveInt: BrandType<Type<"Brand", number & Brand<"Int"> & Brand<"NonNegative">, number, NonNegativeError, number & Brand<"Int">, NumberError | IntError>, "Positive", PositiveError, NumberError | NonNegativeError | IntError>;
|
|
1317
1409
|
export type PositiveInt = typeof PositiveInt.Type;
|
|
1318
|
-
/**
|
|
1410
|
+
/** {@link PositiveInt} value 1. */
|
|
1411
|
+
export declare const onePositiveInt: number & Brand<"Int"> & Brand<"NonNegative"> & Brand<"Positive">;
|
|
1412
|
+
/** Maximum {@link PositiveInt} value (MAX_SAFE_INTEGER). */
|
|
1319
1413
|
export declare const maxPositiveInt: number & Brand<"Int"> & Brand<"NonNegative"> & Brand<"Positive">;
|
|
1320
1414
|
/**
|
|
1321
1415
|
* Non-positive integer (≤ 0).
|
|
1322
1416
|
*
|
|
1323
|
-
* @
|
|
1417
|
+
* @group Number
|
|
1324
1418
|
*/
|
|
1325
|
-
export declare const NonPositiveInt: BrandType<Type<"Brand", number & Brand<"Int">, number, IntError, number, NumberError>, "NonPositive", NonPositiveError,
|
|
1419
|
+
export declare const NonPositiveInt: BrandType<Type<"Brand", number & Brand<"Int">, number, IntError, number, NumberError>, "NonPositive", NonPositiveError, NumberError | IntError>;
|
|
1326
1420
|
export type NonPositiveInt = typeof NonPositiveInt.Type;
|
|
1327
1421
|
/**
|
|
1328
1422
|
* Negative integer (< 0).
|
|
1329
1423
|
*
|
|
1330
|
-
* @
|
|
1424
|
+
* @group Number
|
|
1331
1425
|
*/
|
|
1332
|
-
export declare const NegativeInt: BrandType<Type<"Brand", number & Brand<"Int"> & Brand<"NonPositive">, number, NonPositiveError, number & Brand<"Int">,
|
|
1426
|
+
export declare const NegativeInt: BrandType<Type<"Brand", number & Brand<"Int"> & Brand<"NonPositive">, number, NonPositiveError, number & Brand<"Int">, NumberError | IntError>, "Negative", NegativeError, NumberError | IntError | NonPositiveError>;
|
|
1333
1427
|
export type NegativeInt = typeof NegativeInt.Type;
|
|
1334
1428
|
/**
|
|
1335
1429
|
* Number greater than a specified value.
|
|
1336
1430
|
*
|
|
1337
|
-
* @
|
|
1431
|
+
* Use numeric literal, not expression. See {@link BrandFactory}.
|
|
1432
|
+
*
|
|
1433
|
+
* @group Number
|
|
1338
1434
|
*/
|
|
1339
1435
|
export declare const greaterThan: <Min extends number>(min: Min) => BrandFactory<`GreaterThan${Min}`, number, GreaterThanError<Min>>;
|
|
1340
1436
|
export interface GreaterThanError<Min extends number = number> extends TypeError<"GreaterThan"> {
|
|
@@ -1344,7 +1440,9 @@ export declare const formatGreaterThanError: TypeErrorFormatter<GreaterThanError
|
|
|
1344
1440
|
/**
|
|
1345
1441
|
* Number less than a specified value.
|
|
1346
1442
|
*
|
|
1347
|
-
* @
|
|
1443
|
+
* Use numeric literal, not expression. See {@link BrandFactory}.
|
|
1444
|
+
*
|
|
1445
|
+
* @group Number
|
|
1348
1446
|
*/
|
|
1349
1447
|
export declare const lessThan: <Max extends number>(max: Max) => BrandFactory<`LessThan${Max}`, number, LessThanError<Max>>;
|
|
1350
1448
|
export interface LessThanError<Max extends number = number> extends TypeError<"LessThan"> {
|
|
@@ -1354,7 +1452,9 @@ export declare const formatLessThanError: TypeErrorFormatter<LessThanError<numbe
|
|
|
1354
1452
|
/**
|
|
1355
1453
|
* Number ≥ a specified value.
|
|
1356
1454
|
*
|
|
1357
|
-
* @
|
|
1455
|
+
* Use numeric literal, not expression. See {@link BrandFactory}.
|
|
1456
|
+
*
|
|
1457
|
+
* @group Number
|
|
1358
1458
|
*/
|
|
1359
1459
|
export declare const greaterThanOrEqualTo: <Min extends number>(min: Min) => BrandFactory<`GreaterThanOrEqualTo${Min}`, number, GreaterThanOrEqualToError<Min>>;
|
|
1360
1460
|
export interface GreaterThanOrEqualToError<Min extends number = number> extends TypeError<"GreaterThanOrEqualTo"> {
|
|
@@ -1364,7 +1464,9 @@ export declare const formatGreaterThanOrEqualToError: TypeErrorFormatter<Greater
|
|
|
1364
1464
|
/**
|
|
1365
1465
|
* Number ≤ a specified value.
|
|
1366
1466
|
*
|
|
1367
|
-
* @
|
|
1467
|
+
* Use numeric literal, not expression. See {@link BrandFactory}.
|
|
1468
|
+
*
|
|
1469
|
+
* @group Number
|
|
1368
1470
|
*/
|
|
1369
1471
|
export declare const lessThanOrEqualTo: <Max extends number>(max: Max) => BrandFactory<`LessThanOrEqualTo${Max}`, number, LessThanOrEqualToError<Max>>;
|
|
1370
1472
|
export interface LessThanOrEqualToError<Max extends number = number> extends TypeError<"LessThanOrEqualTo"> {
|
|
@@ -1374,19 +1476,19 @@ export declare const formatLessThanOrEqualToError: TypeErrorFormatter<LessThanOr
|
|
|
1374
1476
|
/**
|
|
1375
1477
|
* Number that is not NaN.
|
|
1376
1478
|
*
|
|
1377
|
-
* @
|
|
1479
|
+
* @group Number
|
|
1378
1480
|
*/
|
|
1379
1481
|
export declare const nonNaN: BrandFactory<"NonNaN", number, NonNaNError>;
|
|
1380
1482
|
export interface NonNaNError extends TypeError<"NonNaN"> {
|
|
1381
1483
|
}
|
|
1382
1484
|
export declare const formatNonNaNError: TypeErrorFormatter<NonNaNError>;
|
|
1383
|
-
/** @
|
|
1485
|
+
/** @group Number */
|
|
1384
1486
|
export declare const NonNaNNumber: BrandType<Type<"Number", number, number, NumberError, number, NumberError>, "NonNaN", NonNaNError, NumberError>;
|
|
1385
1487
|
export type NonNaNNumber = typeof NonNaNNumber.Type;
|
|
1386
1488
|
/**
|
|
1387
1489
|
* Finite number.
|
|
1388
1490
|
*
|
|
1389
|
-
* @
|
|
1491
|
+
* @group Number
|
|
1390
1492
|
*/
|
|
1391
1493
|
export declare const finite: BrandFactory<"Finite", number, FiniteError>;
|
|
1392
1494
|
export interface FiniteError extends TypeError<"Finite"> {
|
|
@@ -1403,14 +1505,16 @@ export declare const formatFiniteError: TypeErrorFormatter<FiniteError>;
|
|
|
1403
1505
|
* finite (e.g., `Infinity`, `-Infinity`, or `NaN`). Using `FiniteNumber` helps
|
|
1404
1506
|
* prevent these unexpected behaviors when working with JSON serialization.
|
|
1405
1507
|
*
|
|
1406
|
-
* @
|
|
1508
|
+
* @group Number
|
|
1407
1509
|
*/
|
|
1408
1510
|
export declare const FiniteNumber: BrandType<Type<"Number", number, number, NumberError, number, NumberError>, "Finite", FiniteError, NumberError>;
|
|
1409
1511
|
export type FiniteNumber = typeof FiniteNumber.Type;
|
|
1410
1512
|
/**
|
|
1411
1513
|
* Number that is a multiple of a divisor.
|
|
1412
1514
|
*
|
|
1413
|
-
* @
|
|
1515
|
+
* Use numeric literal, not expression. See {@link BrandFactory}.
|
|
1516
|
+
*
|
|
1517
|
+
* @group Number
|
|
1414
1518
|
*/
|
|
1415
1519
|
export declare const multipleOf: <Divisor extends number>(divisor: Divisor) => BrandFactory<`MultipleOf${Divisor}`, number, MultipleOfError<Divisor>>;
|
|
1416
1520
|
export interface MultipleOfError<Divisor extends number = number> extends TypeError<"MultipleOf"> {
|
|
@@ -1420,6 +1524,8 @@ export declare const formatMultipleOfError: TypeErrorFormatter<MultipleOfError<n
|
|
|
1420
1524
|
/**
|
|
1421
1525
|
* Number within a range, inclusive.
|
|
1422
1526
|
*
|
|
1527
|
+
* Use numeric literal, not expression. See {@link BrandFactory}.
|
|
1528
|
+
*
|
|
1423
1529
|
* ### Example
|
|
1424
1530
|
*
|
|
1425
1531
|
* ```ts
|
|
@@ -1428,7 +1534,7 @@ export declare const formatMultipleOfError: TypeErrorFormatter<MultipleOfError<n
|
|
|
1428
1534
|
* const errorResult = Between1And10.from(11); // err
|
|
1429
1535
|
* ```
|
|
1430
1536
|
*
|
|
1431
|
-
* @
|
|
1537
|
+
* @group Number
|
|
1432
1538
|
*/
|
|
1433
1539
|
export declare const between: <Min extends number, Max extends number>(min: Min, max: Max) => BrandFactory<`Between${Min}-${Max}`, number, BetweenError<Min, Max>>;
|
|
1434
1540
|
export interface BetweenError<Min extends number = number, Max extends number = number> extends TypeError<"Between"> {
|
|
@@ -1451,7 +1557,7 @@ export declare const formatBetweenError: TypeErrorFormatter<BetweenError<number,
|
|
|
1451
1557
|
*
|
|
1452
1558
|
* TODO: Add JsonValue
|
|
1453
1559
|
*
|
|
1454
|
-
* @
|
|
1560
|
+
* @group Base factories
|
|
1455
1561
|
*/
|
|
1456
1562
|
export declare const literal: <T extends Literal>(expected: T) => LiteralType<T>;
|
|
1457
1563
|
export interface LiteralType<T extends Literal> extends Type<"Literal", T, WidenLiteral<T>, LiteralError<T>> {
|
|
@@ -1464,7 +1570,7 @@ export declare const formatLiteralError: TypeErrorFormatter<LiteralError<Literal
|
|
|
1464
1570
|
/**
|
|
1465
1571
|
* Array of a specific {@link Type}.
|
|
1466
1572
|
*
|
|
1467
|
-
*
|
|
1573
|
+
* ### Example
|
|
1468
1574
|
*
|
|
1469
1575
|
* ```ts
|
|
1470
1576
|
* const NumberArray = array(Number);
|
|
@@ -1473,8 +1579,8 @@ export declare const formatLiteralError: TypeErrorFormatter<LiteralError<Literal
|
|
|
1473
1579
|
* const result2 = NumberArray.from(["a", "b"]); // err(...)
|
|
1474
1580
|
* ```
|
|
1475
1581
|
*
|
|
1476
|
-
* @
|
|
1477
|
-
* @
|
|
1582
|
+
* @group Base factories
|
|
1583
|
+
* @group Array
|
|
1478
1584
|
*/
|
|
1479
1585
|
export declare const array: <ElementType extends AnyType>(element: ElementType) => ArrayType<ElementType>;
|
|
1480
1586
|
/** ArrayType extends Type with an additional `element` property for reflection. */
|
|
@@ -1502,7 +1608,7 @@ export declare const formatArrayError: <Error extends TypeError>(formatTypeError
|
|
|
1502
1608
|
* const result2 = NumberSet.from(new Set(["a", "b"])); // err(...)
|
|
1503
1609
|
* ```
|
|
1504
1610
|
*
|
|
1505
|
-
* @
|
|
1611
|
+
* @group Base factories
|
|
1506
1612
|
*/
|
|
1507
1613
|
export declare const set: <ElementType extends AnyType>(element: ElementType) => SetType<ElementType>;
|
|
1508
1614
|
/** SetType extends Type with an additional `element` property for reflection. */
|
|
@@ -1542,8 +1648,8 @@ export declare const formatSetError: <Error extends TypeError>(formatTypeError:
|
|
|
1542
1648
|
* StringToNumberRecord.from({ a: "x", b: 2 });
|
|
1543
1649
|
* ```
|
|
1544
1650
|
*
|
|
1545
|
-
* @
|
|
1546
|
-
* @
|
|
1651
|
+
* @group Base factories
|
|
1652
|
+
* @group Object
|
|
1547
1653
|
*/
|
|
1548
1654
|
export declare const record: <KeyName extends TypeName, KeyT extends string, KeyInput extends string, KeyError extends TypeError, KeyParent extends string, KeyParentError extends TypeError, Value extends AnyType>(keyType: Type<KeyName, KeyT, KeyInput, KeyError, KeyParent, KeyParentError>, valueType: Value) => RecordType<KeyName, KeyT, KeyInput, KeyError, KeyParent, KeyParentError, Value>;
|
|
1549
1655
|
/**
|
|
@@ -1585,22 +1691,22 @@ export declare const formatRecordError: <Error extends TypeError>(formatTypeErro
|
|
|
1585
1691
|
*
|
|
1586
1692
|
* https://www.typescriptlang.org/docs/handbook/2/objects.html#index-signatures
|
|
1587
1693
|
*
|
|
1588
|
-
* ###
|
|
1694
|
+
* ### Example
|
|
1589
1695
|
*
|
|
1590
|
-
*
|
|
1696
|
+
* ## Basic Object Validation
|
|
1591
1697
|
*
|
|
1592
1698
|
* ```ts
|
|
1593
1699
|
* const User = object({
|
|
1594
1700
|
* name: NonEmptyTrimmedString,
|
|
1595
1701
|
* age: PositiveNumber,
|
|
1596
1702
|
* });
|
|
1597
|
-
*
|
|
1703
|
+
* interface User extends InferType<typeof User> {}
|
|
1598
1704
|
*
|
|
1599
1705
|
* const result = User.from({ name: "John", age: 30 }); // ok({ name: "John", age: 30 })
|
|
1600
1706
|
* const error = User.from({ name: "John", age: -5 }); // err
|
|
1601
1707
|
* ```
|
|
1602
1708
|
*
|
|
1603
|
-
*
|
|
1709
|
+
* ## Optional Properties
|
|
1604
1710
|
*
|
|
1605
1711
|
* In this example the `age` property is marked as optional using
|
|
1606
1712
|
* {@link optional}.
|
|
@@ -1610,10 +1716,10 @@ export declare const formatRecordError: <Error extends TypeError>(formatTypeErro
|
|
|
1610
1716
|
* name: NonEmptyString, // Required
|
|
1611
1717
|
* age: optional(PositiveNumber), // Optional
|
|
1612
1718
|
* });
|
|
1613
|
-
*
|
|
1719
|
+
* interface User extends InferType<typeof User> {}
|
|
1614
1720
|
* ```
|
|
1615
1721
|
*
|
|
1616
|
-
*
|
|
1722
|
+
* ## Allowing Additional Properties
|
|
1617
1723
|
*
|
|
1618
1724
|
* ```ts
|
|
1619
1725
|
* const UserWithAnyExtraProperties = object(
|
|
@@ -1632,7 +1738,7 @@ export declare const formatRecordError: <Error extends TypeError>(formatTypeErro
|
|
|
1632
1738
|
* });
|
|
1633
1739
|
* ```
|
|
1634
1740
|
*
|
|
1635
|
-
*
|
|
1741
|
+
* ## Combining Fixed and Flexible Properties
|
|
1636
1742
|
*
|
|
1637
1743
|
* ```ts
|
|
1638
1744
|
* const NumberDictionary = object(
|
|
@@ -1669,10 +1775,11 @@ export declare const formatRecordError: <Error extends TypeError>(formatTypeErro
|
|
|
1669
1775
|
* );
|
|
1670
1776
|
* ```
|
|
1671
1777
|
*
|
|
1672
|
-
* @
|
|
1673
|
-
* @
|
|
1778
|
+
* @group Base factories
|
|
1779
|
+
* @group Object
|
|
1674
1780
|
*/
|
|
1675
1781
|
export declare function object<Props extends Record<string, AnyType>>(props: Props): ObjectType<Props>;
|
|
1782
|
+
/** With additional record properties. */
|
|
1676
1783
|
export declare function object<Props extends Record<string, AnyType>, KeyName extends TypeName, KeyT extends string, KeyInput extends string, KeyError extends TypeError, KeyParent extends string, KeyParentError extends TypeError, Value extends AnyType>(props: Props, record: RecordType<KeyName, KeyT, KeyInput, KeyError, KeyParent, KeyParentError, Value>): ObjectWithRecordType<Props, KeyName, KeyT, KeyInput, KeyError, KeyParent, KeyParentError, Value>;
|
|
1677
1784
|
/**
|
|
1678
1785
|
* ObjectType extends {@link Type} with an additional `props` property for
|
|
@@ -1718,7 +1825,7 @@ export interface ObjectError<PropsErrors extends Record<string, TypeError> = Rec
|
|
|
1718
1825
|
* Merge Error and ParentError into one ObjectError so tooltips and error
|
|
1719
1826
|
* messages are easier to read.
|
|
1720
1827
|
*
|
|
1721
|
-
* @
|
|
1828
|
+
* @group Utilities
|
|
1722
1829
|
*/
|
|
1723
1830
|
export type MergeObjectTypeErrors<T extends ObjectType<any>> = T extends ObjectType<infer Props> ? ObjectError<{
|
|
1724
1831
|
[K in keyof Props]: InferErrors<Props[K]>;
|
|
@@ -1752,6 +1859,170 @@ export interface ObjectWithRecordError<PropsErrors extends Record<string, TypeEr
|
|
|
1752
1859
|
}> {
|
|
1753
1860
|
}
|
|
1754
1861
|
export declare const formatObjectWithRecordError: <Error extends TypeError>(formatTypeError: TypeErrorFormatter<Error>) => TypeErrorFormatter<ObjectWithRecordError<Record<string, Error>, Error, Error>>;
|
|
1862
|
+
/**
|
|
1863
|
+
* Base interface for objects with a discriminant `type` property.
|
|
1864
|
+
*
|
|
1865
|
+
* This enables
|
|
1866
|
+
* {@link https://www.typescriptlang.org/docs/handbook/2/narrowing.html#discriminated-unions | discriminated unions}
|
|
1867
|
+
* (also known as tagged unions) — a pattern where TypeScript uses a literal
|
|
1868
|
+
* `type` field to narrow union types automatically.
|
|
1869
|
+
*
|
|
1870
|
+
* ## Why Discriminated Unions?
|
|
1871
|
+
*
|
|
1872
|
+
* Discriminated unions model states that are **mutually exclusive**. Instead of
|
|
1873
|
+
* optional fields and boolean flags that can combine into invalid
|
|
1874
|
+
* configurations, each variant is a distinct type. This makes illegal states
|
|
1875
|
+
* unrepresentable — invalid combinations cannot exist, so bugs cannot create
|
|
1876
|
+
* them.
|
|
1877
|
+
*
|
|
1878
|
+
* Benefits:
|
|
1879
|
+
*
|
|
1880
|
+
* - **Self-documenting** — Union cases immediately show all possible states
|
|
1881
|
+
* - **Compile-time safety** — TypeScript enforces handling all cases
|
|
1882
|
+
* - **Refactoring-friendly** — Adding a new state breaks code that doesn't handle
|
|
1883
|
+
* it
|
|
1884
|
+
*
|
|
1885
|
+
* ### Example
|
|
1886
|
+
*
|
|
1887
|
+
* ```ts
|
|
1888
|
+
* // Bad: optional fields allow invalid states (no contact info at all)
|
|
1889
|
+
* interface Contact {
|
|
1890
|
+
* readonly email?: Email;
|
|
1891
|
+
* readonly phone?: Phone;
|
|
1892
|
+
* }
|
|
1893
|
+
*
|
|
1894
|
+
* // Good: discriminated union makes "at least one" explicit
|
|
1895
|
+
* interface EmailOnly extends Typed<"EmailOnly"> {
|
|
1896
|
+
* readonly email: Email;
|
|
1897
|
+
* }
|
|
1898
|
+
* interface PhoneOnly extends Typed<"PhoneOnly"> {
|
|
1899
|
+
* readonly phone: Phone;
|
|
1900
|
+
* }
|
|
1901
|
+
* interface EmailAndPhone extends Typed<"EmailAndPhone"> {
|
|
1902
|
+
* readonly email: Email;
|
|
1903
|
+
* readonly phone: Phone;
|
|
1904
|
+
* }
|
|
1905
|
+
*
|
|
1906
|
+
* type ContactInfo = EmailOnly | PhoneOnly | EmailAndPhone;
|
|
1907
|
+
* ```
|
|
1908
|
+
*
|
|
1909
|
+
* ```ts
|
|
1910
|
+
* interface Pending extends Typed<"Pending"> {
|
|
1911
|
+
* readonly createdAt: DateIso;
|
|
1912
|
+
* }
|
|
1913
|
+
* interface Shipped extends Typed<"Shipped"> {
|
|
1914
|
+
* readonly trackingNumber: TrackingNumber;
|
|
1915
|
+
* }
|
|
1916
|
+
* interface Delivered extends Typed<"Delivered"> {
|
|
1917
|
+
* readonly deliveredAt: DateIso;
|
|
1918
|
+
* }
|
|
1919
|
+
* interface Cancelled extends Typed<"Cancelled"> {
|
|
1920
|
+
* readonly reason: CancellationReason;
|
|
1921
|
+
* }
|
|
1922
|
+
*
|
|
1923
|
+
* type OrderState = Pending | Shipped | Delivered | Cancelled;
|
|
1924
|
+
*
|
|
1925
|
+
* // TypeScript enforces exhaustiveness via return type
|
|
1926
|
+
* const getStatusMessage = (state: OrderState): string => {
|
|
1927
|
+
* switch (state.type) {
|
|
1928
|
+
* case "Pending":
|
|
1929
|
+
* return "Order placed";
|
|
1930
|
+
* case "Shipped":
|
|
1931
|
+
* return `Shipped: ${state.trackingNumber}`;
|
|
1932
|
+
* case "Delivered":
|
|
1933
|
+
* return `Delivered on ${state.deliveredAt.toLocaleDateString()}`;
|
|
1934
|
+
* case "Cancelled":
|
|
1935
|
+
* return `Cancelled: ${state.reason}`;
|
|
1936
|
+
* }
|
|
1937
|
+
* };
|
|
1938
|
+
*
|
|
1939
|
+
* // For void functions, use exhaustiveCheck to ensure all cases are handled
|
|
1940
|
+
* const logState = (state: OrderState): void => {
|
|
1941
|
+
* switch (state.type) {
|
|
1942
|
+
* case "Pending":
|
|
1943
|
+
* console.log("Order placed");
|
|
1944
|
+
* break;
|
|
1945
|
+
* case "Shipped":
|
|
1946
|
+
* console.log(`Shipped: ${state.trackingNumber}`);
|
|
1947
|
+
* break;
|
|
1948
|
+
* case "Delivered":
|
|
1949
|
+
* console.log(
|
|
1950
|
+
* `Delivered on ${state.deliveredAt.toLocaleDateString()}`,
|
|
1951
|
+
* );
|
|
1952
|
+
* break;
|
|
1953
|
+
* case "Cancelled":
|
|
1954
|
+
* console.log(`Cancelled: ${state.reason}`);
|
|
1955
|
+
* break;
|
|
1956
|
+
* default:
|
|
1957
|
+
* exhaustiveCheck(state);
|
|
1958
|
+
* }
|
|
1959
|
+
* };
|
|
1960
|
+
* ```
|
|
1961
|
+
*
|
|
1962
|
+
* ## Why `type` (and not e.g. `_tag`)?
|
|
1963
|
+
*
|
|
1964
|
+
* Underscore-prefixing is meant to avoid clashing with domain properties, but
|
|
1965
|
+
* proper discriminated union design means the discriminant IS the domain
|
|
1966
|
+
* concept — there's no clash to avoid. The `type` prop name also aligns with
|
|
1967
|
+
* {@link Type}'s name. If an entity has a meaningful "type" (like product
|
|
1968
|
+
* category), model it as the discriminant itself:
|
|
1969
|
+
*
|
|
1970
|
+
* ```ts
|
|
1971
|
+
* interface Electronics extends Typed<"Electronics"> {
|
|
1972
|
+
* voltage: Voltage;
|
|
1973
|
+
* }
|
|
1974
|
+
* interface Clothing extends Typed<"Clothing"> {
|
|
1975
|
+
* size: Size;
|
|
1976
|
+
* }
|
|
1977
|
+
* type Product = Electronics | Clothing;
|
|
1978
|
+
* ```
|
|
1979
|
+
*
|
|
1980
|
+
* @see {@link exhaustiveCheck} to ensure all cases are handled in void functions.
|
|
1981
|
+
* @see {@link typed} for runtime-validated typed objects.
|
|
1982
|
+
*/
|
|
1983
|
+
export interface Typed<T extends TypeName> {
|
|
1984
|
+
readonly type: T;
|
|
1985
|
+
}
|
|
1986
|
+
/**
|
|
1987
|
+
* Creates a runtime-validated typed object with a `type` discriminant.
|
|
1988
|
+
*
|
|
1989
|
+
* ### Example
|
|
1990
|
+
*
|
|
1991
|
+
* ```ts
|
|
1992
|
+
* const Card = typed("Card", {
|
|
1993
|
+
* cardNumber: CardNumber,
|
|
1994
|
+
* expiry: DateIso,
|
|
1995
|
+
* });
|
|
1996
|
+
*
|
|
1997
|
+
* const Cash = typed("Cash", {
|
|
1998
|
+
* currency: NonEmptyTrimmedString,
|
|
1999
|
+
* });
|
|
2000
|
+
*
|
|
2001
|
+
* const Payment = union(Card, Cash);
|
|
2002
|
+
* type Payment = typeof Payment.Type;
|
|
2003
|
+
*
|
|
2004
|
+
* const result = Payment.fromUnknown(data);
|
|
2005
|
+
* if (result.ok) {
|
|
2006
|
+
* switch (result.value.type) {
|
|
2007
|
+
* case "Card":
|
|
2008
|
+
* console.log(result.value.cardNumber);
|
|
2009
|
+
* break;
|
|
2010
|
+
* case "Cash":
|
|
2011
|
+
* console.log(result.value.currency);
|
|
2012
|
+
* break;
|
|
2013
|
+
* }
|
|
2014
|
+
* }
|
|
2015
|
+
* ```
|
|
2016
|
+
*
|
|
2017
|
+
* @see {@link Typed} for type-only discrimination.
|
|
2018
|
+
*/
|
|
2019
|
+
export declare function typed<Tag extends TypeName>(tag: Tag): TypedType<Tag>;
|
|
2020
|
+
/** With additional properties. */
|
|
2021
|
+
export declare function typed<Tag extends TypeName, Props extends Record<string, AnyType>>(tag: Tag, props: Props): TypedType<Tag, Props>;
|
|
2022
|
+
/** Return type of {@link typed}. */
|
|
2023
|
+
export type TypedType<Tag extends TypeName, Props extends Record<string, AnyType> = Record<never, never>> = ObjectType<{
|
|
2024
|
+
type: LiteralType<Tag>;
|
|
2025
|
+
} & Props>;
|
|
1755
2026
|
/**
|
|
1756
2027
|
* Union {@link Type}.
|
|
1757
2028
|
*
|
|
@@ -1774,9 +2045,10 @@ export declare const formatObjectWithRecordError: <Error extends TypeError>(form
|
|
|
1774
2045
|
* const result3 = StringOrNumber.from(42); // ok(42)
|
|
1775
2046
|
* ```
|
|
1776
2047
|
*
|
|
1777
|
-
* @
|
|
2048
|
+
* @group Base factories
|
|
1778
2049
|
*/
|
|
1779
2050
|
export declare function union<Members extends [AnyType, AnyType, ...ReadonlyArray<AnyType>]>(...members: Members): UnionType<Members>;
|
|
2051
|
+
/** With literal values. */
|
|
1780
2052
|
export declare function union<Literals extends [Literal, Literal, ...ReadonlyArray<Literal>]>(...literals: Literals): UnionType<{
|
|
1781
2053
|
[K in keyof Literals]: LiteralType<Literals[K]>;
|
|
1782
2054
|
}>;
|
|
@@ -1792,6 +2064,99 @@ export interface UnionError<E extends TypeError = TypeError> extends TypeError<"
|
|
|
1792
2064
|
}
|
|
1793
2065
|
export declare const formatUnionError: <Error extends TypeError>(formatTypeError: TypeErrorFormatter<Error>) => TypeErrorFormatter<UnionError<Error>>;
|
|
1794
2066
|
export declare const isUnionType: (t: AnyType) => t is UnionType<[AnyType, ...ReadonlyArray<AnyType>]>;
|
|
2067
|
+
/**
|
|
2068
|
+
* Creates a {@link Type} for {@link Result} values.
|
|
2069
|
+
*
|
|
2070
|
+
* Use for validating serialized Results from storage, APIs, or message passing.
|
|
2071
|
+
*
|
|
2072
|
+
* ### Example
|
|
2073
|
+
*
|
|
2074
|
+
* ```ts
|
|
2075
|
+
* const SyncResponse = result(
|
|
2076
|
+
* object({ timestamp: NonNegativeInt }),
|
|
2077
|
+
* typed("SyncError", { message: String }),
|
|
2078
|
+
* );
|
|
2079
|
+
*
|
|
2080
|
+
* // Validate response from worker or API
|
|
2081
|
+
* const validated = SyncResponse.from(JSON.parse(message));
|
|
2082
|
+
* if (!validated.ok) return validated; // validation error
|
|
2083
|
+
* // validated.value is Result<{ timestamp }, SyncError>
|
|
2084
|
+
* ```
|
|
2085
|
+
*
|
|
2086
|
+
* @group Composite factories
|
|
2087
|
+
*/
|
|
2088
|
+
export declare const result: <OkType extends AnyType, ErrType extends AnyType>(okType: OkType, errType: ErrType) => UnionType<[ObjectType<{
|
|
2089
|
+
ok: LiteralType<true>;
|
|
2090
|
+
value: OkType;
|
|
2091
|
+
}>, ObjectType<{
|
|
2092
|
+
ok: LiteralType<false>;
|
|
2093
|
+
error: ErrType;
|
|
2094
|
+
}>]>;
|
|
2095
|
+
/**
|
|
2096
|
+
* A {@link result} type for `Result<unknown, unknown>`.
|
|
2097
|
+
*
|
|
2098
|
+
* Useful for serializing Results where the value and error types are unknown.
|
|
2099
|
+
*
|
|
2100
|
+
* @group Composite factories
|
|
2101
|
+
*/
|
|
2102
|
+
export declare const UnknownResult: UnionType<[ObjectType<{
|
|
2103
|
+
ok: LiteralType<true>;
|
|
2104
|
+
value: Type<"Unknown", unknown, unknown, never, unknown, never>;
|
|
2105
|
+
}>, ObjectType<{
|
|
2106
|
+
ok: LiteralType<false>;
|
|
2107
|
+
error: Type<"Unknown", unknown, unknown, never, unknown, never>;
|
|
2108
|
+
}>]>;
|
|
2109
|
+
export type UnknownResult = typeof UnknownResult.Type;
|
|
2110
|
+
/**
|
|
2111
|
+
* Creates a {@link Type} for {@link NextResult} with three outcomes.
|
|
2112
|
+
*
|
|
2113
|
+
* Validates results where the producer responds with:
|
|
2114
|
+
*
|
|
2115
|
+
* - `Ok<A>` — produced a value
|
|
2116
|
+
* - `Err<Done<D>>` — completed normally with a done value
|
|
2117
|
+
* - `Err<E>` — failed with an error
|
|
2118
|
+
*
|
|
2119
|
+
* ### Example
|
|
2120
|
+
*
|
|
2121
|
+
* ```ts
|
|
2122
|
+
* const MyNextResult = nextResult(Item, MyError, Summary);
|
|
2123
|
+
*
|
|
2124
|
+
* const validated = MyNextResult.fromUnknown(data);
|
|
2125
|
+
* if (!validated.ok) return validated;
|
|
2126
|
+
*
|
|
2127
|
+
* const result = validated.value;
|
|
2128
|
+
* if (result.ok) {
|
|
2129
|
+
* console.log(result.value);
|
|
2130
|
+
* } else if (result.error.type === "Done") {
|
|
2131
|
+
* console.log("Done:", result.error.done);
|
|
2132
|
+
* } else {
|
|
2133
|
+
* console.error(result.error);
|
|
2134
|
+
* }
|
|
2135
|
+
* ```
|
|
2136
|
+
*
|
|
2137
|
+
* @group Composite factories
|
|
2138
|
+
*/
|
|
2139
|
+
export declare const nextResult: <ValueType extends AnyType, ErrorType extends AnyType, DoneType extends AnyType>(valueType: ValueType, errorType: ErrorType, doneType: DoneType) => ReturnType<typeof result<ValueType, UnionType<[ErrorType, TypedType<"Done", {
|
|
2140
|
+
done: DoneType;
|
|
2141
|
+
}>]>>>;
|
|
2142
|
+
/**
|
|
2143
|
+
* A {@link nextResult} type for `NextResult<unknown, unknown, unknown>`.
|
|
2144
|
+
*
|
|
2145
|
+
* Useful for checking if a value is a {@link NextResult} via
|
|
2146
|
+
* `UnknownNextResult.is(value)`.
|
|
2147
|
+
*
|
|
2148
|
+
* @group Composite factories
|
|
2149
|
+
*/
|
|
2150
|
+
export declare const UnknownNextResult: UnionType<[ObjectType<{
|
|
2151
|
+
ok: LiteralType<true>;
|
|
2152
|
+
value: Type<"Unknown", unknown, unknown, never, unknown, never>;
|
|
2153
|
+
}>, ObjectType<{
|
|
2154
|
+
ok: LiteralType<false>;
|
|
2155
|
+
error: UnionType<[Type<"Unknown", unknown, unknown, never, unknown, never>, TypedType<"Done", {
|
|
2156
|
+
done: Type<"Unknown", unknown, unknown, never, unknown, never>;
|
|
2157
|
+
}>]>;
|
|
2158
|
+
}>]>;
|
|
2159
|
+
export type UnknownNextResult = typeof UnknownNextResult.Type;
|
|
1795
2160
|
/**
|
|
1796
2161
|
* Recursive {@link Type}.
|
|
1797
2162
|
*
|
|
@@ -1825,7 +2190,7 @@ export declare const isUnionType: (t: AnyType) => t is UnionType<[AnyType, ...Re
|
|
|
1825
2190
|
* );
|
|
1826
2191
|
* ```
|
|
1827
2192
|
*
|
|
1828
|
-
* @
|
|
2193
|
+
* @group Base factories
|
|
1829
2194
|
*/
|
|
1830
2195
|
export declare const recursive: <ParentType extends AnyType>(create: () => ParentType) => RecursiveType<ParentType>;
|
|
1831
2196
|
export interface RecursiveType<ParentType extends AnyType> extends Type<"Recursive", InferType<ParentType>, InferInput<ParentType>, InferError<ParentType>, InferParent<ParentType>, InferParentError<ParentType>> {
|
|
@@ -1843,7 +2208,7 @@ export interface RecursiveType<ParentType extends AnyType> extends Type<"Recursi
|
|
|
1843
2208
|
* NullOrString.from(42); // err(...)
|
|
1844
2209
|
* ```
|
|
1845
2210
|
*
|
|
1846
|
-
* @
|
|
2211
|
+
* @group Base factories
|
|
1847
2212
|
*/
|
|
1848
2213
|
export declare const nullOr: <T extends AnyType>(type: T) => UnionType<[typeof Null, T]>;
|
|
1849
2214
|
/**
|
|
@@ -1858,7 +2223,7 @@ export declare const nullOr: <T extends AnyType>(type: T) => UnionType<[typeof N
|
|
|
1858
2223
|
* UndefinedOrString.from(42); // err(...)
|
|
1859
2224
|
* ```
|
|
1860
2225
|
*
|
|
1861
|
-
* @
|
|
2226
|
+
* @group Base factories
|
|
1862
2227
|
*/
|
|
1863
2228
|
export declare const undefinedOr: <T extends AnyType>(type: T) => UnionType<[typeof Undefined, T]>;
|
|
1864
2229
|
/**
|
|
@@ -1877,7 +2242,7 @@ export declare const undefinedOr: <T extends AnyType>(type: T) => UnionType<[typ
|
|
|
1877
2242
|
* NullishOrString.from(42); // err(...)
|
|
1878
2243
|
* ```
|
|
1879
2244
|
*
|
|
1880
|
-
* @
|
|
2245
|
+
* @group Base factories
|
|
1881
2246
|
*/
|
|
1882
2247
|
export declare const nullishOr: <T extends AnyType>(type: T) => UnionType<[typeof Undefined, typeof Null, T]>;
|
|
1883
2248
|
/**
|
|
@@ -1894,7 +2259,7 @@ export declare const nullishOr: <T extends AnyType>(type: T) => UnionType<[typeo
|
|
|
1894
2259
|
* const error = NameAndAge.from(["Alice", -10]); // err
|
|
1895
2260
|
* ```
|
|
1896
2261
|
*
|
|
1897
|
-
* @
|
|
2262
|
+
* @group Base factories
|
|
1898
2263
|
*/
|
|
1899
2264
|
export declare const tuple: <Elements extends [AnyType, ...ReadonlyArray<AnyType>]>(...elements: Elements) => TupleType<Elements>;
|
|
1900
2265
|
/**
|
|
@@ -1939,7 +2304,7 @@ export declare const formatTupleError: <Error extends TypeError>(formatTypeError
|
|
|
1939
2304
|
*
|
|
1940
2305
|
* https://www.sqlite.org/c3ref/int64.html
|
|
1941
2306
|
*
|
|
1942
|
-
* @
|
|
2307
|
+
* @group Number
|
|
1943
2308
|
*/
|
|
1944
2309
|
export declare const Int64: BrandType<Type<"BigInt", bigint, bigint, BigIntError, bigint, BigIntError>, "Int64", Int64Error, BigIntError>;
|
|
1945
2310
|
export type Int64 = typeof Int64.Type;
|
|
@@ -1949,14 +2314,37 @@ export declare const formatInt64Error: TypeErrorFormatter<Int64Error>;
|
|
|
1949
2314
|
/**
|
|
1950
2315
|
* Stringified {@link Int64}.
|
|
1951
2316
|
*
|
|
1952
|
-
* @
|
|
2317
|
+
* @group String
|
|
1953
2318
|
*/
|
|
1954
2319
|
export declare const Int64String: BrandType<BrandType<Type<"Brand", string & Brand<"Trimmed">, string, TrimmedError, string, StringError>, "MinLength1", MinLengthError<1>, StringError | TrimmedError>, "Int64", Int64StringError, StringError | TrimmedError | MinLengthError<1>>;
|
|
1955
2320
|
export type Int64String = typeof Int64String.Type;
|
|
1956
2321
|
export interface Int64StringError extends TypeError<"Int64String"> {
|
|
1957
2322
|
}
|
|
1958
2323
|
export declare const formatInt64StringError: TypeErrorFormatter<Int64StringError>;
|
|
2324
|
+
/**
|
|
2325
|
+
* Validated JSON-compatible value.
|
|
2326
|
+
*
|
|
2327
|
+
* This is the output side of JSON data in Evolu. It uses {@link FiniteNumber}
|
|
2328
|
+
* instead of `number` because JSON numbers are expected to be finite once the
|
|
2329
|
+
* value has been parsed or validated.
|
|
2330
|
+
*
|
|
2331
|
+
* Compare with {@link JsonValueInput}, which represents caller-provided input
|
|
2332
|
+
* before validation.
|
|
2333
|
+
*/
|
|
1959
2334
|
export type JsonValue = string | FiniteNumber | boolean | null | JsonArray | JsonObject;
|
|
2335
|
+
/**
|
|
2336
|
+
* JSON-compatible input value before validation.
|
|
2337
|
+
*
|
|
2338
|
+
* This is broader than {@link JsonValue} because inputs arrive as ordinary
|
|
2339
|
+
* JavaScript values, so numbers are typed as `number` before validation can
|
|
2340
|
+
* narrow them to {@link FiniteNumber}.
|
|
2341
|
+
*
|
|
2342
|
+
* That means `JsonValueInput` can temporarily contain numbers that are lossy in
|
|
2343
|
+
* JSON serialization. For example, `JSON.stringify(NaN)` and
|
|
2344
|
+
* `JSON.stringify(Infinity)` produce `null`, and `JSON.stringify(-0)` produces
|
|
2345
|
+
* `0`. Use {@link JsonValue} when the value must already satisfy JSON numeric
|
|
2346
|
+
* constraints.
|
|
2347
|
+
*/
|
|
1960
2348
|
export type JsonValueInput = string | number | boolean | null | JsonArrayInput | JsonObjectInput;
|
|
1961
2349
|
export type JsonValueError = UnionError<StringError | BooleanError | NullError | FiniteError | NumberError | ArrayError<JsonValueError> | RecordError<StringError, JsonValueError>>;
|
|
1962
2350
|
export interface JsonObject {
|
|
@@ -1971,19 +2359,19 @@ export type JsonArrayInput = ReadonlyArray<JsonValueInput>;
|
|
|
1971
2359
|
* JSON-compatible value: string, {@link FiniteNumber}, boolean, null,
|
|
1972
2360
|
* {@link JsonArray}, or {@link JsonObject}.
|
|
1973
2361
|
*
|
|
1974
|
-
* @
|
|
2362
|
+
* @group Base Types
|
|
1975
2363
|
*/
|
|
1976
2364
|
export declare const JsonValue: RecursiveType<UnionType<[Type<"String", string, string, StringError, string, StringError>, BrandType<Type<"Number", number, number, NumberError, number, NumberError>, "Finite", FiniteError, NumberError>, Type<"Boolean", boolean, boolean, BooleanError, boolean, BooleanError>, Type<"Null", null, null, NullError, null, NullError>, ArrayType<Type<"Recursive", JsonValue, JsonValueInput, JsonValueError, JsonValueInput, JsonValueError>>, RecordType<"String", string, string, StringError, string, StringError, Type<"Recursive", JsonValue, JsonValueInput, JsonValueError, JsonValueInput, JsonValueError>>]>>;
|
|
1977
2365
|
/**
|
|
1978
2366
|
* JSON-compatible array of {@link JsonValue} elements.
|
|
1979
2367
|
*
|
|
1980
|
-
* @
|
|
2368
|
+
* @group Array
|
|
1981
2369
|
*/
|
|
1982
2370
|
export declare const JsonArray: ArrayType<RecursiveType<UnionType<[Type<"String", string, string, StringError, string, StringError>, BrandType<Type<"Number", number, number, NumberError, number, NumberError>, "Finite", FiniteError, NumberError>, Type<"Boolean", boolean, boolean, BooleanError, boolean, BooleanError>, Type<"Null", null, null, NullError, null, NullError>, ArrayType<Type<"Recursive", JsonValue, JsonValueInput, JsonValueError, JsonValueInput, JsonValueError>>, RecordType<"String", string, string, StringError, string, StringError, Type<"Recursive", JsonValue, JsonValueInput, JsonValueError, JsonValueInput, JsonValueError>>]>>>;
|
|
1983
2371
|
/**
|
|
1984
2372
|
* JSON-compatible object with string keys and {@link JsonValue} values.
|
|
1985
2373
|
*
|
|
1986
|
-
* @
|
|
2374
|
+
* @group Object
|
|
1987
2375
|
*/
|
|
1988
2376
|
export declare const JsonObject: RecordType<"String", string, string, StringError, string, StringError, RecursiveType<UnionType<[Type<"String", string, string, StringError, string, StringError>, BrandType<Type<"Number", number, number, NumberError, number, NumberError>, "Finite", FiniteError, NumberError>, Type<"Boolean", boolean, boolean, BooleanError, boolean, BooleanError>, Type<"Null", null, null, NullError, null, NullError>, ArrayType<Type<"Recursive", JsonValue, JsonValueInput, JsonValueError, JsonValueInput, JsonValueError>>, RecordType<"String", string, string, StringError, string, StringError, Type<"Recursive", JsonValue, JsonValueInput, JsonValueError, JsonValueInput, JsonValueError>>]>>>;
|
|
1989
2377
|
export declare const parseJson: (value: string) => Result<JsonValue, JsonError>;
|
|
@@ -1997,7 +2385,7 @@ export declare const parseJson: (value: string) => Result<JsonValue, JsonError>;
|
|
|
1997
2385
|
* const error = Json.from("invalid json"); // err
|
|
1998
2386
|
* ```
|
|
1999
2387
|
*
|
|
2000
|
-
* @
|
|
2388
|
+
* @group String
|
|
2001
2389
|
*/
|
|
2002
2390
|
export declare const Json: BrandType<Type<"String", string, string, StringError, string, StringError>, "Json", JsonError, StringError>;
|
|
2003
2391
|
export type Json = typeof Json.Type;
|
|
@@ -2027,7 +2415,7 @@ export declare const jsonToJsonValue: (value: Json) => JsonValue;
|
|
|
2027
2415
|
* name: NonEmptyString100,
|
|
2028
2416
|
* age: FiniteNumber,
|
|
2029
2417
|
* });
|
|
2030
|
-
*
|
|
2418
|
+
* interface Person extends InferType<typeof Person> {}
|
|
2031
2419
|
*
|
|
2032
2420
|
* const [PersonJson, personToPersonJson, personJsonToPerson] = json(
|
|
2033
2421
|
* Person,
|
|
@@ -2095,7 +2483,7 @@ export declare const isOptionalType: (x: unknown) => x is OptionalType<any>;
|
|
|
2095
2483
|
* PartialUser.from({ age: -5 });
|
|
2096
2484
|
* ```
|
|
2097
2485
|
*
|
|
2098
|
-
* @
|
|
2486
|
+
* @group Object
|
|
2099
2487
|
*/
|
|
2100
2488
|
export declare const partial: <Props extends Record<string, AnyType>>(props: Props) => ObjectType<{ [K in keyof Props]: OptionalType<Props[K]>; }>;
|
|
2101
2489
|
/**
|
|
@@ -2104,7 +2492,7 @@ export declare const partial: <Props extends Record<string, AnyType>>(props: Pro
|
|
|
2104
2492
|
* entirely, or set it to `null`, or set it to the non-null member of the
|
|
2105
2493
|
* union.
|
|
2106
2494
|
*
|
|
2107
|
-
* @
|
|
2495
|
+
* @group Object
|
|
2108
2496
|
*/
|
|
2109
2497
|
export declare const nullableToOptional: <Props extends Record<string, AnyType>>(props: Props) => ObjectType<NullableToOptionalProps<Props>>;
|
|
2110
2498
|
export type NullableToOptionalProps<Props extends Record<string, AnyType>> = {
|
|
@@ -2116,21 +2504,9 @@ export type NullTypeInMembers<Members extends [AnyType, ...Array<AnyType>]> = Me
|
|
|
2116
2504
|
/**
|
|
2117
2505
|
* Create a new `object` {@link Type} by omitting some keys.
|
|
2118
2506
|
*
|
|
2119
|
-
* @
|
|
2507
|
+
* @group Object
|
|
2120
2508
|
*/
|
|
2121
|
-
export declare
|
|
2122
|
-
export declare const maxMutationSize = 655360;
|
|
2123
|
-
/**
|
|
2124
|
-
* Evolu has to limit the maximum mutation size. Otherwise, sync couldn't use
|
|
2125
|
-
* the `maxProtocolMessageRangesSize`. The max size is 640KB in bytes, measured
|
|
2126
|
-
* via MessagePack. Evolu Protocol DbChange will be smaller thanks to various
|
|
2127
|
-
* optimizations.
|
|
2128
|
-
*/
|
|
2129
|
-
export declare const validMutationSize: <T extends AnyType>(type: T) => BrandType<T, "ValidMutationSize", ValidMutationSizeError, InferErrors<T>>;
|
|
2130
|
-
export interface ValidMutationSizeError extends TypeError<"ValidMutationSize"> {
|
|
2131
|
-
}
|
|
2132
|
-
export declare const formatValidMutationSizeError: TypeErrorFormatter<ValidMutationSizeError>;
|
|
2133
|
-
export type ValidMutationSize<Props extends Record<string, AnyType>> = BrandType<ObjectType<Props>, "ValidMutationSize", ValidMutationSizeError, InferErrors<ObjectType<Props>>>;
|
|
2509
|
+
export declare const omit: <T extends ObjectType<any>, Keys extends keyof T["props"]>(objectType: T, ...keys: ReadonlyArray<Keys>) => ObjectType<Omit<T["props"], Keys>>;
|
|
2134
2510
|
/**
|
|
2135
2511
|
* Union of all `TypeError`s defined in the `Type.ts` file, including base type
|
|
2136
2512
|
* errors (e.g., `StringError`, `NumberError`), composite type errors
|
|
@@ -2142,9 +2518,9 @@ export type ValidMutationSize<Props extends Record<string, AnyType>> = BrandType
|
|
|
2142
2518
|
* Used by {@link createFormatTypeError} to generate human-readable error
|
|
2143
2519
|
* messages.
|
|
2144
2520
|
*
|
|
2145
|
-
* @
|
|
2521
|
+
* @group Utilities
|
|
2146
2522
|
*/
|
|
2147
|
-
export type TypeErrors<ExtraErrors extends TypeError = never> = StringError | NumberError | BigIntError | BooleanError | UndefinedError | NullError | FunctionError | Uint8ArrayError | InstanceOfError | EvoluTypeError | CurrencyCodeError | DateIsoError | TrimmedError | MinLengthError | MaxLengthError | LengthError | MnemonicError | RegexError | SimplePasswordError | IdError | TableIdError | PositiveError | NegativeError | NonPositiveError | NonNegativeError | IntError | GreaterThanError | LessThanError | GreaterThanOrEqualToError | LessThanOrEqualToError | NonNaNError | FiniteError | MultipleOfError | BetweenError | LiteralError | Int64Error | Int64StringError | JsonError |
|
|
2523
|
+
export type TypeErrors<ExtraErrors extends TypeError = never> = StringError | NumberError | BigIntError | BooleanError | UndefinedError | NullError | FunctionError | Uint8ArrayError | ArrayBufferError | InstanceOfError | EvoluTypeError | CurrencyCodeError | DateIsoError | TrimmedError | MinLengthError | MaxLengthError | LengthError | MnemonicError | RegexError | SimplePasswordError | IdError | TableIdError | PositiveError | NegativeError | NonPositiveError | NonNegativeError | IntError | GreaterThanError | LessThanError | GreaterThanOrEqualToError | LessThanOrEqualToError | NonNaNError | FiniteError | MultipleOfError | BetweenError | LiteralError | Int64Error | Int64StringError | JsonError | ExtraErrors | ArrayError<TypeErrors<ExtraErrors>> | SetError<TypeErrors<ExtraErrors>> | RecordError<TypeErrors<ExtraErrors>, TypeErrors<ExtraErrors>> | ObjectError<Record<string, TypeErrors<ExtraErrors>>> | ObjectWithRecordError<Record<string, TypeErrors<ExtraErrors>>, TypeErrors<ExtraErrors>, TypeErrors<ExtraErrors>> | UnionError<TypeErrors<ExtraErrors>> | TupleError<TypeErrors<ExtraErrors>>;
|
|
2148
2524
|
/**
|
|
2149
2525
|
* Formats Evolu Type errors into user-friendly messages.
|
|
2150
2526
|
*
|
|
@@ -2223,18 +2599,20 @@ export type TypeErrors<ExtraErrors extends TypeError = never> = StringError | Nu
|
|
|
2223
2599
|
* };
|
|
2224
2600
|
* ```
|
|
2225
2601
|
*
|
|
2226
|
-
* @
|
|
2602
|
+
* @group Utilities
|
|
2227
2603
|
*/
|
|
2228
2604
|
export declare const createFormatTypeError: <ExtraErrors extends TypeError = never>(extraFormatter?: TypeErrorFormatter<ExtraErrors>) => TypeErrorFormatter<TypeErrors<ExtraErrors>>;
|
|
2229
2605
|
/**
|
|
2230
2606
|
* Converts an Evolu {@link TypeError} to Standard Schema V1 issues format.
|
|
2231
2607
|
*
|
|
2232
2608
|
* This function recursively converts Evolu's typed errors into the Standard
|
|
2233
|
-
* Schema issue format with proper path tracking for nested structures.
|
|
2609
|
+
* Schema issue format with proper path tracking for nested structures. The
|
|
2610
|
+
* `message` field contains the JSON-serialized typed error, which users can
|
|
2611
|
+
* deserialize and format using appropriate {@link TypeErrorFormatter}s.
|
|
2234
2612
|
*
|
|
2235
|
-
* @
|
|
2613
|
+
* @group Utilities
|
|
2236
2614
|
*/
|
|
2237
|
-
export declare const typeErrorToStandardSchemaIssues: <ExtraErrors extends TypeError = never>(error: TypeErrors<ExtraErrors>,
|
|
2615
|
+
export declare const typeErrorToStandardSchemaIssues: <ExtraErrors extends TypeError = never>(error: TypeErrors<ExtraErrors>, path?: ReadonlyArray<PropertyKey>) => ReadonlyArray<StandardSchemaV1.Issue>;
|
|
2238
2616
|
/** The Standard Schema interface. */
|
|
2239
2617
|
export interface StandardSchemaV1<Input = unknown, Output = Input> {
|
|
2240
2618
|
/** The Standard Schema properties. */
|