1
- type Branded < T > = T & { __tryCatchTupleResult : any } ;
2
- type Success < T > = Branded < [ data : T , error : null ] > ;
3
- type Failure < E > = Branded < [ data : null , error : E | Error ] > ;
4
- type Result < T , E > = Success < T > | Failure < E > ;
1
+ type Branded < T > = T & { __tryCatchTupleResult : never } ;
2
+ type DisableArrayMethods < T > = T & {
3
+ [ K in Exclude < keyof Array < any > , "length" | symbol > ] : never ;
4
+ } ;
5
+
6
+ type DataErrorTuple < T , E > = Branded <
7
+ DisableArrayMethods < [ data : T , error : E ] & never [ ] >
8
+ > ;
9
+
10
+ /**
11
+ * Represents a successful result where `data` is present and `error` is `null`.
12
+ */
13
+ export type Success < T > = DataErrorTuple < T , null > ;
14
+
15
+ /**
16
+ * Represents a failure result where `error` contains an error instance and `data` is `null`.
17
+ */
18
+ export type Failure < E extends Error > = DataErrorTuple < null , E | Error > ;
19
+
20
+ /**
21
+ * Represents the result of an operation that can either succeed with `T` or fail with `E`.
22
+ */
23
+ export type Result < T , E extends Error > = Success < T > | Failure < E > ;
5
24
6
- type TryCatchResult < T , E > = T extends Promise < infer U >
25
+ /**
26
+ * Resolves the return type based on whether `T` is a promise:
27
+ * - If `T` is a `Promise<U>`, returns `Promise<Result<U, E>>`.
28
+ * - Otherwise, returns `Result<T, E>`.
29
+ */
30
+ export type TryCatchResult < T , E extends Error > = T extends Promise < infer U >
7
31
? Promise < Result < U , E > >
8
32
: Result < T , E > ;
9
33
10
- type TryCatchFunc < E_ extends Error = never > = < T , E extends Error = E_ > (
11
- fn ?: T | ( ( ) => T ) ,
34
+ /**
35
+ * Function type for handling try-catch logic.
36
+ *
37
+ * @template E_ Default error type.
38
+ * @template T The return type of the function being executed.
39
+ * @template E The error type that extends the default error type.
40
+ *
41
+ * @param fn A function, promise, or value to execute within a try-catch block.
42
+ * @param operationName Optional name added to `error.message` for better debugging and context.
43
+ */
44
+ export type TryCatchFunc < E_ extends Error = Error > = < T , E extends Error = E_ > (
45
+ fn : T | ( ( ) => T ) ,
12
46
operationName ?: string ,
13
47
) => TryCatchResult < T , E > ;
14
48
15
- type TryCatch <
49
+ /**
50
+ * A utility for handling synchronous and asynchronous operations within a try-catch block.
51
+ *
52
+ * @template F The function type for try-catch execution.
53
+ * @template E_ The base error type.
54
+ */
55
+ export type TryCatch <
16
56
F extends TryCatchFunc = TryCatchFunc ,
17
- E_ extends Error = never ,
57
+ E_ extends Error = Error ,
18
58
> = F & {
59
+ /**
60
+ * Executes a synchronous function inside a try-catch block.
61
+ *
62
+ * @param fn The function to execute.
63
+ * @param operationName Optional name added to `error.message` for better debugging and context.
64
+ * @returns A `Result<T, E>` indicating success or failure.
65
+ */
19
66
sync : < T , E extends Error = E_ > (
20
67
fn : ( ) => T ,
21
68
operationName ?: string ,
22
69
) => Result < T , E > ;
70
+
71
+ /**
72
+ * Executes an asynchronous function inside a try-catch block.
73
+ *
74
+ * @param fn The function or promise to execute.
75
+ * @param operationName Optional name added to `error.message` for better debugging and context.
76
+ * @returns A `Promise<Result<T, E>>` indicating success or failure.
77
+ */
23
78
async : < T , E extends Error = E_ > (
24
79
fn : Promise < T > | ( ( ) => Promise < T > ) ,
25
80
operationName ?: string ,
26
81
) => Promise < Result < T , E > > ;
82
+
83
+ /**
84
+ * Creates a new `TryCatch` instance that handles additional error types.
85
+ *
86
+ * @template E Extends the existing error type.
87
+ * @returns A new `TryCatch` instance with extended error handling capabilities.
88
+ */
27
89
errors : < E extends Error > ( ) => TryCatch < TryCatchFunc < E | E_ > , E | E_ > ;
28
90
} ;
29
91
30
92
/**
31
93
* tryCatch - Error handling that can be synchronous or asynchronous
32
94
* based on the input function.
33
95
*
34
- * @param fn Function to execute, Promise or value.
35
- * @param operationName Optional name for context.
96
+ * @param fn A function, promise, or value to execute within a try-catch block .
97
+ * @param operationName Optional name added to `error.message` for better debugging and context.
36
98
* @returns A Result, or a Promise resolving to a Result, depending on fn.
37
99
*/
38
- export const tryCatch : TryCatch = < T , E extends Error = never > (
39
- fn ? : T | ( ( ) => T ) ,
100
+ export const tryCatch : TryCatch = < T , E extends Error = Error > (
101
+ fn : T | ( ( ) => T ) ,
40
102
operationName ?: string ,
41
103
) => {
42
104
try {
@@ -51,7 +113,7 @@ export const tryCatch: TryCatch = <T, E extends Error = never>(
51
113
}
52
114
} ;
53
115
54
- export const tryCatchSync : TryCatch [ "sync" ] = < T , E extends Error = never > (
116
+ export const tryCatchSync : TryCatch [ "sync" ] = < T , E extends Error = Error > (
55
117
fn : ( ) => T ,
56
118
operationName ?: string ,
57
119
) => {
@@ -65,7 +127,7 @@ export const tryCatchSync: TryCatch["sync"] = <T, E extends Error = never>(
65
127
66
128
export const tryCatchAsync : TryCatch [ "async" ] = async <
67
129
T ,
68
- E extends Error = never ,
130
+ E extends Error = Error ,
69
131
> (
70
132
fn : Promise < T > | ( ( ) => Promise < T > ) ,
71
133
operationName ?: string ,
0 commit comments