diff --git a/test/types/retry-handler.test-d.ts b/test/types/retry-handler.test-d.ts new file mode 100644 index 00000000000..8dac930fa98 --- /dev/null +++ b/test/types/retry-handler.test-d.ts @@ -0,0 +1,49 @@ +import { expectType, expectAssignable, expectNotAssignable } from 'tsd' +import { Dispatcher, RetryHandler } from '../..' + +// Test the basic structure of RetryCallback +expectType((err, context, callback) => { + expectType(err) + expectType<{ + state: RetryHandler.RetryState; + opts: Dispatcher.DispatchOptions & { + retryOptions?: RetryHandler.RetryOptions; + }; + }>(context) + expectType(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(testCallback(new Error(), testContext, () => {})) + +// Test that the function is assignable to RetryCallback +expectAssignable(testCallback) + +// Test that an incorrectly typed function is not assignable to RetryCallback +expectNotAssignable((() => {}) as ( + err: string, + context: number, + callback: boolean +) => void) + +// Test the nested types +const contextTest: Parameters[1] = { + state: {} as RetryHandler.RetryState, + opts: { + method: 'GET', + path: 'some-path', + retryOptions: {} as RetryHandler.RetryOptions + } +} +expectType(contextTest.state) +expectType< + Dispatcher.DispatchOptions & { retryOptions?: RetryHandler.RetryOptions } +>(contextTest.opts) diff --git a/types/retry-handler.d.ts b/types/retry-handler.d.ts index eedd353bfce..c4471cd6189 100644 --- a/types/retry-handler.d.ts +++ b/types/retry-handler.d.ts @@ -32,7 +32,7 @@ declare namespace RetryHandler { }; }, callback: OnRetryCallback - ) => number | null + ) => void export interface RetryOptions { /**