@mkvlrn/result 5.0.3 → 5.0.4
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/build/index.d.ts +36 -0
- package/build/index.js +1 -0
- package/package.json +1 -1
package/build/index.d.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
//#region src/index.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Result type to represent the outcome of an operation.
|
|
4
|
+
* It can either be a success with a value or an error.
|
|
5
|
+
* This is a generic type that can be used with any type of value and error (should extend Error).
|
|
6
|
+
*
|
|
7
|
+
* It is also an alias object containing the ok and error functions to
|
|
8
|
+
* make it easier to create Result objects.
|
|
9
|
+
*/
|
|
10
|
+
type Result<T, E extends Error> = {
|
|
11
|
+
readonly isError: false;
|
|
12
|
+
readonly isOk: true;
|
|
13
|
+
readonly value: T;
|
|
14
|
+
} | {
|
|
15
|
+
readonly isError: true;
|
|
16
|
+
readonly isOk: false;
|
|
17
|
+
readonly error: E;
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Async version of Result type that wraps a Result in a Promise.
|
|
21
|
+
*/
|
|
22
|
+
type AsyncResult<T, E extends Error> = Promise<Result<T, E>>;
|
|
23
|
+
/**
|
|
24
|
+
* Creates a successful Result with the given value.
|
|
25
|
+
* @param value The success value
|
|
26
|
+
* @returns A Result object representing success
|
|
27
|
+
*/
|
|
28
|
+
declare function ok<T>(value: T): Result<T, never>;
|
|
29
|
+
/**
|
|
30
|
+
* Creates an error Result with the given error.
|
|
31
|
+
* @param error The error value
|
|
32
|
+
* @returns A Result object representing error
|
|
33
|
+
*/
|
|
34
|
+
declare function err<E extends Error>(error: E): Result<never, E>;
|
|
35
|
+
//#endregion
|
|
36
|
+
export { AsyncResult, Result, err, ok };
|
package/build/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
function e(e){return{isError:!1,isOk:!0,value:e}}function t(e){return{isError:!0,isOk:!1,error:e}}export{t as err,e as ok};
|