resultfier 1.0.0 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Vitor Koch
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,48 @@
1
+ # resultfier
2
+
3
+ A TypeScript/JavaScript library that brings Rust's `Result<T, E>` type to your projects. Provides robust error handling with type safety and functional programming patterns.
4
+
5
+ ## Features
6
+
7
+ - 🦀 Rust-inspired `Result<T, E>` type
8
+ - 🛡️ Type-safe error handling
9
+ - ⚡ Zero dependencies
10
+ - 🔄 Functional programming methods (map, andThen, etc.)
11
+ - 📦 TypeScript-first with full type support
12
+ - 🎯 Simple and intuitive API
13
+
14
+ ## Installation
15
+
16
+ ```bash
17
+ npm install @vikoch/resultfier
18
+ # or
19
+ yarn add @vikoch/resultfier
20
+ # or
21
+ bun add @vikoch/resultfier
22
+ ```
23
+
24
+ ## Quick Start
25
+
26
+ ```typescript
27
+ import type { Result } from '@vikoch/resultfier'
28
+ import { err, ok } from '@vikoch/resultfier'
29
+
30
+ function divide(a: number, b: number): Result<number, string> {
31
+ if (b === 0) {
32
+ return err('Cannot divide by zero')
33
+ }
34
+ return ok(a / b)
35
+ }
36
+
37
+ const result = divide(10, 2)
38
+
39
+ // Type Narrowing
40
+ if (result.isOk) {
41
+ console.log('Result:', result.value) // 5
42
+ // ^? Ok<number>
43
+ }
44
+ else {
45
+ console.log('Error:', result.error) // Cannot divide by zero
46
+ // ^? Err<string>
47
+ }
48
+ ```
@@ -0,0 +1,6 @@
1
+ import { t as AsyncResult } from "../result-DIdNM9rp.mjs";
2
+
3
+ //#region src/action/action.d.ts
4
+ type AsyncAction<TData, TError, TParams = null> = (params: TParams extends null ? void : TParams) => AsyncResult<TData, TError>;
5
+ //#endregion
6
+ export { type AsyncAction };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,2 @@
1
+ import { a as err, i as Err, n as Ok, r as ok, t as toAsyncResult } from "../core-BdRFoSfb.mjs";
2
+ export { Err, Ok, err, ok, toAsyncResult };
@@ -0,0 +1,101 @@
1
+ var ResultBase = class {};
2
+ function _typeof(o) {
3
+ "@babel/helpers - typeof";
4
+ return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o$1) {
5
+ return typeof o$1;
6
+ } : function(o$1) {
7
+ return o$1 && "function" == typeof Symbol && o$1.constructor === Symbol && o$1 !== Symbol.prototype ? "symbol" : typeof o$1;
8
+ }, _typeof(o);
9
+ }
10
+ function toPrimitive(t, r) {
11
+ if ("object" != _typeof(t) || !t) return t;
12
+ var e = t[Symbol.toPrimitive];
13
+ if (void 0 !== e) {
14
+ var i = e.call(t, r || "default");
15
+ if ("object" != _typeof(i)) return i;
16
+ throw new TypeError("@@toPrimitive must return a primitive value.");
17
+ }
18
+ return ("string" === r ? String : Number)(t);
19
+ }
20
+ function toPropertyKey(t) {
21
+ var i = toPrimitive(t, "string");
22
+ return "symbol" == _typeof(i) ? i : i + "";
23
+ }
24
+ function _defineProperty(e, r, t) {
25
+ return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
26
+ value: t,
27
+ enumerable: !0,
28
+ configurable: !0,
29
+ writable: !0
30
+ }) : e[r] = t, e;
31
+ }
32
+ var Err = class Err extends ResultBase {
33
+ constructor(error) {
34
+ super();
35
+ this.error = error;
36
+ _defineProperty(this, "_tag", "err");
37
+ _defineProperty(this, "isOk", false);
38
+ _defineProperty(this, "isErr", true);
39
+ }
40
+ map(_fn) {
41
+ return this;
42
+ }
43
+ mapErr(fn) {
44
+ return new Err(fn(this.error));
45
+ }
46
+ valueOr(defaultValue) {
47
+ return defaultValue;
48
+ }
49
+ valueOrElse(fn) {
50
+ return fn(this.error);
51
+ }
52
+ andThen(_fn) {
53
+ return this;
54
+ }
55
+ contains(_value) {
56
+ return false;
57
+ }
58
+ containsErr(error) {
59
+ return this.error === error;
60
+ }
61
+ };
62
+ function err(error) {
63
+ return new Err(error);
64
+ }
65
+ var Ok = class Ok extends ResultBase {
66
+ constructor(value) {
67
+ super();
68
+ this.value = value;
69
+ _defineProperty(this, "_tag", "ok");
70
+ _defineProperty(this, "isOk", true);
71
+ _defineProperty(this, "isErr", false);
72
+ }
73
+ map(fn) {
74
+ return new Ok(fn(this.value));
75
+ }
76
+ mapErr(_fn) {
77
+ return this;
78
+ }
79
+ valueOr(_defaultValue) {
80
+ return this.value;
81
+ }
82
+ valueOrElse(_fn) {
83
+ return this.value;
84
+ }
85
+ andThen(fn) {
86
+ return fn(this.value);
87
+ }
88
+ contains(value) {
89
+ return this.value === value;
90
+ }
91
+ containsErr(_error) {
92
+ return false;
93
+ }
94
+ };
95
+ function ok(value) {
96
+ return new Ok(value);
97
+ }
98
+ async function toAsyncResult(callback) {
99
+ return Promise.resolve(callback()).then((value) => ok(value)).catch((error) => err(error));
100
+ }
101
+ export { err as a, Err as i, Ok as n, ok as r, toAsyncResult as t };
@@ -0,0 +1,6 @@
1
+ import { a as ok, i as Ok, n as Result, o as Err, r as ResultBase, s as err, t as AsyncResult } from "./result-DIdNM9rp.mjs";
2
+
3
+ //#region src/core/utils.d.ts
4
+ declare function toAsyncResult<T, E>(callback: () => Promise<T> | T): AsyncResult<T, E>;
5
+ //#endregion
6
+ export { AsyncResult, Err, Ok, Result, ResultBase, err, ok, toAsyncResult };
package/dist/index.mjs ADDED
@@ -0,0 +1,2 @@
1
+ import { a as err, i as Err, n as Ok, r as ok, t as toAsyncResult } from "./core-BdRFoSfb.mjs";
2
+ export { Err, Ok, err, ok, toAsyncResult };
@@ -0,0 +1,147 @@
1
+ //#region src/core/err.d.ts
2
+
3
+ /**
4
+ * Represents an error result containing an error value.
5
+ */
6
+ declare class Err<E> extends ResultBase<never, E> {
7
+ readonly error: E;
8
+ readonly _tag: "err";
9
+ readonly isOk = false;
10
+ readonly isErr = true;
11
+ constructor(error: E);
12
+ /**
13
+ * Maps the value type, but since this is `Err`, it's a no-op.
14
+ */
15
+ map<U>(_fn: (value: never) => U): Err<E>;
16
+ /**
17
+ * Maps the contained error using the provided function.
18
+ * Since this is `Err`, the function is always applied.
19
+ */
20
+ mapErr<F>(fn: (error: E) => F): Err<F>;
21
+ /**
22
+ * Returns the provided default value since this is `Err`.
23
+ */
24
+ valueOr<T>(defaultValue: T): T;
25
+ /**
26
+ * Computes and returns the fallback value using the provided function.
27
+ */
28
+ valueOrElse<T>(fn: (error: E) => T): T;
29
+ /**
30
+ * Returns this `Err` unchanged since there's no value to chain.
31
+ */
32
+ andThen<U>(_fn: (value: never) => Result<U, E>): Err<E>;
33
+ /**
34
+ * Always returns false since this is `Err`, not `Ok`.
35
+ */
36
+ contains(_value: never): boolean;
37
+ /**
38
+ * Checks if the contained error matches the provided error.
39
+ */
40
+ containsErr(error: E): boolean;
41
+ }
42
+ /**
43
+ * Creates a new `Err` result with the given error.
44
+ */
45
+ declare function err<E>(error: E): Err<E>;
46
+ //#endregion
47
+ //#region src/core/ok.d.ts
48
+ /**
49
+ * Represents a successful result containing a value.
50
+ */
51
+ declare class Ok<T> extends ResultBase<T, never> {
52
+ readonly value: T;
53
+ readonly _tag: "ok";
54
+ readonly isOk = true;
55
+ readonly isErr = false;
56
+ constructor(value: T);
57
+ /**
58
+ * Maps the contained value using the provided function.
59
+ * Since this is `Ok`, the function is always applied.
60
+ */
61
+ map<U>(fn: (value: T) => U): Ok<U>;
62
+ /**
63
+ * Maps the error type, but since this is `Ok`, it's a no-op.
64
+ */
65
+ mapErr<F>(_fn: (error: never) => F): Ok<T>;
66
+ /**
67
+ * Returns the contained value, ignoring the default.
68
+ */
69
+ valueOr(_defaultValue: T): T;
70
+ /**
71
+ * Returns the contained value, ignoring the fallback function.
72
+ */
73
+ valueOrElse(_fn: (error: never) => T): T;
74
+ /**
75
+ * Applies the function to the contained value and returns the result.
76
+ */
77
+ andThen<U>(fn: (value: T) => Result<U, never>): Result<U, never>;
78
+ /**
79
+ * Checks if the contained value matches the provided value.
80
+ */
81
+ contains(value: T): boolean;
82
+ /**
83
+ * Always returns false since this is `Ok`, not `Err`.
84
+ */
85
+ containsErr(_error: never): false;
86
+ }
87
+ /**
88
+ * Creates a new `Ok` result with the given value.
89
+ */
90
+ declare function ok<T>(value: T): Ok<T>;
91
+ //#endregion
92
+ //#region src/core/result.d.ts
93
+ /**
94
+ * Represents a Result type that can either be a success (`Ok`) or an error (`Err`).
95
+ * This is an abstract base class that provides common functionality for both variants.
96
+ */
97
+ declare abstract class ResultBase<T, E> {
98
+ /**
99
+ * Type guard to check if the result is an `Ok` variant.
100
+ */
101
+ abstract readonly isOk: boolean;
102
+ /**
103
+ * Type guard to check if the result is an `Err` variant.
104
+ */
105
+ abstract readonly isErr: boolean;
106
+ /**
107
+ * Maps a `Result<T, E>` to `Result<U, E>` by applying a function to a contained `Ok` value,
108
+ * leaving an `Err` value untouched.
109
+ */
110
+ abstract map<U>(fn: (value: T) => U): Result<U, E>;
111
+ /**
112
+ * Maps a `Result<T, E>` to `Result<T, F>` by applying a function to a contained `Err` value,
113
+ * leaving an `Ok` value untouched.
114
+ */
115
+ abstract mapErr<F>(fn: (error: E) => F): Result<T, F>;
116
+ /**
117
+ * Returns the contained `Ok` value or a provided default.
118
+ */
119
+ abstract valueOr(defaultValue: T): T;
120
+ /**
121
+ * Returns the contained `Ok` value or computes it from a closure.
122
+ */
123
+ abstract valueOrElse(fn: (error: E) => T): T;
124
+ /**
125
+ * Calls the provided function with the contained value and returns the result.
126
+ * If the result is `Err`, the function is not called and the `Err` is returned.
127
+ */
128
+ abstract andThen<U>(fn: (value: T) => Result<U, E>): Result<U, E>;
129
+ /**
130
+ * Returns true if the result is `Ok` and the value inside of it matches a predicate.
131
+ */
132
+ abstract contains(value: T): boolean;
133
+ /**
134
+ * Returns true if the result is `Err` and the error inside of it matches a predicate.
135
+ */
136
+ abstract containsErr(error: E): boolean;
137
+ }
138
+ /**
139
+ * Type alias for a `Result` that can be either `Ok<T>` or `Err<E>`.
140
+ */
141
+ type Result<T, E> = Ok<T> | Err<E>;
142
+ /**
143
+ * Type alias for an async `Result` (Promise-wrapped `Result`).
144
+ */
145
+ type AsyncResult<T, E> = Promise<Result<T, E>>;
146
+ //#endregion
147
+ export { ok as a, Ok as i, Result as n, Err as o, ResultBase as r, err as s, AsyncResult as t };
package/package.json CHANGED
@@ -1,27 +1,43 @@
1
1
  {
2
- "name": "resultfier",
3
- "version": "1.0.0",
4
- "description": "A TypeScript implementation of Rust-like Result type for better error handling",
5
- "type": "module",
6
- "scripts": {
7
- "build": "tsc",
8
- "prepublishOnly": "bun run build"
9
- },
10
- "author": "Vitor Koch <vitorgabrielkoch@gmail.com>",
11
- "license": "MIT",
12
- "repository": {
13
- "type": "git",
14
- "url": "git+https://github.com/vitorkoch/resultfier.git"
15
- },
16
- "bugs": {
17
- "url": "https://github.com/vitorkoch/resultfier/issues"
18
- },
19
- "homepage": "https://github.com/vitorkoch/resultfier#readme",
20
- "devDependencies": {
21
- "@types/bun": "latest",
22
- "typescript": "^5.9.2"
23
- },
24
- "peerDependencies": {
25
- "typescript": "^5"
26
- }
2
+ "name": "resultfier",
3
+ "type": "module",
4
+ "version": "3.0.0",
5
+ "private": "false",
6
+ "packageManager": "bun@1.3.5",
7
+ "description": "A TypeScript/JavaScript library that brings Rust's Result<T, E> type for robust error handling",
8
+ "author": "Vitor Koch",
9
+ "license": "MIT",
10
+ "keywords": ["rust", "error handling", "result", "typescript"],
11
+ "exports": {
12
+ ".": {
13
+ "types": "./dist/index.d.mts",
14
+ "import": "./dist/index.mjs"
15
+ },
16
+ "./core": {
17
+ "types": "./dist/core/index.d.mts",
18
+ "import": "./dist/core/index.mjs"
19
+ },
20
+ "./action": {
21
+ "types": "./dist/action/index.d.mts",
22
+ "import": "./dist/action/index.mjs"
23
+ }
24
+ },
25
+ "main": "./dist/index.mjs",
26
+ "module": "./dist/index.mjs",
27
+ "types": "./dist/index.d.mts",
28
+ "files": [
29
+ "dist"
30
+ ],
31
+ "scripts": {
32
+ "lint": "eslint",
33
+ "lint:fix": "eslint --fix",
34
+ "build": "tsdown",
35
+ "prepublishOnly": "npm run build"
36
+ },
37
+ "devDependencies": {
38
+ "@antfu/eslint-config": "^6.7.3",
39
+ "eslint": "^9.39.2",
40
+ "eslint-plugin-format": "^1.1.0",
41
+ "tsdown": "^0.18.2"
42
+ }
27
43
  }
