Skip to content

Commit

Permalink
Update return type of RetryCallback (#3851)
Browse files Browse the repository at this point in the history
* Update return type

* add type tests
  • Loading branch information
mqayyuum authored Nov 22, 2024
1 parent e49b575 commit a1fb2cc
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
49 changes: 49 additions & 0 deletions test/types/retry-handler.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { expectType, expectAssignable, expectNotAssignable } from 'tsd'
import { Dispatcher, RetryHandler } from '../..'

// Test the basic structure of RetryCallback
expectType<RetryHandler.RetryCallback>((err, context, callback) => {
expectType<Error>(err)
expectType<{
state: RetryHandler.RetryState;
opts: Dispatcher.DispatchOptions & {
retryOptions?: RetryHandler.RetryOptions;
};
}>(context)
expectType<RetryHandler.OnRetryCallback>(callback)
})

// Test that RetryCallback returns void
const testCallback = (() => {}) as RetryHandler.RetryCallback
const testContext = {
state: {} as RetryHandler.RetryState,
opts: {} as Dispatcher.DispatchOptions & {
retryOptions?: RetryHandler.RetryOptions;
}
}

expectType<void>(testCallback(new Error(), testContext, () => {}))

// Test that the function is assignable to RetryCallback
expectAssignable<RetryHandler.RetryCallback>(testCallback)

// Test that an incorrectly typed function is not assignable to RetryCallback
expectNotAssignable<RetryHandler.RetryCallback>((() => {}) as (
err: string,
context: number,
callback: boolean
) => void)

// Test the nested types
const contextTest: Parameters<RetryHandler.RetryCallback>[1] = {
state: {} as RetryHandler.RetryState,
opts: {
method: 'GET',
path: 'some-path',
retryOptions: {} as RetryHandler.RetryOptions
}
}
expectType<RetryHandler.RetryState>(contextTest.state)
expectType<
Dispatcher.DispatchOptions & { retryOptions?: RetryHandler.RetryOptions }
>(contextTest.opts)
2 changes: 1 addition & 1 deletion types/retry-handler.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ declare namespace RetryHandler {
};
},
callback: OnRetryCallback
) => number | null
) => void

export interface RetryOptions {
/**
Expand Down

0 comments on commit a1fb2cc

Please sign in to comment.