Skip to content
Myr

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 file

Take it

Preserve JSON types when cloning requests and responsesSindre Sorhusbefore adoptionf4886ca

lines 1–19why, one step back
1import {expectTypeOf} from 'expect-type';
2import ky, {type KyRequest} from 'ky';34const request: KyRequest<{name: string}> = new Request('https://example.com');5const clone = request.clone();67expectTypeOf(clone.json()).toEqualTypeOf<Promise<{name: string}>>();8expectTypeOf(clone.clone().json()).toEqualTypeOf<Promise<{name: string}>>();9expectTypeOf(clone.json<{id: number}>()).toEqualTypeOf<Promise<{id: number}>>();1011void ky.post('https://example.com', {12 json: {name: 'Ada'},13 hooks: {14 beforeRequest: [({request}) => {15 expectTypeOf(request.clone().json()).toEqualTypeOf<Promise<unknown>>();16 expectTypeOf(request.clone().json<{name: string}>()).toEqualTypeOf<Promise<{name: string}>>();17 }],18 },19});