File tree Expand file tree Collapse file tree 2 files changed +18
-3
lines changed Expand file tree Collapse file tree 2 files changed +18
-3
lines changed Original file line number Diff line number Diff line change @@ -41,7 +41,7 @@ const createGoError = <E extends Error>(err: unknown): GoResultError<E> => {
41
41
return fail ( new GoWrappedError ( err ) ) ;
42
42
} ;
43
43
44
- export const goSync = < T , E extends Error > ( fn : ( ) => T ) : GoResult < T , E > => {
44
+ export const goSync = < T , E extends Error = Error > ( fn : ( ) => T ) : GoResult < T , E > => {
45
45
try {
46
46
return success ( fn ( ) ) ;
47
47
} catch ( err ) {
@@ -72,8 +72,8 @@ export interface GoAsyncOptions {
72
72
timeoutMs : number ;
73
73
}
74
74
75
- export const go = async < T , E extends Error > (
76
- fn : ( ) => T ,
75
+ export const go = async < T , E extends Error = Error > (
76
+ fn : ( ) => Promise < T > ,
77
77
options ?: GoAsyncOptions
78
78
) : Promise < GoResult < Awaited < T > , E > > => {
79
79
try {
Original file line number Diff line number Diff line change @@ -100,6 +100,21 @@ describe('basic go usage', () => {
100
100
} ) ;
101
101
expect ( res ) . toEqual ( fail ( err ) ) ;
102
102
} ) ;
103
+
104
+ it ( 'allows to specify the type of the returned value' , async ( ) => {
105
+ const goResOnlyData = await go < string | number > ( ( ) => {
106
+ return Promise . resolve ( 123 ) ;
107
+ } ) ;
108
+ assertGoSuccess ( goResOnlyData ) ;
109
+ expectType < string | number > ( goResOnlyData . data ) ;
110
+
111
+ class CustomError extends Error { }
112
+ const goResBoth = await go < string | number , CustomError > ( ( ) => {
113
+ return Promise . resolve ( 123 ) ;
114
+ } ) ;
115
+ assertGoSuccess ( goResBoth ) ;
116
+ expectType < string | number > ( goResBoth . data ) ;
117
+ } ) ;
103
118
} ) ;
104
119
105
120
describe ( 'basic timeout usage' , ( ) => {
You can’t perform that action at this time.
0 commit comments