request-clone.ts
This file
Path
test-d/request-clone.ts
19 lines · 713 bytes · TypeScript
Last changed by
Preserve JSON types when cloning requests and responsesSindre Sorhusbefore adoptionf4886ca
History of this fileTake it
1234567891011121314151617181920import {expectTypeOf} from 'expect-type';import ky, {type KyRequest} from 'ky';
const request: KyRequest<{name: string}> = new Request('https://example.com');const clone = request.clone();
expectTypeOf(clone.json()).toEqualTypeOf<Promise<{name: string}>>();expectTypeOf(clone.clone().json()).toEqualTypeOf<Promise<{name: string}>>();expectTypeOf(clone.json<{id: number}>()).toEqualTypeOf<Promise<{id: number}>>();
void ky.post('https://example.com', { json: {name: 'Ada'}, hooks: { beforeRequest: [({request}) => { expectTypeOf(request.clone().json()).toEqualTypeOf<Promise<unknown>>(); expectTypeOf(request.clone().json<{name: string}>()).toEqualTypeOf<Promise<{name: string}>>(); }], },});