From 1b469bf527948c8ebdbbb9bf51633c02816dc59d Mon Sep 17 00:00:00 2001 From: Qayyuum Harun Date: Wed, 20 Nov 2024 10:03:46 +0100 Subject: [PATCH 1/2] Update return type --- types/retry-handler.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 { /** From 9add66317b8d069377659782e1716dbe48d11ec3 Mon Sep 17 00:00:00 2001 From: Qayyuum Harun Date: Wed, 20 Nov 2024 21:41:51 +0100 Subject: [PATCH 2/2] add type tests --- test/types/retry-handler.test-d.ts | 49 ++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) 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)