package/bun.lock DELETED
@@ -1,30 +0,0 @@
1
- {
2
- "lockfileVersion": 1,
3
- "workspaces": {
4
- "": {
5
- "name": "resultfier",
6
- "devDependencies": {
7
- "@types/bun": "latest",
8
- "typescript": "^5.9.2",
9
- },
10
- "peerDependencies": {
11
- "typescript": "^5.9.2",
12
- },
13
- },
14
- },
15
- "packages": {
16
- "@types/bun": ["@types/bun@1.2.21", "", { "dependencies": { "bun-types": "1.2.21" } }, "sha512-NiDnvEqmbfQ6dmZ3EeUO577s4P5bf4HCTXtI6trMc6f6RzirY5IrF3aIookuSpyslFzrnvv2lmEWv5HyC1X79A=="],
17
-
18
- "@types/node": ["@types/node@24.3.1", "", { "dependencies": { "undici-types": "~7.10.0" } }, "sha512-3vXmQDXy+woz+gnrTvuvNrPzekOi+Ds0ReMxw0LzBiK3a+1k0kQn9f2NWk+lgD4rJehFUmYy2gMhJ2ZI+7YP9g=="],
19
-
20
- "@types/react": ["@types/react@19.1.12", "", { "dependencies": { "csstype": "^3.0.2" } }, "sha512-cMoR+FoAf/Jyq6+Df2/Z41jISvGZZ2eTlnsaJRptmZ76Caldwy1odD4xTr/gNV9VLj0AWgg/nmkevIyUfIIq5w=="],
21
-
22
- "bun-types": ["bun-types@1.2.21", "", { "dependencies": { "@types/node": "*" }, "peerDependencies": { "@types/react": "^19" } }, "sha512-sa2Tj77Ijc/NTLS0/Odjq/qngmEPZfbfnOERi0KRUYhT9R8M4VBioWVmMWE5GrYbKMc+5lVybXygLdibHaqVqw=="],
23
-
24
- "csstype": ["csstype@3.1.3", "", {}, "sha512-M1uQkMl8rQK/szD0LNhtqxIPLpimGm8sOBwU7lLnCpSbTyY3yeU1Vc7l4KT5zT4s/yOxHH5O7tIuuLOCnLADRw=="],
25
-
26
- "typescript": ["typescript@5.9.2", "", { "bin": { "tsc": "bin/tsc", "tsserver": "bin/tsserver" } }, "sha512-CWBzXQrc/qOkhidw1OzBTQuYRbfyxDXJMVJ1XNwUHGROVmuaeiEm3OslpZ1RV96d7SKKjZKrSJu3+t/xlw3R9A=="],
27
-
28
- "undici-types": ["undici-types@7.10.0", "", {}, "sha512-t5Fy/nfn+14LuOc2KNYg75vZqClpAiqscVvMygNnlsHBFpSXdJaYtXMcdNLpl/Qvc3P2cB3s6lOV51nqsFq4ag=="],
29
- }
30
- }
package/lib/core/err.ts DELETED
@@ -1,93 +0,0 @@
1
- import { type Result, ResultBase } from "./result";
2
-
3
- /**
4
- * Represents an error result containing an error value.
5
- */
6
- export class Err<E> extends ResultBase<never, E> {
7
- readonly _tag = "err" as const;
8
- readonly isOk = false;
9
- readonly isErr = true;
10
-
11
- constructor(readonly error: E) {
12
- super();
13
- }
14
-
15
- /**
16
- * Maps the value type, but since this is `Err`, it's a no-op.
17
- */
18
- map<U>(_fn: (value: never) => U): Err<E> {
19
- return this;
20
- }
21
-
22
- /**
23
- * Maps the contained error using the provided function.
24
- * Since this is `Err`, the function is always applied.
25
- */
26
- mapErr<F>(fn: (error: E) => F): Err<F> {
27
- return new Err(fn(this.error));
28
- }
29
-
30
- /**
31
- * Throws an error since this is `Err`, not `Ok`.
32
- */
33
- unwrap(): never {
34
- throw new Error(`Called unwrap() on an Err value: ${this.error}`);
35
- }
36
-
37
- /**
38
- * Returns the contained error.
39
- */
40
- unwrapErr(): E {
41
- return this.error;
42
- }
43
-
44
- /**
45
- * Returns the provided default value since this is `Err`.
46
- */
47
- unwrapOr<T>(defaultValue: T): T {
48
- return defaultValue;
49
- }
50
-
51
- /**
52
- * Computes and returns the fallback value using the provided function.
53
- */
54
- unwrapOrElse<T>(fn: (error: E) => T): T {
55
- return fn(this.error);
56
- }
57
-
58
- /**
59
- * Returns this `Err` unchanged since there's no value to chain.
60
- */
61
- andThen<U>(_fn: (value: never) => Result<U, E>): Err<E> {
62
- return this;
63
- }
64
-
65
- /**
66
- * Always returns false since this is `Err`, not `Ok`.
67
- */
68
- contains(_value: never): boolean {
69
- return false;
70
- }
71
-
72
- /**
73
- * Checks if the contained error matches the provided error.
74
- */
75
- containsErr(error: E): boolean {
76
- return this.error === error;
77
- }
78
- }
79
-
80
- /**
81
- * Creates a new `Err` result with the given error.
82
- */
83
- export function err<E>(error: E): Err<E> {
84
- return new Err(error);
85
- }
86
-
87
- /**
88
- * Type guard to check if a result is `Err`.
89
- * @deprecated use `.isErr` instead
90
- */
91
- export function isErr<T, E>(result: Result<T, E>): result is Err<E> {
92
- return result.isErr;
93
- }
package/lib/core/index.ts DELETED
@@ -1,3 +0,0 @@
1
- export { err, isErr, Err } from "./err";
2
- export { ok, isOk, Ok } from "./ok";
3
- export type { Result, AsyncResult } from "./result";
package/lib/core/ok.ts DELETED
@@ -1,93 +0,0 @@
1
- import { type Result, ResultBase } from "./result";
2
-
3
- /**
4
- * Represents a successful result containing a value.
5
- */
6
- export class Ok<T> extends ResultBase<T, never> {
7
- readonly _tag = "ok" as const;
8
- readonly isOk = true;
9
- readonly isErr = false;
10
-
11
- constructor(readonly value: T) {
12
- super();
13
- }
14
-
15
- /**
16
- * Maps the contained value using the provided function.
17
- * Since this is `Ok`, the function is always applied.
18
- */
19
- map<U>(fn: (value: T) => U): Ok<U> {
20
- return new Ok(fn(this.value));
21
- }
22
-
23
- /**
24
- * Maps the error type, but since this is `Ok`, it's a no-op.
25
- */
26
- mapErr<F>(_fn: (error: never) => F): Ok<T> {
27
- return this;
28
- }
29
-
30
- /**
31
- * Returns the contained value.
32
- */
33
- unwrap(): T {
34
- return this.value;
35
- }
36
-
37
- /**
38
- * Throws an error since this is Ok, not Err.
39
- */
40
- unwrapErr(): never {
41
- throw new Error(`Called unwrapErr() on an Ok value: ${this.value}`);
42
- }
43
-
44
- /**
45
- * Returns the contained value, ignoring the default.
46
- */
47
- unwrapOr(_defaultValue: T): T {
48
- return this.value;
49
- }
50
-
51
- /**
52
- * Returns the contained value, ignoring the fallback function.
53
- */
54
- unwrapOrElse(_fn: (error: never) => T): T {
55
- return this.value;
56
- }
57
-
58
- /**
59
- * Applies the function to the contained value and returns the result.
60
- */
61
- andThen<U>(fn: (value: T) => Result<U, never>): Result<U, never> {
62
- return fn(this.value);
63
- }
64
-
65
- /**
66
- * Checks if the contained value matches the provided value.
67
- */
68
- contains(value: T): boolean {
69
- return this.value === value;
70
- }
71
-
72
- /**
73
- * Always returns false since this is `Ok`, not `Err`.
74
- */
75
- override containsErr(_error: never): false {
76
- return false;
77
- }
78
- }
79
-
80
- /**
81
- * Creates a new `Ok` result with the given value.
82
- */
83
- export function ok<T>(value: T): Ok<T> {
84
- return new Ok(value);
85
- }
86
-
87
- /**
88
- * Type guard to check if a result is `Ok`.
89
- * @deprecated use `.isOk` instead
90
- */
91
- export function isOk<T, E>(result: Result<T, E>): result is Ok<T> {
92
- return result.isOk;
93
- }
@@ -1,78 +0,0 @@
1
- import type { Err } from "./err";
2
- import type { Ok } from "./ok";
3
-
4
- /**
5
- * Represents a Result type that can either be a success (`Ok`) or an error (`Err`).
6
- * This is an abstract base class that provides common functionality for both variants.
7
- */
8
- export abstract class ResultBase<T, E> {
9
- /**
10
- * Type guard to check if the result is an `Ok` variant.
11
- */
12
- abstract readonly isOk: boolean;
13
-
14
- /**
15
- * Type guard to check if the result is an `Err` variant.
16
- */
17
- abstract readonly isErr: boolean;
18
-
19
- /**
20
- * Maps a `Result<T, E>` to `Result<U, E>` by applying a function to a contained `Ok` value,
21
- * leaving an `Err` value untouched.
22
- */
23
- abstract map<U>(fn: (value: T) => U): Result<U, E>;
24
-
25
- /**
26
- * Maps a `Result<T, E>` to `Result<T, F>` by applying a function to a contained `Err` value,
27
- * leaving an `Ok` value untouched.
28
- */
29
- abstract mapErr<F>(fn: (error: E) => F): Result<T, F>;
30
-
31
- /**
32
- * Returns the contained `Ok` value, consuming the self value.
33
- * Throws an error if the result is `Err`.
34
- */
35
- abstract unwrap(): T;
36
-
37
- /**
38
- * Returns the contained `Err` value, consuming the self value.
39
- * Throws an error if the result is Ok.
40
- */
41
- abstract unwrapErr(): E;
42
-
43
- /**
44
- * Returns the contained `Ok` value or a provided default.
45
- */
46
- abstract unwrapOr(defaultValue: T): T;
47
-
48
- /**
49
- * Returns the contained `Ok` value or computes it from a closure.
50
- */
51
- abstract unwrapOrElse(fn: (error: E) => T): T;
52
-
53
- /**
54
- * Calls the provided function with the contained value and returns the result.
55
- * If the result is `Err`, the function is not called and the `Err` is returned.
56
- */
57
- abstract andThen<U>(fn: (value: T) => Result<U, E>): Result<U, E>;
58
-
59
- /**
60
- * Returns true if the result is `Ok` and the value inside of it matches a predicate.
61
- */
62
- abstract contains(value: T): boolean;
63
-
64
- /**
65
- * Returns true if the result is `Err` and the error inside of it matches a predicate.
66
- */
67
- abstract containsErr(error: E): boolean;
68
- }
69
-
70
- /**
71
- * Type alias for a `Result` that can be either `Ok<T>` or `Err<E>`.
72
- */
73
- export type Result<T, E> = Ok<T> | Err<E>;
74
-
75
- /**
76
- * Type alias for an async `Result` (Promise-wrapped `Result`).
77
- */
78
- export type AsyncResult<T, E> = Promise<Result<T, E>>;
package/tsconfig.json DELETED
@@ -1,35 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- // Output configuration for npm publishing
4
- "outDir": "./out",
5
- "declaration": true,
6
- "declarationMap": true,
7
- "sourceMap": true,
8
- "rootDir": "lib/",
9
-
10
- // Module system
11
- "target": "ES2020",
12
- "module": "ESNext",
13
- "moduleResolution": "node",
14
- "esModuleInterop": true,
15
- "allowSyntheticDefaultImports": true,
16
-
17
- // Type checking
18
- "strict": true,
19
- "noImplicitAny": true,
20
- "strictNullChecks": true,
21
- "strictFunctionTypes": true,
22
- "noImplicitReturns": true,
23
- "noFallthroughCasesInSwitch": true,
24
- "noUncheckedIndexedAccess": true,
25
-
26
- // Additional checks
27
- "skipLibCheck": true,
28
- "forceConsistentCasingInFileNames": true,
29
-
30
- // Library
31
- "lib": ["ES2020"]
32
- },
33
- "include": ["**/*.ts"],
34
- "exclude": ["node_modules", "out"]
35
- }