From 228c1b7844ed46a49f33b0c2591ac0b8f41843a6 Mon Sep 17 00:00:00 2001 From: Artem Zakharchenko Date: Mon, 25 Mar 2024 12:34:43 +0100 Subject: [PATCH] test: add extra keys response body type --- test/typings/custom-resolver.test-d.ts | 6 +++-- test/typings/http.test-d.ts | 31 ++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/test/typings/custom-resolver.test-d.ts b/test/typings/custom-resolver.test-d.ts index ed2e520bb..18d30a6e4 100644 --- a/test/typings/custom-resolver.test-d.ts +++ b/test/typings/custom-resolver.test-d.ts @@ -35,10 +35,12 @@ it('custom http resolver has correct parameters type', () => { http.get<{ id: string }, never, 'hello'>( '/user/:id', - // @ts-expect-error Response body doesn't match the response type. withDelay(250, ({ params }) => { expectTypeOf(params).toEqualTypeOf<{ id: string }>() - return HttpResponse.text('non-matching') + return HttpResponse.text( + // @ts-expect-error Response body doesn't match the response type. + 'non-matching', + ) }), ) }) diff --git a/test/typings/http.test-d.ts b/test/typings/http.test-d.ts index 8e79cf8e9..f782db09f 100644 --- a/test/typings/http.test-d.ts +++ b/test/typings/http.test-d.ts @@ -95,6 +95,37 @@ it('supports narrow object as a response body generic argument', () => { }) }) +it('supports object with extra keys as a response body generic argument', () => { + type ResponseBody = { + [key: string]: number | string + id: 123 + } + + http.get('/user', () => { + return HttpResponse.json({ + id: 123, + // Extra keys are allowed if they satisfy the index signature. + name: 'John', + }) + }) + + http.get('/user', () => { + return HttpResponse.json({ + // @ts-expect-error Must be 123. + id: 456, + name: 'John', + }) + }) + + http.get('/user', () => { + return HttpResponse.json({ + id: 123, + // @ts-expect-error Must satisfy the index signature. + name: { a: 1 }, + }) + }) +}) + it('supports response body generic declared via type', () => { type ResponseBodyType = { id: number } http.get('/user', () => {