Skip to content

Commit b51a0af

Browse files
committed
Improve manual TS typings for go utils
1 parent e60c7b6 commit b51a0af

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

src/go/go.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ const createGoError = <E extends Error>(err: unknown): GoResultError<E> => {
4141
return fail(new GoWrappedError(err));
4242
};
4343

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> => {
4545
try {
4646
return success(fn());
4747
} catch (err) {
@@ -72,8 +72,8 @@ export interface GoAsyncOptions {
7272
timeoutMs: number;
7373
}
7474

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>,
7777
options?: GoAsyncOptions
7878
): Promise<GoResult<Awaited<T>, E>> => {
7979
try {

test/go.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,21 @@ describe('basic go usage', () => {
100100
});
101101
expect(res).toEqual(fail(err));
102102
});
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+
});
103118
});
104119

105120
describe('basic timeout usage', () => {

0 commit comments

Comments
 (0)