From 2414bc9f7d651f830902af00238e1b11d9a389dc Mon Sep 17 00:00:00 2001 From: Carlos Fuentes Date: Mon, 25 Nov 2024 18:17:29 +0100 Subject: [PATCH] Update return type of RetryCallback (#3851) (#3876) * Update return type * add type tests (cherry picked from commit a1fb2cc17b4055898bdc7251a903d7e7d62a4387) Co-authored-by: Qayyuum Harun <69181532+mqayyuum@users.noreply.github.com> --- test/types/retry-handler.test-d.ts | 49 ++++++++++++++++++++++++++++++ types/retry-handler.d.ts | 2 +- 2 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 test/types/retry-handler.test-d.ts 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 e44b207c221..6cb34c12d6d 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 { /**