@firtoz/maybe-error 1.0.0 → 1.0.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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/MaybeError.ts +119 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@firtoz/maybe-error",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "Type-safe result handling with MaybeError pattern",
5
5
  "main": "./src/index.ts",
6
6
  "module": "./src/index.ts",
package/src/MaybeError.ts CHANGED
@@ -1,8 +1,61 @@
1
+ /**
2
+ * @fileoverview Type-safe error handling utilities using discriminated unions
3
+ *
4
+ * This module provides a Result-like pattern for handling operations that may fail,
5
+ * inspired by Rust's Result type and functional programming error handling patterns.
6
+ *
7
+ * @example
8
+ * ```typescript
9
+ * // Basic usage
10
+ * const result = success(42);
11
+ * const error = fail("Something went wrong");
12
+ *
13
+ * // Type-safe error handling
14
+ * function divide(a: number, b: number): MaybeError<number> {
15
+ * if (b === 0) return fail("Division by zero");
16
+ * return success(a / b);
17
+ * }
18
+ *
19
+ * const result = divide(10, 2);
20
+ * if (result.success) {
21
+ * console.log(result.result); // 5
22
+ * } else {
23
+ * console.error(result.error); // "Division by zero"
24
+ * }
25
+ * ```
26
+ */
27
+
28
+ /**
29
+ * Represents a failed operation with an error value.
30
+ *
31
+ * @template TError - The type of the error value (defaults to string)
32
+ * @example
33
+ * ```typescript
34
+ * const error: DefiniteError<string> = { success: false, error: "Not found" };
35
+ * const customError: DefiniteError<{code: number, message: string}> = {
36
+ * success: false,
37
+ * error: { code: 404, message: "Resource not found" }
38
+ * };
39
+ * ```
40
+ */
1
41
  export type DefiniteError<TError = string> = {
2
42
  success: false;
3
43
  error: TError;
4
44
  };
5
45
 
46
+ /**
47
+ * Represents a successful operation with an optional result value.
48
+ *
49
+ * Uses conditional types to make the result field optional when T is undefined,
50
+ * but required when T has a concrete type.
51
+ *
52
+ * @template T - The type of the success value (defaults to undefined)
53
+ * @example
54
+ * ```typescript
55
+ * const voidSuccess: DefiniteSuccess = { success: true };
56
+ * const valueSuccess: DefiniteSuccess<number> = { success: true, result: 42 };
57
+ * ```
58
+ */
6
59
  export type DefiniteSuccess<T = undefined> = {
7
60
  success: true;
8
61
  } & (T extends undefined
@@ -13,10 +66,47 @@ export type DefiniteSuccess<T = undefined> = {
13
66
  result: T;
14
67
  });
15
68
 
69
+ /**
70
+ * A discriminated union representing either a successful result or an error.
71
+ *
72
+ * This is the main type for operations that may fail. The `success` field
73
+ * acts as a discriminant, allowing TypeScript to narrow the type in conditionals.
74
+ *
75
+ * @template T - The type of the success value (defaults to undefined)
76
+ * @template TError - The type of the error value (defaults to string)
77
+ * @example
78
+ * ```typescript
79
+ * function fetchUser(id: string): MaybeError<User, ApiError> {
80
+ * // Implementation that returns either success(user) or fail(apiError)
81
+ * }
82
+ *
83
+ * const result = fetchUser("123");
84
+ * if (result.success) {
85
+ * // TypeScript knows result.result is User
86
+ * console.log(result.result.name);
87
+ * } else {
88
+ * // TypeScript knows result.error is ApiError
89
+ * console.error(result.error.message);
90
+ * }
91
+ * ```
92
+ */
16
93
  export type MaybeError<T = undefined, TError = string> =
17
94
  | DefiniteSuccess<T>
18
95
  | DefiniteError<TError>;
19
96
 
97
+ /**
98
+ * Utility type to extract the success value type from a MaybeError type.
99
+ *
100
+ * Useful for type manipulation when you need to work with the success type
101
+ * without the error handling wrapper.
102
+ *
103
+ * @template T - A MaybeError type
104
+ * @example
105
+ * ```typescript
106
+ * type UserResult = MaybeError<User, string>;
107
+ * type UserType = AssumeSuccess<UserResult>; // User
108
+ * ```
109
+ */
20
110
  export type AssumeSuccess<T extends MaybeError<unknown>> = Exclude<
21
111
  T,
22
112
  undefined
@@ -24,6 +114,22 @@ export type AssumeSuccess<T extends MaybeError<unknown>> = Exclude<
24
114
  ? U
25
115
  : never;
26
116
 
117
+ /**
118
+ * Creates a successful result with an optional value.
119
+ *
120
+ * Uses function overloading to provide different signatures based on whether
121
+ * a value is provided or not.
122
+ *
123
+ * @template T - The type of the success value
124
+ * @param params - The success value (optional for void operations)
125
+ * @returns A DefiniteSuccess instance
126
+ * @example
127
+ * ```typescript
128
+ * const voidResult = success(); // DefiniteSuccess<undefined>
129
+ * const valueResult = success(42); // DefiniteSuccess<number>
130
+ * const stringResult = success("hello"); // DefiniteSuccess<string>
131
+ * ```
132
+ */
27
133
  export const success = <T = undefined>(
28
134
  ...params: T extends undefined ? [] : [T]
29
135
  ): DefiniteSuccess<T> => {
@@ -37,6 +143,19 @@ export const success = <T = undefined>(
37
143
  } as unknown as DefiniteSuccess<T>;
38
144
  };
39
145
 
146
+ /**
147
+ * Creates a failed result with an error value.
148
+ *
149
+ * @template TError - The type of the error value
150
+ * @param error - The error value
151
+ * @returns A DefiniteError instance
152
+ * @example
153
+ * ```typescript
154
+ * const stringError = fail("Something went wrong");
155
+ * const objectError = fail({ code: 500, message: "Internal server error" });
156
+ * const customError = fail(new Error("Custom error"));
157
+ * ```
158
+ */
40
159
  export const fail = <TError = string>(error: TError): DefiniteError<TError> => {
41
160
  return {
42
161
  success: false,