1
1
type Branded < T > = T & { __tryCatchTupleResult : any } ;
2
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 > ;
3
+ type Failure < E extends Error > = Branded < [ data : null , error : E | Error ] > ;
4
+ type Result < T , E extends Error > = Success < T > | Failure < E > ;
5
5
6
- type TryCatchResult < T , E > = T extends Promise < infer U >
6
+ type TryCatchResult < T , E extends Error > = T extends Promise < infer U >
7
7
? Promise < Result < U , E > >
8
8
: Result < T , E > ;
9
9
10
- type TryCatchFunc < E_ extends Error = never > = < T , E extends Error = E_ > (
10
+ type TryCatchFunc < E_ extends Error = Error > = < T , E extends Error = E_ > (
11
11
fn ?: T | ( ( ) => T ) ,
12
12
operationName ?: string ,
13
13
) => TryCatchResult < T , E > ;
14
14
15
15
type TryCatch <
16
16
F extends TryCatchFunc = TryCatchFunc ,
17
- E_ extends Error = never ,
17
+ E_ extends Error = Error ,
18
18
> = F & {
19
19
sync : < T , E extends Error = E_ > (
20
20
fn : ( ) => T ,
@@ -35,7 +35,7 @@ type TryCatch<
35
35
* @param operationName Optional name for context.
36
36
* @returns A Result, or a Promise resolving to a Result, depending on fn.
37
37
*/
38
- export const tryCatch : TryCatch = < T , E extends Error = never > (
38
+ export const tryCatch : TryCatch = < T , E extends Error = Error > (
39
39
fn ?: T | ( ( ) => T ) ,
40
40
operationName ?: string ,
41
41
) => {
@@ -51,7 +51,7 @@ export const tryCatch: TryCatch = <T, E extends Error = never>(
51
51
}
52
52
} ;
53
53
54
- export const tryCatchSync : TryCatch [ "sync" ] = < T , E extends Error = never > (
54
+ export const tryCatchSync : TryCatch [ "sync" ] = < T , E extends Error = Error > (
55
55
fn : ( ) => T ,
56
56
operationName ?: string ,
57
57
) => {
@@ -65,7 +65,7 @@ export const tryCatchSync: TryCatch["sync"] = <T, E extends Error = never>(
65
65
66
66
export const tryCatchAsync : TryCatch [ "async" ] = async <
67
67
T ,
68
- E extends Error = never ,
68
+ E extends Error = Error ,
69
69
> (
70
70
fn : Promise < T > | ( ( ) => Promise < T > ) ,
71
71
operationName ?: string ,
0 commit comments