|
| 1 | +import { expectType, expectAssignable, expectNotAssignable } from 'tsd' |
| 2 | +import { Dispatcher, RetryHandler } from '../..' |
| 3 | + |
| 4 | +// Test the basic structure of RetryCallback |
| 5 | +expectType<RetryHandler.RetryCallback>((err, context, callback) => { |
| 6 | + expectType<Error>(err) |
| 7 | + expectType<{ |
| 8 | + state: RetryHandler.RetryState; |
| 9 | + opts: Dispatcher.DispatchOptions & { |
| 10 | + retryOptions?: RetryHandler.RetryOptions; |
| 11 | + }; |
| 12 | + }>(context) |
| 13 | + expectType<RetryHandler.OnRetryCallback>(callback) |
| 14 | +}) |
| 15 | + |
| 16 | +// Test that RetryCallback returns void |
| 17 | +const testCallback = (() => {}) as RetryHandler.RetryCallback |
| 18 | +const testContext = { |
| 19 | + state: {} as RetryHandler.RetryState, |
| 20 | + opts: {} as Dispatcher.DispatchOptions & { |
| 21 | + retryOptions?: RetryHandler.RetryOptions; |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +expectType<void>(testCallback(new Error(), testContext, () => {})) |
| 26 | + |
| 27 | +// Test that the function is assignable to RetryCallback |
| 28 | +expectAssignable<RetryHandler.RetryCallback>(testCallback) |
| 29 | + |
| 30 | +// Test that an incorrectly typed function is not assignable to RetryCallback |
| 31 | +expectNotAssignable<RetryHandler.RetryCallback>((() => {}) as ( |
| 32 | + err: string, |
| 33 | + context: number, |
| 34 | + callback: boolean |
| 35 | +) => void) |
| 36 | + |
| 37 | +// Test the nested types |
| 38 | +const contextTest: Parameters<RetryHandler.RetryCallback>[1] = { |
| 39 | + state: {} as RetryHandler.RetryState, |
| 40 | + opts: { |
| 41 | + method: 'GET', |
| 42 | + path: 'some-path', |
| 43 | + retryOptions: {} as RetryHandler.RetryOptions |
| 44 | + } |
| 45 | +} |
| 46 | +expectType<RetryHandler.RetryState>(contextTest.state) |
| 47 | +expectType< |
| 48 | + Dispatcher.DispatchOptions & { retryOptions?: RetryHandler.RetryOptions } |
| 49 | +>(contextTest.opts) |
0 commit comments