1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | import Unit from './unit.js';
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | export declare const Variant: {
|
15 | readonly Ok: "Ok";
|
16 | readonly Err: "Err";
|
17 | };
|
18 | export type Variant = keyof typeof Variant;
|
19 | export interface OkJSON<T> {
|
20 | variant: 'Ok';
|
21 | value: T;
|
22 | }
|
23 | export interface ErrJSON<E> {
|
24 | variant: 'Err';
|
25 | error: E;
|
26 | }
|
27 | export type ResultJSON<T, E> = OkJSON<T> | ErrJSON<E>;
|
28 | declare class ResultImpl<T, E> {
|
29 | private repr;
|
30 | private constructor();
|
31 | /**
|
32 | Create an instance of {@linkcode Ok}.
|
33 |
|
34 | Note: While you *may* create the {@linkcode Result} type via normal
|
35 | JavaScript class construction, it is not recommended for the functional
|
36 | style for which the library is intended. Instead, use {@linkcode ok}.
|
37 |
|
38 | ```ts
|
39 | // Avoid:
|
40 | const aString = new Result.Ok('characters');
|
41 |
|
42 | // Prefer:
|
43 | const aString = Result.ok('characters);
|
44 | ```
|
45 |
|
46 | Note that you may explicitly pass {@linkcode Unit} to the {@linkcode Ok}
|
47 | constructor to create a `Result<Unit, E>`. However, you may *not* call the
|
48 | `Ok` constructor with `null` or `undefined` to get that result (the type
|
49 | system won't allow you to construct it that way). Instead, for convenience,
|
50 | you can simply call {@linkcode ok}, which will construct the type correctly.
|
51 | */
|
52 | static ok<T extends {}, E = never>(): Result<Unit, E>;
|
53 | |
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 | static ok<T, E = never>(value: T): Result<T, E>;
|
77 | |
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 | static err<T = never, E = never>(): Result<T, Unit>;
|
93 | |
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 | static err<T = never, E = never>(error: E): Result<T, E>;
|
111 |
|
112 | get variant(): Variant;
|
113 | |
114 |
|
115 |
|
116 |
|
117 |
|
118 | get value(): T | never;
|
119 | |
120 |
|
121 |
|
122 |
|
123 |
|
124 | get error(): E | never;
|
125 |
|
126 | get isOk(): boolean;
|
127 |
|
128 | get isErr(): boolean;
|
129 |
|
130 | map<U>(mapFn: (t: T) => U): Result<U, E>;
|
131 | /** Method variant for {@linkcode mapOr} */
|
132 | mapOr<U>(orU: U, mapFn: (t: T) => U): U;
|
133 | /** Method variant for {@linkcode mapOrElse} */
|
134 | mapOrElse<U>(orElseFn: (err: E) => U, mapFn: (t: T) => U): U;
|
135 | /** Method variant for {@linkcode match} */
|
136 | match<U>(matcher: Matcher<T, E, U>): U;
|
137 | /** Method variant for {@linkcode mapErr} */
|
138 | mapErr<F>(mapErrFn: (e: E) => F): Result<T, F>;
|
139 | /** Method variant for {@linkcode or} */
|
140 | or<F>(orResult: Result<T, F>): Result<T, F>;
|
141 | /** Method variant for {@linkcode orElse} */
|
142 | orElse<F>(orElseFn: (err: E) => Result<T, F>): Result<T, F>;
|
143 | /** Method variant for {@linkcode and} */
|
144 | and<U>(mAnd: Result<U, E>): Result<U, E>;
|
145 | /** Method variant for {@linkcode andThen} */
|
146 | andThen<U>(andThenFn: (t: T) => Result<U, E>): Result<U, E>;
|
147 | /** Method variant for {@linkcode unwrapOr} */
|
148 | unwrapOr<U = T>(defaultValue: U): T | U;
|
149 | /** Method variant for {@linkcode unwrapOrElse} */
|
150 | unwrapOrElse<U>(elseFn: (error: E) => U): T | U;
|
151 | /** Method variant for {@linkcode toString} */
|
152 | toString(): string;
|
153 | /** Method variant for {@linkcode toJSON} */
|
154 | toJSON(): ResultJSON<T, E>;
|
155 | /** Method variant for {@linkcode equals} */
|
156 | equals(comparison: Result<T, E>): boolean;
|
157 | /** Method variant for {@linkcode ap} */
|
158 | ap<A, B>(this: Result<(a: A) => B, E>, r: Result<A, E>): Result<B, E>;
|
159 | cast(): this;
|
160 | }
|
161 | /**
|
162 | An `Ok` instance is the *successful* variant instance of the
|
163 | {@linkcode Result} type, representing a successful outcome from an operation
|
164 | which may fail. For a full discussion, see the module docs.
|
165 |
|
166 | @template T The type wrapped in this `Ok` variant of `Result`.
|
167 | @template E The type which would be wrapped in an `Err` variant of `Result`.
|
168 | */
|
169 | export interface Ok<T, E> extends Omit<ResultImpl<T, E>, 'error' | 'cast'> {
|
170 | /** `Ok` is always `Variant.Ok`. */
|
171 | readonly variant: 'Ok';
|
172 | isOk: true;
|
173 | isErr: false;
|
174 | /** The wrapped value */
|
175 | value: T;
|
176 | cast<F>(): Result<T, F>;
|
177 | }
|
178 | /**
|
179 | An `Err` instance is the *failure* variant instance of the {@linkcode Result}
|
180 | type, representing a failure outcome from an operation which may fail. For a
|
181 | full discussion, see the module docs.
|
182 |
|
183 | @template T The type which would be wrapped in an `Ok` variant of `Result`.
|
184 | @template E The type wrapped in this `Err` variant of `Result`.
|
185 | */
|
186 | export interface Err<T, E> extends Omit<ResultImpl<T, E>, 'value' | 'cast'> {
|
187 | /** `Err` is always `Variant.Err`. */
|
188 | readonly variant: 'Err';
|
189 | isOk: false;
|
190 | isErr: true;
|
191 | /** The wrapped error value. */
|
192 | error: E;
|
193 | cast<U>(): Result<U, E>;
|
194 | }
|
195 | /**
|
196 | Execute the provided callback, wrapping the return value in {@linkcode Ok} or
|
197 | {@linkcode Err Err(error)} if there is an exception.
|
198 |
|
199 | ```ts
|
200 | const aSuccessfulOperation = () => 2 + 2;
|
201 |
|
202 | const anOkResult = Result.tryOr('Oh noes!!1', () => {
|
203 | aSuccessfulOperation()
|
204 | });
|
205 |
|
206 | const thisOperationThrows = () => throw new Error('Bummer');
|
207 |
|
208 | const anErrResult = Result.tryOr('Oh noes!!1', () => {
|
209 | thisOperationThrows();
|
210 | });
|
211 | ```
|
212 |
|
213 | @param error The error value in case of an exception
|
214 | @param callback The callback to try executing
|
215 | */
|
216 | export declare function tryOr<T, E>(error: E, callback: () => T): Result<T, E>;
|
217 | export declare function tryOr<T, E>(error: E): (callback: () => T) => Result<T, E>;
|
218 | /**
|
219 | Create an instance of {@linkcode Ok}.
|
220 |
|
221 | If you need to create an instance with a specific type (as you do whenever you
|
222 | are not constructing immediately for a function return or as an argument to a
|
223 | function), you can use a type parameter:
|
224 |
|
225 | ```ts
|
226 | const yayNumber = Result.ok<number, string>(12);
|
227 | ```
|
228 |
|
229 | Note: passing nothing, or passing `null` or `undefined` explicitly, will
|
230 | produce a `Result<Unit, E>`, rather than producing the nonsensical and in
|
231 | practice quite annoying `Result<null, string>` etc. See {@linkcode Unit} for
|
232 | more.
|
233 |
|
234 | ```ts
|
235 | const normalResult = Result.ok<number, string>(42);
|
236 | const explicitUnit = Result.ok<Unit, string>(Unit);
|
237 | const implicitUnit = Result.ok<Unit, string>();
|
238 | ```
|
239 |
|
240 | In the context of an immediate function return, or an arrow function with a
|
241 | single expression value, you do not have to specify the types, so this can be
|
242 | quite convenient.
|
243 |
|
244 | ```ts
|
245 | type SomeData = {
|
246 |
|
247 | };
|
248 |
|
249 | const isValid = (data: SomeData): boolean => {
|
250 |
|
251 | }
|
252 |
|
253 | const arrowValidate = (data: SomeData): Result<Unit, string> =>
|
254 | isValid(data) ? Result.ok() : Result.err('something was wrong!');
|
255 |
|
256 | function fnValidate(data: someData): Result<Unit, string> {
|
257 | return isValid(data) ? Result.ok() : Result.err('something was wrong');
|
258 | }
|
259 | ```
|
260 |
|
261 | @template T The type of the item contained in the `Result`.
|
262 | @param value The value to wrap in a `Result.Ok`.
|
263 | */
|
264 | export declare const ok: typeof ResultImpl.ok;
|
265 | /**
|
266 | Is the {@linkcode Result} an {@linkcode Ok}?
|
267 |
|
268 | @template T The type of the item contained in the `Result`.
|
269 | @param result The `Result` to check.
|
270 | @returns A type guarded `Ok`.
|
271 | */
|
272 | export declare function isOk<T, E>(result: Result<T, E>): result is Ok<T, E>;
|
273 | /**
|
274 | Is the {@linkcode Result} an {@linkcode Err}?
|
275 |
|
276 | @template T The type of the item contained in the `Result`.
|
277 | @param result The `Result` to check.
|
278 | @returns A type guarded `Err`.
|
279 | */
|
280 | export declare function isErr<T, E>(result: Result<T, E>): result is Err<T, E>;
|
281 | /**
|
282 | Create an instance of {@linkcode Err}.
|
283 |
|
284 | If you need to create an instance with a specific type (as you do whenever you
|
285 | are not constructing immediately for a function return or as an argument to a
|
286 | function), you can use a type parameter:
|
287 |
|
288 | ```ts
|
289 | const notString = Result.err<number, string>('something went wrong');
|
290 | ```
|
291 |
|
292 | Note: passing nothing, or passing `null` or `undefined` explicitly, will
|
293 | produce a `Result<T, Unit>`, rather than producing the nonsensical and in
|
294 | practice quite annoying `Result<null, string>` etc. See {@linkcode Unit} for
|
295 | more.
|
296 |
|
297 | ```ts
|
298 | const normalResult = Result.err<number, string>('oh no');
|
299 | const explicitUnit = Result.err<number, Unit>(Unit);
|
300 | const implicitUnit = Result.err<number, Unit>();
|
301 | ```
|
302 |
|
303 | In the context of an immediate function return, or an arrow function with a
|
304 | single expression value, you do not have to specify the types, so this can be
|
305 | quite convenient.
|
306 |
|
307 | ```ts
|
308 | type SomeData = {
|
309 |
|
310 | };
|
311 |
|
312 | const isValid = (data: SomeData): boolean => {
|
313 |
|
314 | }
|
315 |
|
316 | const arrowValidate = (data: SomeData): Result<number, Unit> =>
|
317 | isValid(data) ? Result.ok(42) : Result.err();
|
318 |
|
319 | function fnValidate(data: someData): Result<number, Unit> {
|
320 | return isValid(data) ? Result.ok(42) : Result.err();
|
321 | }
|
322 | ```
|
323 |
|
324 | @template T The type of the item contained in the `Result`.
|
325 | @param E The error value to wrap in a `Result.Err`.
|
326 | */
|
327 | export declare const err: typeof ResultImpl.err;
|
328 | /**
|
329 | Execute the provided callback, wrapping the return value in {@linkcode Ok}.
|
330 | If there is an exception, return a {@linkcode Err} of whatever the `onError`
|
331 | function returns.
|
332 |
|
333 | ```ts
|
334 | const aSuccessfulOperation = () => 2 + 2;
|
335 |
|
336 | const anOkResult = Result.tryOrElse(
|
337 | (e) => e,
|
338 | aSuccessfulOperation
|
339 | );
|
340 |
|
341 | const thisOperationThrows = () => throw 'Bummer'
|
342 |
|
343 | const anErrResult = Result.tryOrElse((e) => e, () => {
|
344 | thisOperationThrows();
|
345 | });
|
346 | ```
|
347 |
|
348 | @param onError A function that takes `e` exception and returns what will
|
349 | be wrapped in a `Result.Err`
|
350 | @param callback The callback to try executing
|
351 | */
|
352 | export declare function tryOrElse<T, E>(onError: (e: unknown) => E, callback: () => T): Result<T, E>;
|
353 | export declare function tryOrElse<T, E>(onError: (e: unknown) => E): (callback: () => T) => Result<T, E>;
|
354 | /**
|
355 | Map over a {@linkcode Result} instance: apply the function to the wrapped
|
356 | value if the instance is {@linkcode Ok}, and return the wrapped error value
|
357 | wrapped as a new {@linkcode Err} of the correct type (`Result<U, E>`) if the
|
358 | instance is `Err`.
|
359 |
|
360 | `map` works a lot like `Array.prototype.map`, but with one important
|
361 | difference. Both `Result` and `Array` are containers for other kinds of items,
|
362 | but where `Array.prototype.map` has 0 to _n_ items, a `Result` always has
|
363 | exactly one item, which is *either* a success or an error instance.
|
364 |
|
365 | Where `Array.prototype.map` will apply the mapping function to every item in
|
366 | the array (if there are any), `Result.map` will only apply the mapping
|
367 | function to the (single) element if an `Ok` instance, if there is one.
|
368 |
|
369 | If you have no items in an array of numbers named `foo` and call `foo.map(x =>
|
370 | x + 1)`, you'll still some have an array with nothing in it. But if you have
|
371 | any items in the array (`[2, 3]`), and you call `foo.map(x => x + 1)` on it,
|
372 | you'll get a new array with each of those items inside the array "container"
|
373 | transformed (`[3, 4]`).
|
374 |
|
375 | With this `map`, the `Err` variant is treated *by the `map` function* kind of
|
376 | the same way as the empty array case: it's just ignored, and you get back a
|
377 | new `Result` that is still just the same `Err` instance. But if you have an
|
378 | `Ok` variant, the map function is applied to it, and you get back a new
|
379 | `Result` with the value transformed, and still wrapped in an `Ok`.
|
380 |
|
381 | #### Examples
|
382 |
|
383 | ```ts
|
384 | import { ok, err, map, toString } from 'true-myth/result';
|
385 | const double = n => n * 2;
|
386 |
|
387 | const anOk = ok(12);
|
388 | const mappedOk = map(double, anOk);
|
389 | console.log(toString(mappedOk));
|
390 |
|
391 | const anErr = err("nothing here!");
|
392 | const mappedErr = map(double, anErr);
|
393 | console.log(toString(mappedOk));
|
394 | ```
|
395 |
|
396 | @template T The type of the value wrapped in an `Ok` instance, and taken as
|
397 | the argument to the `mapFn`.
|
398 | @template U The type of the value wrapped in the new `Ok` instance after
|
399 | applying `mapFn`, that is, the type returned by `mapFn`.
|
400 | @template E The type of the value wrapped in an `Err` instance.
|
401 | @param mapFn The function to apply the value to if `result` is `Ok`.
|
402 | @param result The `Result` instance to map over.
|
403 | @returns A new `Result` with the result of applying `mapFn` to the value
|
404 | in an `Ok`, or else the original `Err` value wrapped in the new
|
405 | instance.
|
406 | */
|
407 | export declare function map<T, U, E>(mapFn: (t: T) => U, result: Result<T, E>): Result<U, E>;
|
408 | export declare function map<T, U, E>(mapFn: (t: T) => U): (result: Result<T, E>) => Result<U, E>;
|
409 | /**
|
410 | Map over a {@linkcode Result} instance as in [`map`](#map) and get out the
|
411 | value if `result` is an {@linkcode Ok}, or return a default value if `result`
|
412 | is an {@linkcode Err}.
|
413 |
|
414 | #### Examples
|
415 |
|
416 | ```ts
|
417 | import { ok, err, mapOr } from 'true-myth/result';
|
418 |
|
419 | const length = (s: string) => s.length;
|
420 |
|
421 | const anOkString = ok('a string');
|
422 | const theStringLength = mapOr(0, anOkString);
|
423 | console.log(theStringLength);
|
424 |
|
425 | const anErr = err('uh oh');
|
426 | const anErrMapped = mapOr(0, anErr);
|
427 | console.log(anErrMapped);
|
428 | ```
|
429 |
|
430 | @param orU The default value to use if `result` is an `Err`.
|
431 | @param mapFn The function to apply the value to if `result` is an `Ok`.
|
432 | @param result The `Result` instance to map over.
|
433 | */
|
434 | export declare function mapOr<T, U, E>(orU: U, mapFn: (t: T) => U, result: Result<T, E>): U;
|
435 | export declare function mapOr<T, U, E>(orU: U, mapFn: (t: T) => U): (result: Result<T, E>) => U;
|
436 | export declare function mapOr<T, U, E>(orU: U): (mapFn: (t: T) => U) => (result: Result<T, E>) => U;
|
437 | /**
|
438 | Map over a {@linkcode Result} instance as in {@linkcode map} and get out the
|
439 | value if `result` is {@linkcode Ok}, or apply a function (`orElseFn`) to the
|
440 | value wrapped in the {@linkcode Err} to get a default value.
|
441 |
|
442 | Like {@linkcode mapOr} but using a function to transform the error into a
|
443 | usable value instead of simply using a default value.
|
444 |
|
445 | #### Examples
|
446 |
|
447 | ```ts
|
448 | import { ok, err, mapOrElse } from 'true-myth/result';
|
449 |
|
450 | const summarize = (s: string) => `The response was: '${s}'`;
|
451 | const getReason = (err: { code: number, reason: string }) => err.reason;
|
452 |
|
453 | const okResponse = ok("Things are grand here.");
|
454 | const mappedOkAndUnwrapped = mapOrElse(getReason, summarize, okResponse);
|
455 | console.log(mappedOkAndUnwrapped);
|
456 |
|
457 | const errResponse = err({ code: 500, reason: 'Nothing at this endpoint!' });
|
458 | const mappedErrAndUnwrapped = mapOrElse(getReason, summarize, errResponse);
|
459 | console.log(mappedErrAndUnwrapped);
|
460 | ```
|
461 |
|
462 | @template T The type of the wrapped `Ok` value.
|
463 | @template U The type of the resulting value from applying `mapFn` to the
|
464 | `Ok` value or `orElseFn` to the `Err` value.
|
465 | @template E The type of the wrapped `Err` value.
|
466 | @param orElseFn The function to apply to the wrapped `Err` value to get a
|
467 | usable value if `result` is an `Err`.
|
468 | @param mapFn The function to apply to the wrapped `Ok` value if `result` is
|
469 | an `Ok`.
|
470 | @param result The `Result` instance to map over.
|
471 | */
|
472 | export declare function mapOrElse<T, U, E>(orElseFn: (err: E) => U, mapFn: (t: T) => U, result: Result<T, E>): U;
|
473 | export declare function mapOrElse<T, U, E>(orElseFn: (err: E) => U, mapFn: (t: T) => U): (result: Result<T, E>) => U;
|
474 | export declare function mapOrElse<T, U, E>(orElseFn: (err: E) => U): (mapFn: (t: T) => U) => (result: Result<T, E>) => U;
|
475 | /**
|
476 | Map over a {@linkcode Ok}, exactly as in {@linkcode map}, but operating on the
|
477 | value wrapped in an {@linkcode Err} instead of the value wrapped in the
|
478 | {@linkcode Ok}. This is handy for when you need to line up a bunch of
|
479 | different types of errors, or if you need an error of one shape to be in a
|
480 | different shape to use somewhere else in your codebase.
|
481 |
|
482 | #### Examples
|
483 |
|
484 | ```ts
|
485 | import { ok, err, mapErr, toString } from 'true-myth/result';
|
486 |
|
487 | const reason = (err: { code: number, reason: string }) => err.reason;
|
488 |
|
489 | const anOk = ok(12);
|
490 | const mappedOk = mapErr(reason, anOk);
|
491 | console.log(toString(mappedOk));
|
492 |
|
493 | const anErr = err({ code: 101, reason: 'bad file' });
|
494 | const mappedErr = mapErr(reason, anErr);
|
495 | console.log(toString(mappedErr));
|
496 | ```
|
497 |
|
498 | @template T The type of the value wrapped in the `Ok` of the `Result`.
|
499 | @template E The type of the value wrapped in the `Err` of the `Result`.
|
500 | @template F The type of the value wrapped in the `Err` of a new `Result`,
|
501 | returned by the `mapErrFn`.
|
502 | @param mapErrFn The function to apply to the value wrapped in `Err` if
|
503 | `result` is an `Err`.
|
504 | @param result The `Result` instance to map over an error case for.
|
505 | */
|
506 | export declare function mapErr<T, E, F>(mapErrFn: (e: E) => F, result: Result<T, E>): Result<T, F>;
|
507 | export declare function mapErr<T, E, F>(mapErrFn: (e: E) => F): (result: Result<T, E>) => Result<T, F>;
|
508 | /**
|
509 | You can think of this like a short-circuiting logical "and" operation on a
|
510 | {@linkcode Result} type. If `result` is {@linkcode Ok}, then the result is the
|
511 | `andResult`. If `result` is {@linkcode Err}, the result is the `Err`.
|
512 |
|
513 | This is useful when you have another `Result` value you want to provide if and
|
514 | *only if* you have an `Ok` – that is, when you need to make sure that if you
|
515 | `Err`, whatever else you're handing a `Result` to *also* gets that `Err`.
|
516 |
|
517 | Notice that, unlike in [`map`](#map) or its variants, the original `result` is
|
518 | not involved in constructing the new `Result`.
|
519 |
|
520 | #### Examples
|
521 |
|
522 | ```ts
|
523 | import { and, ok, err, toString } from 'true-myth/result';
|
524 |
|
525 | const okA = ok('A');
|
526 | const okB = ok('B');
|
527 | const anErr = err({ so: 'bad' });
|
528 |
|
529 | console.log(toString(and(okB, okA)));
|
530 | console.log(toString(and(okB, anErr)));
|
531 | console.log(toString(and(anErr, okA)));
|
532 | console.log(toString(and(anErr, anErr)));
|
533 | ```
|
534 |
|
535 | @template T The type of the value wrapped in the `Ok` of the `Result`.
|
536 | @template U The type of the value wrapped in the `Ok` of the `andResult`,
|
537 | i.e. the success type of the `Result` present if the checked
|
538 | `Result` is `Ok`.
|
539 | @template E The type of the value wrapped in the `Err` of the `Result`.
|
540 | @param andResult The `Result` instance to return if `result` is `Err`.
|
541 | @param result The `Result` instance to check.
|
542 | */
|
543 | export declare function and<T, U, E>(andResult: Result<U, E>, result: Result<T, E>): Result<U, E>;
|
544 | export declare function and<T, U, E>(andResult: Result<U, E>): (result: Result<T, E>) => Result<U, E>;
|
545 | /**
|
546 | Apply a function to the wrapped value if {@linkcode Ok} and return a new `Ok`
|
547 | containing the resulting value; or if it is {@linkcode Err} return it
|
548 | unmodified.
|
549 |
|
550 | This differs from `map` in that `thenFn` returns another {@linkcode Result}.
|
551 | You can use `andThen` to combine two functions which *both* create a `Result`
|
552 | from an unwrapped type.
|
553 |
|
554 | You may find the `.then` method on an ES6 `Promise` helpful for comparison: if
|
555 | you have a `Promise`, you can pass its `then` method a callback which returns
|
556 | another `Promise`, and the result will not be a *nested* promise, but a single
|
557 | `Promise`. The difference is that `Promise#then` unwraps *all* layers to only
|
558 | ever return a single `Promise` value, whereas `Result.andThen` will not unwrap
|
559 | nested `Result`s.
|
560 |
|
561 | This is is sometimes also known as `bind`, but *not* aliased as such because
|
562 | [`bind` already means something in JavaScript][bind].
|
563 |
|
564 | [bind]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind
|
565 |
|
566 | #### Examples
|
567 |
|
568 | ```ts
|
569 | import { ok, err, andThen, toString } from 'true-myth/result';
|
570 |
|
571 | const toLengthAsResult = (s: string) => ok(s.length);
|
572 |
|
573 | const anOk = ok('just a string');
|
574 | const lengthAsResult = andThen(toLengthAsResult, anOk);
|
575 | console.log(toString(lengthAsResult));
|
576 |
|
577 | const anErr = err(['srsly', 'whatever']);
|
578 | const notLengthAsResult = andThen(toLengthAsResult, anErr);
|
579 | console.log(toString(notLengthAsResult));
|
580 | ```
|
581 |
|
582 | @template T The type of the value wrapped in the `Ok` of the `Result`.
|
583 | @template U The type of the value wrapped in the `Ok` of the `Result`
|
584 | returned by the `thenFn`.
|
585 | @template E The type of the value wrapped in the `Err` of the `Result`.
|
586 | @param thenFn The function to apply to the wrapped `T` if `maybe` is `Just`.
|
587 | @param result The `Maybe` to evaluate and possibly apply a function to.
|
588 | */
|
589 | export declare function andThen<T, U, E>(thenFn: (t: T) => Result<U, E>, result: Result<T, E>): Result<U, E>;
|
590 | export declare function andThen<T, U, E>(thenFn: (t: T) => Result<U, E>): (result: Result<T, E>) => Result<U, E>;
|
591 | /**
|
592 | Provide a fallback for a given {@linkcode Result}. Behaves like a logical
|
593 | `or`: if the `result` value is an {@linkcode Ok}, returns that `result`;
|
594 | otherwise, returns the `defaultResult` value.
|
595 |
|
596 | This is useful when you want to make sure that something which takes a
|
597 | `Result` always ends up getting an `Ok` variant, by supplying a default value
|
598 | for the case that you currently have an {@linkcode Err}.
|
599 |
|
600 | ```ts
|
601 | import { ok, err, Result, or } from 'true-utils/result';
|
602 |
|
603 | const okA = ok<string, string>('a');
|
604 | const okB = ok<string, string>('b');
|
605 | const anErr = err<string, string>(':wat:');
|
606 | const anotherErr = err<string, string>(':headdesk:');
|
607 |
|
608 | console.log(or(okB, okA).toString());
|
609 | console.log(or(anErr, okA).toString());
|
610 | console.log(or(okB, anErr).toString());
|
611 | console.log(or(anotherErr, anErr).toString());
|
612 | ```
|
613 |
|
614 | @template T The type wrapped in the `Ok` case of `result`.
|
615 | @template E The type wrapped in the `Err` case of `result`.
|
616 | @template F The type wrapped in the `Err` case of `defaultResult`.
|
617 | @param defaultResult The `Result` to use if `result` is an `Err`.
|
618 | @param result The `Result` instance to check.
|
619 | @returns `result` if it is an `Ok`, otherwise `defaultResult`.
|
620 | */
|
621 | export declare function or<T, E, F>(defaultResult: Result<T, F>, result: Result<T, E>): Result<T, F>;
|
622 | export declare function or<T, E, F>(defaultResult: Result<T, F>): (result: Result<T, E>) => Result<T, F>;
|
623 | /**
|
624 | Like {@linkcode or}, but using a function to construct the alternative
|
625 | {@linkcode Result}.
|
626 |
|
627 | Sometimes you need to perform an operation using other data in the environment
|
628 | to construct the fallback value. In these situations, you can pass a function
|
629 | (which may be a closure) as the `elseFn` to generate the fallback `Result<T>`.
|
630 | It can then transform the data in the `Err` to something usable as an
|
631 | {@linkcode Ok}, or generate a new {@linkcode Err} instance as appropriate.
|
632 |
|
633 | Useful for transforming failures to usable data.
|
634 |
|
635 | @param elseFn The function to apply to the contents of the `Err` if `result`
|
636 | is an `Err`, to create a new `Result`.
|
637 | @param result The `Result` to use if it is an `Ok`.
|
638 | @returns The `result` if it is `Ok`, or the `Result` returned by `elseFn`
|
639 | if `result` is an `Err.
|
640 | */
|
641 | export declare function orElse<T, E, F>(elseFn: (err: E) => Result<T, F>, result: Result<T, E>): Result<T, F>;
|
642 | export declare function orElse<T, E, F>(elseFn: (err: E) => Result<T, F>): (result: Result<T, E>) => Result<T, F>;
|
643 |
|
644 |
|
645 |
|
646 |
|
647 |
|
648 |
|
649 |
|
650 |
|
651 |
|
652 |
|
653 |
|
654 |
|
655 |
|
656 |
|
657 |
|
658 |
|
659 |
|
660 |
|
661 |
|
662 |
|
663 |
|
664 |
|
665 | export declare function unwrapOr<T, U, E>(defaultValue: U, result: Result<T, E>): U | T;
|
666 | export declare function unwrapOr<T, U, E>(defaultValue: U): (result: Result<T, E>) => U | T;
|
667 |
|
668 |
|
669 |
|
670 |
|
671 |
|
672 |
|
673 |
|
674 |
|
675 |
|
676 |
|
677 |
|
678 |
|
679 |
|
680 |
|
681 |
|
682 |
|
683 |
|
684 |
|
685 |
|
686 |
|
687 |
|
688 |
|
689 |
|
690 |
|
691 |
|
692 |
|
693 |
|
694 |
|
695 |
|
696 |
|
697 |
|
698 | export declare function unwrapOrElse<T, U, E>(orElseFn: (error: E) => U, result: Result<T, E>): T | U;
|
699 | export declare function unwrapOrElse<T, U, E>(orElseFn: (error: E) => U): (result: Result<T, E>) => T | U;
|
700 |
|
701 |
|
702 |
|
703 |
|
704 |
|
705 |
|
706 |
|
707 |
|
708 |
|
709 |
|
710 |
|
711 |
|
712 |
|
713 |
|
714 |
|
715 |
|
716 |
|
717 |
|
718 |
|
719 |
|
720 |
|
721 |
|
722 | export declare const toString: <T, E>(result: Result<T, E>) => string;
|
723 |
|
724 |
|
725 |
|
726 |
|
727 |
|
728 |
|
729 |
|
730 |
|
731 | export declare const toJSON: <T, E>(result: Result<T, E>) => ResultJSON<T, E>;
|
732 |
|
733 |
|
734 |
|
735 |
|
736 | export type Matcher<T, E, A> = {
|
737 | Ok: (value: T) => A;
|
738 | Err: (error: E) => A;
|
739 | };
|
740 |
|
741 |
|
742 |
|
743 |
|
744 |
|
745 |
|
746 |
|
747 |
|
748 |
|
749 |
|
750 |
|
751 |
|
752 |
|
753 |
|
754 |
|
755 |
|
756 |
|
757 |
|
758 |
|
759 |
|
760 |
|
761 |
|
762 |
|
763 |
|
764 |
|
765 |
|
766 |
|
767 |
|
768 |
|
769 |
|
770 |
|
771 |
|
772 |
|
773 |
|
774 |
|
775 |
|
776 |
|
777 |
|
778 |
|
779 |
|
780 |
|
781 |
|
782 |
|
783 |
|
784 |
|
785 |
|
786 |
|
787 |
|
788 |
|
789 |
|
790 | export declare function match<T, E, A>(matcher: Matcher<T, E, A>, result: Result<T, E>): A;
|
791 |
|
792 |
|
793 |
|
794 |
|
795 |
|
796 |
|
797 |
|
798 |
|
799 |
|
800 |
|
801 |
|
802 |
|
803 |
|
804 |
|
805 |
|
806 |
|
807 |
|
808 |
|
809 |
|
810 |
|
811 |
|
812 |
|
813 |
|
814 |
|
815 |
|
816 |
|
817 |
|
818 |
|
819 |
|
820 |
|
821 |
|
822 |
|
823 |
|
824 |
|
825 |
|
826 |
|
827 |
|
828 |
|
829 |
|
830 |
|
831 |
|
832 |
|
833 |
|
834 |
|
835 |
|
836 |
|
837 |
|
838 |
|
839 |
|
840 | export declare function match<T, E, A>(matcher: Matcher<T, E, A>): (result: Result<T, E>) => A;
|
841 |
|
842 |
|
843 |
|
844 |
|
845 |
|
846 |
|
847 |
|
848 |
|
849 |
|
850 |
|
851 |
|
852 |
|
853 |
|
854 |
|
855 |
|
856 |
|
857 |
|
858 |
|
859 | export declare function equals<T, E>(resultB: Result<T, E>, resultA: Result<T, E>): boolean;
|
860 | export declare function equals<T, E>(resultB: Result<T, E>): (resultA: Result<T, E>) => boolean;
|
861 |
|
862 |
|
863 |
|
864 |
|
865 |
|
866 |
|
867 |
|
868 |
|
869 |
|
870 |
|
871 |
|
872 |
|
873 |
|
874 |
|
875 |
|
876 |
|
877 |
|
878 |
|
879 |
|
880 |
|
881 |
|
882 |
|
883 |
|
884 |
|
885 |
|
886 |
|
887 |
|
888 |
|
889 |
|
890 |
|
891 |
|
892 |
|
893 |
|
894 |
|
895 |
|
896 |
|
897 |
|
898 |
|
899 |
|
900 |
|
901 |
|
902 |
|
903 |
|
904 |
|
905 |
|
906 |
|
907 |
|
908 |
|
909 |
|
910 |
|
911 |
|
912 |
|
913 |
|
914 |
|
915 |
|
916 |
|
917 |
|
918 |
|
919 |
|
920 |
|
921 |
|
922 |
|
923 |
|
924 |
|
925 |
|
926 |
|
927 |
|
928 |
|
929 |
|
930 |
|
931 |
|
932 |
|
933 |
|
934 |
|
935 |
|
936 |
|
937 |
|
938 |
|
939 |
|
940 |
|
941 |
|
942 |
|
943 |
|
944 |
|
945 |
|
946 |
|
947 |
|
948 |
|
949 |
|
950 |
|
951 |
|
952 |
|
953 |
|
954 |
|
955 |
|
956 |
|
957 |
|
958 |
|
959 |
|
960 |
|
961 |
|
962 |
|
963 |
|
964 |
|
965 |
|
966 |
|
967 |
|
968 |
|
969 |
|
970 |
|
971 |
|
972 |
|
973 |
|
974 |
|
975 |
|
976 |
|
977 |
|
978 |
|
979 |
|
980 |
|
981 |
|
982 |
|
983 |
|
984 |
|
985 |
|
986 |
|
987 |
|
988 |
|
989 |
|
990 |
|
991 |
|
992 |
|
993 |
|
994 |
|
995 |
|
996 |
|
997 |
|
998 |
|
999 |
|
1000 |
|
1001 |
|
1002 |
|
1003 |
|
1004 |
|
1005 |
|
1006 |
|
1007 |
|
1008 |
|
1009 |
|
1010 |
|
1011 |
|
1012 |
|
1013 |
|
1014 |
|
1015 |
|
1016 |
|
1017 |
|
1018 |
|
1019 |
|
1020 |
|
1021 |
|
1022 |
|
1023 |
|
1024 |
|
1025 |
|
1026 |
|
1027 |
|
1028 |
|
1029 |
|
1030 |
|
1031 | export declare function ap<A, B, E>(resultFn: Result<(a: A) => B, E>, result: Result<A, E>): Result<B, E>;
|
1032 | export declare function ap<A, B, E>(resultFn: Result<(a: A) => B, E>): (result: Result<A, E>) => Result<B, E>;
|
1033 |
|
1034 |
|
1035 |
|
1036 |
|
1037 |
|
1038 | export declare function isInstance<T, E>(item: unknown): item is Result<T, E>;
|
1039 | export interface ResultConstructor {
|
1040 | ok: typeof ResultImpl.ok;
|
1041 | err: typeof ResultImpl.err;
|
1042 | }
|
1043 |
|
1044 |
|
1045 |
|
1046 |
|
1047 |
|
1048 |
|
1049 | export type Result<T, E> = Ok<T, E> | Err<T, E>;
|
1050 | export declare const Result: ResultConstructor;
|
1051 | export default Result;
|
1052 |
|
\ | No newline at end of file |