Skip to content

Commit 2414bc9

Browse files
metcoder95mqayyuum
andauthored
Update return type of RetryCallback (#3851) (#3876)
* Update return type * add type tests (cherry picked from commit a1fb2cc) Co-authored-by: Qayyuum Harun <[email protected]>
1 parent be8cd0a commit 2414bc9

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

test/types/retry-handler.test-d.ts

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { expectType, expectAssignable, expectNotAssignable } from 'tsd'
2+
import { Dispatcher, RetryHandler } from '../..'
3+
4+
// Test the basic structure of RetryCallback
5+
expectType<RetryHandler.RetryCallback>((err, context, callback) => {
6+
expectType<Error>(err)
7+
expectType<{
8+
state: RetryHandler.RetryState;
9+
opts: Dispatcher.DispatchOptions & {
10+
retryOptions?: RetryHandler.RetryOptions;
11+
};
12+
}>(context)
13+
expectType<RetryHandler.OnRetryCallback>(callback)
14+
})
15+
16+
// Test that RetryCallback returns void
17+
const testCallback = (() => {}) as RetryHandler.RetryCallback
18+
const testContext = {
19+
state: {} as RetryHandler.RetryState,
20+
opts: {} as Dispatcher.DispatchOptions & {
21+
retryOptions?: RetryHandler.RetryOptions;
22+
}
23+
}
24+
25+
expectType<void>(testCallback(new Error(), testContext, () => {}))
26+
27+
// Test that the function is assignable to RetryCallback
28+
expectAssignable<RetryHandler.RetryCallback>(testCallback)
29+
30+
// Test that an incorrectly typed function is not assignable to RetryCallback
31+
expectNotAssignable<RetryHandler.RetryCallback>((() => {}) as (
32+
err: string,
33+
context: number,
34+
callback: boolean
35+
) => void)
36+
37+
// Test the nested types
38+
const contextTest: Parameters<RetryHandler.RetryCallback>[1] = {
39+
state: {} as RetryHandler.RetryState,
40+
opts: {
41+
method: 'GET',
42+
path: 'some-path',
43+
retryOptions: {} as RetryHandler.RetryOptions
44+
}
45+
}
46+
expectType<RetryHandler.RetryState>(contextTest.state)
47+
expectType<
48+
Dispatcher.DispatchOptions & { retryOptions?: RetryHandler.RetryOptions }
49+
>(contextTest.opts)

types/retry-handler.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ declare namespace RetryHandler {
3232
};
3333
},
3434
callback: OnRetryCallback
35-
) => number | null;
35+
) => void
3636

3737
export interface RetryOptions {
3838
/**

0 commit comments

Comments
 (0)