Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update return type of RetryCallback #3851

Merged
merged 2 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading