Skip to content
Myr

retry.ts

This file

Path

test-d/retry.ts

42 lines · 1.3 KiB · TypeScript

Last changed by

Support readonly inputs and explicit undefined in TypeScript optionsSindre Sorhusbefore adoption51b3177

History of this file

Take it

Support readonly inputs and explicit undefined in TypeScript optionsSindre Sorhusbefore adoption51b3177

lines 1–42why, one step back
1import {expectTypeOf} from 'expect-type';
2import type {InitHook, RetryOptions} from 'ky';34const retryOptions: RetryOptions = {5 methods: ['get'] as const,6 statusCodes: [503] as const,7 afterStatusCodes: [503] as const,8};910const methods = ['get'] as const;11const statusCodes = [503] as const;12const afterStatusCodes = [503] as const;13const readonlyRetryOptions: RetryOptions = {methods, statusCodes, afterStatusCodes};1415expectTypeOf(readonlyRetryOptions).toEqualTypeOf(retryOptions);1617const initHook: InitHook = options => {18 if (typeof options.retry === 'object') {19 options.retry.methods?.push('post');20 options.retry.statusCodes?.push(500);21 options.retry.afterStatusCodes?.push(503);22 options.retry.methods = undefined;23 options.retry.statusCodes = undefined;24 options.retry.afterStatusCodes = undefined;25 }26};2728expectTypeOf(initHook).toEqualTypeOf<InitHook>();2930const defaultRetryDelay: RetryOptions = {delay: undefined};31expectTypeOf(defaultRetryDelay.delay).toMatchTypeOf<RetryOptions['delay']>();3233const resetRetryOptions: RetryOptions[] = [34 {limit: undefined},35 {methods: undefined},36 {statusCodes: undefined},37 {afterStatusCodes: undefined},38 {maxRetryAfter: undefined},39 {backoffLimit: undefined},40 {retryOnTimeout: undefined},41];42expectTypeOf(resetRetryOptions).toEqualTypeOf<RetryOptions[]>();