@shirudo/ddd-kit 1.0.0-rc.1 → 1.0.0-rc.2
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 +96 -143
- package/dist/index.d.ts +808 -186
- package/dist/index.js +643 -380
- package/dist/index.js.map +1 -1
- package/dist/utils.d.ts +92 -1
- package/dist/utils.js +281 -0
- package/dist/utils.js.map +1 -1
- package/package.json +69 -65
- package/dist/deep-equal-except-C8yoSk4L.d.ts +0 -57
- package/dist/result-jCwPSjFa.d.ts +0 -352
- package/dist/result.d.ts +0 -204
- package/dist/result.js +0 -298
- package/dist/result.js.map +0 -1
- package/dist/utils-array.d.ts +0 -47
- package/dist/utils-array.js +0 -242
- package/dist/utils-array.js.map +0 -1
|
@@ -1,352 +0,0 @@
|
|
|
1
|
-
type Ok<T> = {
|
|
2
|
-
ok: true;
|
|
3
|
-
value: T;
|
|
4
|
-
};
|
|
5
|
-
type Err<E> = {
|
|
6
|
-
ok: false;
|
|
7
|
-
error: E;
|
|
8
|
-
};
|
|
9
|
-
type Result<T, E> = Ok<T> | Err<E>;
|
|
10
|
-
/**
|
|
11
|
-
* Creates an Ok result with a value.
|
|
12
|
-
*
|
|
13
|
-
* @param value - The success value
|
|
14
|
-
* @returns An Ok result containing the value
|
|
15
|
-
*
|
|
16
|
-
* @example
|
|
17
|
-
* ```typescript
|
|
18
|
-
* const result = ok(42);
|
|
19
|
-
* // result is Ok<number>
|
|
20
|
-
* ```
|
|
21
|
-
*/
|
|
22
|
-
declare function ok<T>(value: T): Ok<T>;
|
|
23
|
-
/**
|
|
24
|
-
* Creates an Ok result with void (no value).
|
|
25
|
-
*
|
|
26
|
-
* @returns An Ok<void> result
|
|
27
|
-
*
|
|
28
|
-
* @example
|
|
29
|
-
* ```typescript
|
|
30
|
-
* const result = ok();
|
|
31
|
-
* // result is Ok<void>
|
|
32
|
-
* ```
|
|
33
|
-
*/
|
|
34
|
-
declare function ok(): Ok<void>;
|
|
35
|
-
/**
|
|
36
|
-
* Creates an Err result with an error.
|
|
37
|
-
*
|
|
38
|
-
* @param error - The error value
|
|
39
|
-
* @returns An Err result containing the error
|
|
40
|
-
*
|
|
41
|
-
* @example
|
|
42
|
-
* ```typescript
|
|
43
|
-
* const result = err("Something went wrong");
|
|
44
|
-
* // result is Err<string>
|
|
45
|
-
* ```
|
|
46
|
-
*/
|
|
47
|
-
declare function err<E>(error: E): Err<E>;
|
|
48
|
-
/**
|
|
49
|
-
* Creates an Err result with void (no error value).
|
|
50
|
-
*
|
|
51
|
-
* @returns An Err<void> result
|
|
52
|
-
*
|
|
53
|
-
* @example
|
|
54
|
-
* ```typescript
|
|
55
|
-
* const result = err();
|
|
56
|
-
* // result is Err<void>
|
|
57
|
-
* ```
|
|
58
|
-
*/
|
|
59
|
-
declare function err(): Err<void>;
|
|
60
|
-
/**
|
|
61
|
-
* Type guard to check if a Result is Ok.
|
|
62
|
-
* Narrows the type to Ok<T> when returning true.
|
|
63
|
-
*
|
|
64
|
-
* @param result - The result to check
|
|
65
|
-
* @returns true if the result is Ok, false otherwise
|
|
66
|
-
*
|
|
67
|
-
* @example
|
|
68
|
-
* ```typescript
|
|
69
|
-
* const result = voWithValidation(data, validator);
|
|
70
|
-
* if (isOk(result)) {
|
|
71
|
-
* // TypeScript knows result is Ok<VO<T>>
|
|
72
|
-
* console.log(result.value);
|
|
73
|
-
* }
|
|
74
|
-
* ```
|
|
75
|
-
*/
|
|
76
|
-
declare function isOk<T, E>(result: Result<T, E>): result is Ok<T>;
|
|
77
|
-
/**
|
|
78
|
-
* Type guard to check if a Result is Err.
|
|
79
|
-
* Narrows the type to Err<E> when returning true.
|
|
80
|
-
*
|
|
81
|
-
* @param result - The result to check
|
|
82
|
-
* @returns true if the result is Err, false otherwise
|
|
83
|
-
*
|
|
84
|
-
* @example
|
|
85
|
-
* ```typescript
|
|
86
|
-
* const result = voWithValidation(data, validator);
|
|
87
|
-
* if (isErr(result)) {
|
|
88
|
-
* // TypeScript knows result is Err<string>
|
|
89
|
-
* console.error(result.error);
|
|
90
|
-
* }
|
|
91
|
-
* ```
|
|
92
|
-
*/
|
|
93
|
-
declare function isErr<T, E>(result: Result<T, E>): result is Err<E>;
|
|
94
|
-
/**
|
|
95
|
-
* Chains Result operations (flatMap/bind).
|
|
96
|
-
* If the result is Ok, applies the function to the value.
|
|
97
|
-
* If Err, returns the error unchanged.
|
|
98
|
-
*
|
|
99
|
-
* @param result - The result to chain
|
|
100
|
-
* @param fn - Function that takes the Ok value and returns a new Result
|
|
101
|
-
* @returns A new Result
|
|
102
|
-
*
|
|
103
|
-
* @example
|
|
104
|
-
* ```typescript
|
|
105
|
-
* const result = validateUserId("123")
|
|
106
|
-
* .andThen(userId => validateEmail("test@example.com")
|
|
107
|
-
* .map(email => ({ id: userId, email }))
|
|
108
|
-
* );
|
|
109
|
-
* ```
|
|
110
|
-
*/
|
|
111
|
-
declare function andThen<T, E, U>(result: Result<T, E>, fn: (value: T) => Result<U, E>): Result<U, E>;
|
|
112
|
-
/**
|
|
113
|
-
* Transforms the Ok value using a function.
|
|
114
|
-
* If the result is Err, returns the error unchanged.
|
|
115
|
-
*
|
|
116
|
-
* @param result - The result to transform
|
|
117
|
-
* @param fn - Function to transform the Ok value
|
|
118
|
-
* @returns A new Result with transformed value
|
|
119
|
-
*
|
|
120
|
-
* @example
|
|
121
|
-
* ```typescript
|
|
122
|
-
* const result = ok(5);
|
|
123
|
-
* const doubled = map(result, x => x * 2); // Ok<10>
|
|
124
|
-
* ```
|
|
125
|
-
*/
|
|
126
|
-
declare function map<T, E, U>(result: Result<T, E>, fn: (value: T) => U): Result<U, E>;
|
|
127
|
-
/**
|
|
128
|
-
* Transforms the Err value using a function.
|
|
129
|
-
* If the result is Ok, returns the value unchanged.
|
|
130
|
-
*
|
|
131
|
-
* @param result - The result to transform
|
|
132
|
-
* @param fn - Function to transform the Err value
|
|
133
|
-
* @returns A new Result with transformed error
|
|
134
|
-
*
|
|
135
|
-
* @example
|
|
136
|
-
* ```typescript
|
|
137
|
-
* const result = err("not found");
|
|
138
|
-
* const mapped = mapErr(result, e => `Error: ${e}`); // Err<"Error: not found">
|
|
139
|
-
* ```
|
|
140
|
-
*/
|
|
141
|
-
declare function mapErr<T, E, F>(result: Result<T, E>, fn: (error: E) => F): Result<T, F>;
|
|
142
|
-
/**
|
|
143
|
-
* Returns the value if Ok, otherwise returns the default value.
|
|
144
|
-
*
|
|
145
|
-
* @param result - The result to unwrap
|
|
146
|
-
* @param defaultValue - Default value to return if Err
|
|
147
|
-
* @returns The Ok value or the default value
|
|
148
|
-
*
|
|
149
|
-
* @example
|
|
150
|
-
* ```typescript
|
|
151
|
-
* const result = validateUserId("123");
|
|
152
|
-
* const userId = unwrapOr(result, "default-id");
|
|
153
|
-
* ```
|
|
154
|
-
*/
|
|
155
|
-
declare function unwrapOr<T, E>(result: Result<T, E>, defaultValue: T): T;
|
|
156
|
-
/**
|
|
157
|
-
* Returns the value if Ok, otherwise computes default from error.
|
|
158
|
-
*
|
|
159
|
-
* @param result - The result to unwrap
|
|
160
|
-
* @param fn - Function to compute default from error
|
|
161
|
-
* @returns The Ok value or computed default
|
|
162
|
-
*
|
|
163
|
-
* @example
|
|
164
|
-
* ```typescript
|
|
165
|
-
* const result = validateUserId("");
|
|
166
|
-
* const userId = unwrapOrElse(result, err => `fallback-${Date.now()}`);
|
|
167
|
-
* ```
|
|
168
|
-
*/
|
|
169
|
-
declare function unwrapOrElse<T, E>(result: Result<T, E>, fn: (error: E) => T): T;
|
|
170
|
-
/**
|
|
171
|
-
* Pattern matching for Result.
|
|
172
|
-
* Applies one function if Ok, another if Err.
|
|
173
|
-
*
|
|
174
|
-
* @param result - The result to match
|
|
175
|
-
* @param onOk - Function to apply if Ok
|
|
176
|
-
* @param onErr - Function to apply if Err
|
|
177
|
-
* @returns The result of applying the appropriate function
|
|
178
|
-
*
|
|
179
|
-
* @example
|
|
180
|
-
* ```typescript
|
|
181
|
-
* const message = match(result,
|
|
182
|
-
* value => `Success: ${value}`,
|
|
183
|
-
* error => `Error: ${error}`
|
|
184
|
-
* );
|
|
185
|
-
* ```
|
|
186
|
-
*
|
|
187
|
-
* @example Using object syntax
|
|
188
|
-
* ```typescript
|
|
189
|
-
* const message = match(result, {
|
|
190
|
-
* ok: value => `Success: ${value}`,
|
|
191
|
-
* err: error => `Error: ${error}`
|
|
192
|
-
* });
|
|
193
|
-
* ```
|
|
194
|
-
*/
|
|
195
|
-
declare function match<T, E, R>(result: Result<T, E>, onOk: (value: T) => R, onErr: (error: E) => R): R;
|
|
196
|
-
declare function match<T, E, R>(result: Result<T, E>, handlers: {
|
|
197
|
-
ok: (value: T) => R;
|
|
198
|
-
err: (error: E) => R;
|
|
199
|
-
}): R;
|
|
200
|
-
/**
|
|
201
|
-
* Async pattern matching for Result.
|
|
202
|
-
* Applies one async function if Ok, another if Err.
|
|
203
|
-
* Both handlers must return Promises.
|
|
204
|
-
*
|
|
205
|
-
* @param result - The result to match
|
|
206
|
-
* @param onOk - Async function to apply if Ok
|
|
207
|
-
* @param onErr - Async function to apply if Err
|
|
208
|
-
* @returns Promise resolving to the result of applying the appropriate function
|
|
209
|
-
*
|
|
210
|
-
* @example
|
|
211
|
-
* ```typescript
|
|
212
|
-
* const message = await matchAsync(result,
|
|
213
|
-
* async (value) => `Success: ${value}`,
|
|
214
|
-
* async (error) => `Error: ${error}`
|
|
215
|
-
* );
|
|
216
|
-
* ```
|
|
217
|
-
*
|
|
218
|
-
* @example Using object syntax
|
|
219
|
-
* ```typescript
|
|
220
|
-
* const message = await matchAsync(result, {
|
|
221
|
-
* ok: async (value) => `Success: ${value}`,
|
|
222
|
-
* err: async (error) => `Error: ${error}`
|
|
223
|
-
* });
|
|
224
|
-
* ```
|
|
225
|
-
*/
|
|
226
|
-
declare function matchAsync<T, E, R>(result: Result<T, E>, onOk: (value: T) => Promise<R>, onErr: (error: E) => Promise<R>): Promise<R>;
|
|
227
|
-
declare function matchAsync<T, E, R>(result: Result<T, E>, handlers: {
|
|
228
|
-
ok: (value: T) => Promise<R>;
|
|
229
|
-
err: (error: E) => Promise<R>;
|
|
230
|
-
}): Promise<R>;
|
|
231
|
-
/**
|
|
232
|
-
* Pattern matching for Result that returns a Result.
|
|
233
|
-
* Both handlers must return a Result, allowing you to transform
|
|
234
|
-
* both the success and error cases while staying in Result context.
|
|
235
|
-
*
|
|
236
|
-
* @param result - The result to match
|
|
237
|
-
* @param handlers - Object with ok and err handlers, both returning Result
|
|
238
|
-
* @returns A new Result from the appropriate handler
|
|
239
|
-
*
|
|
240
|
-
* @example
|
|
241
|
-
* ```typescript
|
|
242
|
-
* const result = await fetchUser(id);
|
|
243
|
-
* return matchResult(result, {
|
|
244
|
-
* ok: (user) => ok(transformUser(user)),
|
|
245
|
-
* err: (error) => err(mapToAppError(error))
|
|
246
|
-
* });
|
|
247
|
-
* ```
|
|
248
|
-
*/
|
|
249
|
-
declare function matchResult<T, E, U, F>(result: Result<T, E>, handlers: {
|
|
250
|
-
ok: (value: T) => Result<U, F>;
|
|
251
|
-
err: (error: E) => Result<U, F>;
|
|
252
|
-
}): Result<U, F>;
|
|
253
|
-
declare function matchResult<T, E, U, F>(result: Result<T, E>, onOk: (value: T) => Result<U, F>, onErr: (error: E) => Result<U, F>): Result<U, F>;
|
|
254
|
-
/**
|
|
255
|
-
* Pipes a Result through multiple operations.
|
|
256
|
-
* Each function receives the previous Result and returns a new Result.
|
|
257
|
-
* Stops on first error.
|
|
258
|
-
*
|
|
259
|
-
* @param initial - The initial Result value
|
|
260
|
-
* @param fns - Array of functions that take the previous Result and return a new Result
|
|
261
|
-
* @returns The final Result after all operations
|
|
262
|
-
*
|
|
263
|
-
* @example
|
|
264
|
-
* ```typescript
|
|
265
|
-
* // Instead of nested andThen calls:
|
|
266
|
-
* andThen(
|
|
267
|
-
* updateCountryCode(code),
|
|
268
|
-
* () => andThen(updateCurrencyCode(currency), () => updateLanguageCode(lang))
|
|
269
|
-
* )
|
|
270
|
-
*
|
|
271
|
-
* // Use pipe (cleaner and more readable):
|
|
272
|
-
* pipe(
|
|
273
|
-
* updateCountryCode(code),
|
|
274
|
-
* () => updateCurrencyCode(currency),
|
|
275
|
-
* () => updateLanguageCode(lang)
|
|
276
|
-
* )
|
|
277
|
-
* ```
|
|
278
|
-
*
|
|
279
|
-
* @example With void results
|
|
280
|
-
* ```typescript
|
|
281
|
-
* setInitialData(initialData: JobConfigProps["initialData"]): Result<void, JobDomainError> {
|
|
282
|
-
* return pipe(
|
|
283
|
-
* this.updateCountryCode(initialData.countryCode),
|
|
284
|
-
* () => this.updateCurrencyCode(initialData.currencyCode),
|
|
285
|
-
* () => this.updateLanguageCode(initialData.languageCode)
|
|
286
|
-
* );
|
|
287
|
-
* }
|
|
288
|
-
* ```
|
|
289
|
-
*/
|
|
290
|
-
declare function pipe<T, E>(initial: Result<T, E>, ...fns: Array<(prev: Result<T, E>) => Result<T, E>>): Result<T, E>;
|
|
291
|
-
/**
|
|
292
|
-
* Wraps a function that may throw exceptions into a Result type.
|
|
293
|
-
* Catches any thrown exceptions and converts them to Err results.
|
|
294
|
-
*
|
|
295
|
-
* @param fn - Function that may throw exceptions
|
|
296
|
-
* @param errorMapper - Optional function to transform the caught error
|
|
297
|
-
* @returns A Result containing the function's return value or error
|
|
298
|
-
*
|
|
299
|
-
* @example
|
|
300
|
-
* ```typescript
|
|
301
|
-
* function riskyOperation(): string {
|
|
302
|
-
* if (Math.random() > 0.5) {
|
|
303
|
-
* throw new Error("Something went wrong");
|
|
304
|
-
* }
|
|
305
|
-
* return "success";
|
|
306
|
-
* }
|
|
307
|
-
*
|
|
308
|
-
* const result = tryCatch(() => riskyOperation());
|
|
309
|
-
* if (result.ok) {
|
|
310
|
-
* console.log(result.value); // "success"
|
|
311
|
-
* } else {
|
|
312
|
-
* console.error(result.error.message); // "Something went wrong"
|
|
313
|
-
* }
|
|
314
|
-
* ```
|
|
315
|
-
*
|
|
316
|
-
* @example With custom error mapper
|
|
317
|
-
* ```typescript
|
|
318
|
-
* const result = tryCatch(
|
|
319
|
-
* () => riskyOperation(),
|
|
320
|
-
* (error) => `Custom: ${error instanceof Error ? error.message : String(error)}`
|
|
321
|
-
* );
|
|
322
|
-
* ```
|
|
323
|
-
*/
|
|
324
|
-
declare function tryCatch<T, E = Error>(fn: () => T, errorMapper?: (error: unknown) => E): Result<T, E>;
|
|
325
|
-
/**
|
|
326
|
-
* Wraps an async function that may throw exceptions into a Promise<Result>.
|
|
327
|
-
* Catches any thrown exceptions and converts them to Err results.
|
|
328
|
-
*
|
|
329
|
-
* @param fn - Async function that may throw exceptions
|
|
330
|
-
* @param errorMapper - Optional function to transform the caught error
|
|
331
|
-
* @returns A Promise resolving to a Result containing the function's return value or error
|
|
332
|
-
*
|
|
333
|
-
* @example
|
|
334
|
-
* ```typescript
|
|
335
|
-
* async function riskyAsyncOperation(): Promise<string> {
|
|
336
|
-
* if (Math.random() > 0.5) {
|
|
337
|
-
* throw new Error("Something went wrong");
|
|
338
|
-
* }
|
|
339
|
-
* return "success";
|
|
340
|
-
* }
|
|
341
|
-
*
|
|
342
|
-
* const result = await tryCatchAsync(() => riskyAsyncOperation());
|
|
343
|
-
* if (result.ok) {
|
|
344
|
-
* console.log(result.value); // "success"
|
|
345
|
-
* } else {
|
|
346
|
-
* console.error(result.error.message); // "Something went wrong"
|
|
347
|
-
* }
|
|
348
|
-
* ```
|
|
349
|
-
*/
|
|
350
|
-
declare function tryCatchAsync<T, E = Error>(fn: () => Promise<T>, errorMapper?: (error: unknown) => E): Promise<Result<T, E>>;
|
|
351
|
-
|
|
352
|
-
export { type Err as E, type Ok as O, type Result as R, andThen as a, isOk as b, mapErr as c, match as d, err as e, matchAsync as f, matchResult as g, tryCatchAsync as h, isErr as i, unwrapOrElse as j, map as m, ok as o, pipe as p, tryCatch as t, unwrapOr as u };
|
package/dist/result.d.ts
DELETED
|
@@ -1,204 +0,0 @@
|
|
|
1
|
-
import { R as Result } from './result-jCwPSjFa.js';
|
|
2
|
-
export { E as Err, O as Ok, a as andThen, e as err, i as isErr, b as isOk, m as map, c as mapErr, d as match, f as matchAsync, g as matchResult, o as ok, p as pipe, t as tryCatch, h as tryCatchAsync, u as unwrapOr, j as unwrapOrElse } from './result-jCwPSjFa.js';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Base class for Result with method chaining support.
|
|
6
|
-
* Provides common methods for both Ok and Err classes.
|
|
7
|
-
*/
|
|
8
|
-
declare abstract class ResultBase<T, E> {
|
|
9
|
-
protected readonly _result: Result<T, E>;
|
|
10
|
-
protected constructor(result: Result<T, E>);
|
|
11
|
-
/**
|
|
12
|
-
* Returns the value if Ok, otherwise throws an error.
|
|
13
|
-
* If error is an Error instance, throws it directly.
|
|
14
|
-
* Otherwise, wraps the error in a new Error.
|
|
15
|
-
*
|
|
16
|
-
* @throws Error if the result is Err
|
|
17
|
-
*/
|
|
18
|
-
unwrap(): T;
|
|
19
|
-
/**
|
|
20
|
-
* Returns the value if Ok, otherwise returns the default value.
|
|
21
|
-
*/
|
|
22
|
-
unwrapOr(defaultValue: T): T;
|
|
23
|
-
/**
|
|
24
|
-
* Returns the value if Ok, otherwise computes default from error.
|
|
25
|
-
*/
|
|
26
|
-
unwrapOrElse(fn: (error: E) => T): T;
|
|
27
|
-
/**
|
|
28
|
-
* Pattern matching for Result.
|
|
29
|
-
* Applies one function if Ok, another if Err.
|
|
30
|
-
*
|
|
31
|
-
* @example
|
|
32
|
-
* ```typescript
|
|
33
|
-
* outcome.match(
|
|
34
|
-
* value => `Success: ${value}`,
|
|
35
|
-
* error => `Error: ${error}`
|
|
36
|
-
* );
|
|
37
|
-
* ```
|
|
38
|
-
*
|
|
39
|
-
* @example Using object syntax
|
|
40
|
-
* ```typescript
|
|
41
|
-
* outcome.match({
|
|
42
|
-
* ok: value => `Success: ${value}`,
|
|
43
|
-
* err: error => `Error: ${error}`
|
|
44
|
-
* });
|
|
45
|
-
* ```
|
|
46
|
-
*/
|
|
47
|
-
match<R>(onOk: (value: T) => R, onErr: (error: E) => R): R;
|
|
48
|
-
match<R>(handlers: {
|
|
49
|
-
ok: (value: T) => R;
|
|
50
|
-
err: (error: E) => R;
|
|
51
|
-
}): R;
|
|
52
|
-
/**
|
|
53
|
-
* Async pattern matching for Result.
|
|
54
|
-
* Applies one async function if Ok, another if Err.
|
|
55
|
-
*
|
|
56
|
-
* @example
|
|
57
|
-
* ```typescript
|
|
58
|
-
* await outcome.matchAsync(
|
|
59
|
-
* async (value) => `Success: ${value}`,
|
|
60
|
-
* async (error) => `Error: ${error}`
|
|
61
|
-
* );
|
|
62
|
-
* ```
|
|
63
|
-
*
|
|
64
|
-
* @example Using object syntax
|
|
65
|
-
* ```typescript
|
|
66
|
-
* await outcome.matchAsync({
|
|
67
|
-
* ok: async (value) => `Success: ${value}`,
|
|
68
|
-
* err: async (error) => `Error: ${error}`
|
|
69
|
-
* });
|
|
70
|
-
* ```
|
|
71
|
-
*/
|
|
72
|
-
matchAsync<R>(onOk: (value: T) => Promise<R>, onErr: (error: E) => Promise<R>): Promise<R>;
|
|
73
|
-
matchAsync<R>(handlers: {
|
|
74
|
-
ok: (value: T) => Promise<R>;
|
|
75
|
-
err: (error: E) => Promise<R>;
|
|
76
|
-
}): Promise<R>;
|
|
77
|
-
/**
|
|
78
|
-
* Type guard to check if the result is Ok.
|
|
79
|
-
*/
|
|
80
|
-
isOk(): this is Success<T>;
|
|
81
|
-
/**
|
|
82
|
-
* Type guard to check if the result is Err.
|
|
83
|
-
*/
|
|
84
|
-
isErr(): this is Erroneous<E>;
|
|
85
|
-
/**
|
|
86
|
-
* Gets the underlying Result value.
|
|
87
|
-
*/
|
|
88
|
-
get result(): Result<T, E>;
|
|
89
|
-
}
|
|
90
|
-
/**
|
|
91
|
-
* Class representing a successful result with method chaining support.
|
|
92
|
-
* Use this for class-based API with method chaining.
|
|
93
|
-
*
|
|
94
|
-
* @example
|
|
95
|
-
* ```typescript
|
|
96
|
-
* const okResult = Ok(1);
|
|
97
|
-
* const doubled = okResult.map(x => x * 2).unwrap(); // 2
|
|
98
|
-
*
|
|
99
|
-
* const chained = Ok(5)
|
|
100
|
-
* .andThen(x => Ok(x * 2))
|
|
101
|
-
* .map(x => x + 1)
|
|
102
|
-
* .unwrap(); // 11
|
|
103
|
-
* ```
|
|
104
|
-
*/
|
|
105
|
-
declare class Success<T> extends ResultBase<T, never> {
|
|
106
|
-
constructor(value: T);
|
|
107
|
-
/**
|
|
108
|
-
* Factory function to create an Ok instance.
|
|
109
|
-
* Can be called with or without `new`.
|
|
110
|
-
*/
|
|
111
|
-
static of<T>(value: T): Success<T>;
|
|
112
|
-
/**
|
|
113
|
-
* Chains Result operations (flatMap/bind).
|
|
114
|
-
* If the result is Ok, applies the function to the value.
|
|
115
|
-
* If Err, returns the error unchanged.
|
|
116
|
-
*/
|
|
117
|
-
andThen<U, E>(fn: (value: T) => Result<U, E>): Success<U> | Erroneous<E>;
|
|
118
|
-
/**
|
|
119
|
-
* Transforms the Ok value using a function.
|
|
120
|
-
* If the result is Err, returns the error unchanged.
|
|
121
|
-
*/
|
|
122
|
-
map<U>(fn: (value: T) => U): Success<U>;
|
|
123
|
-
/**
|
|
124
|
-
* Transforms the Err value using a function.
|
|
125
|
-
* If the result is Ok, returns the value unchanged.
|
|
126
|
-
*/
|
|
127
|
-
mapErr<F>(_fn: (error: never) => F): Success<T>;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
/**
|
|
131
|
-
* Class representing an erroneous result with method chaining support.
|
|
132
|
-
* Use this for class-based API with method chaining.
|
|
133
|
-
*
|
|
134
|
-
* @example
|
|
135
|
-
* ```typescript
|
|
136
|
-
* const errResult = Err(new Error("error"));
|
|
137
|
-
* errResult.map(x => x * 2).unwrap(); // throws Error("error")
|
|
138
|
-
*
|
|
139
|
-
* const mapped = Err("original")
|
|
140
|
-
* .mapErr(e => `Error: ${e}`)
|
|
141
|
-
* .unwrap(); // throws Error("Error: original")
|
|
142
|
-
* ```
|
|
143
|
-
*/
|
|
144
|
-
declare class Erroneous<E> extends ResultBase<never, E> {
|
|
145
|
-
constructor(error: E);
|
|
146
|
-
/**
|
|
147
|
-
* Factory function to create an Err instance.
|
|
148
|
-
* Can be called with or without `new`.
|
|
149
|
-
*/
|
|
150
|
-
static of<E>(error: E): Erroneous<E>;
|
|
151
|
-
/**
|
|
152
|
-
* Chains Result operations (flatMap/bind).
|
|
153
|
-
* If the result is Ok, applies the function to the value.
|
|
154
|
-
* If Err, returns the error unchanged.
|
|
155
|
-
*/
|
|
156
|
-
andThen<U>(_fn: (value: never) => Result<U, E>): Erroneous<E>;
|
|
157
|
-
/**
|
|
158
|
-
* Transforms the Ok value using a function.
|
|
159
|
-
* If the result is Err, returns the error unchanged.
|
|
160
|
-
*/
|
|
161
|
-
map<U>(_fn: (value: never) => U): Erroneous<E>;
|
|
162
|
-
/**
|
|
163
|
-
* Transforms the Err value using a function.
|
|
164
|
-
* If the result is Ok, returns the value unchanged.
|
|
165
|
-
*/
|
|
166
|
-
mapErr<F>(fn: (error: E) => F): Erroneous<F>;
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
/**
|
|
170
|
-
* Class-based API for Result with method chaining support.
|
|
171
|
-
* Provides an object-oriented alternative to the functional Result type.
|
|
172
|
-
* Use `Outcome.from()` to wrap an existing Result, or `Outcome.ok()` / `Outcome.err()` to create new instances.
|
|
173
|
-
*
|
|
174
|
-
* @example
|
|
175
|
-
* ```typescript
|
|
176
|
-
* // Wrap an existing Result
|
|
177
|
-
* const result = Outcome.from(ok(1));
|
|
178
|
-
* const doubled = result.map(x => x * 2).unwrap(); // 2
|
|
179
|
-
*
|
|
180
|
-
* // Create directly
|
|
181
|
-
* const outcome = Outcome.ok(42);
|
|
182
|
-
* const value = outcome.map(x => x + 1).unwrap(); // 43
|
|
183
|
-
* ```
|
|
184
|
-
*/
|
|
185
|
-
declare class Outcome<T, E> extends ResultBase<T, E> {
|
|
186
|
-
private constructor();
|
|
187
|
-
/**
|
|
188
|
-
* Creates an Outcome from an Ok value.
|
|
189
|
-
*/
|
|
190
|
-
static ok<T>(value: T): Success<T>;
|
|
191
|
-
/**
|
|
192
|
-
* Creates an Outcome from an Err value.
|
|
193
|
-
*/
|
|
194
|
-
static err<E>(error: E): Erroneous<E>;
|
|
195
|
-
/**
|
|
196
|
-
* Creates an Outcome from a Result.
|
|
197
|
-
*/
|
|
198
|
-
static from<T, E>(result: Result<T, E>): Outcome<T, E>;
|
|
199
|
-
andThen<U>(fn: (value: T) => Result<U, E>): Outcome<U, E>;
|
|
200
|
-
map<U>(fn: (value: T) => U): Outcome<U, E>;
|
|
201
|
-
mapErr<F>(fn: (error: E) => F): Outcome<T, F>;
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
export { Erroneous, Outcome, Result, Success };
|