From 85239bfdd309f3509140f76a5b3dd808320ba5a0 Mon Sep 17 00:00:00 2001 From: Yusuke Wada Date: Fri, 31 Jan 2025 14:31:05 +0900 Subject: [PATCH] fix(client): correct inferring empty object `c.json({})` --- src/client/client.test.ts | 12 ++++++++++++ src/client/types.ts | 15 +-------------- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/src/client/client.test.ts b/src/client/client.test.ts index 97292c736..69f59ca18 100644 --- a/src/client/client.test.ts +++ b/src/client/client.test.ts @@ -50,6 +50,7 @@ describe('Basic - JSON', () => { ) .get('/hello-not-found', (c) => c.notFound()) .get('/null', (c) => c.json(null)) + .get('/empty', (c) => c.json({})) type AppType = typeof route @@ -77,6 +78,9 @@ describe('Basic - JSON', () => { http.get('http://localhost/null', () => { return HttpResponse.json(null) }), + http.get('http://localhost/empty', () => { + return HttpResponse.json({}) + }), http.get('http://localhost/api/string', () => { return HttpResponse.json('a-string') }), @@ -139,6 +143,14 @@ describe('Basic - JSON', () => { expect(data).toBe(null) }) + it('Should get a `{}` content', async () => { + const client = hc('http://localhost') + const res = await client.empty.$get() + const data = await res.json() + expectTypeOf(data).toMatchTypeOf<{}>() + expect(data).toStrictEqual({}) + }) + it('Should have correct types - primitives', async () => { const app = new Hono() const route = app diff --git a/src/client/types.ts b/src/client/types.ts index 29e5790e1..a7cbe2e6f 100644 --- a/src/client/types.ts +++ b/src/client/types.ts @@ -54,15 +54,6 @@ export type ClientRequest = { : {} : {}) -// eslint-disable-next-line @typescript-eslint/no-explicit-any -type BlankRecordToNever = T extends any - ? T extends null - ? null - : keyof T extends never - ? never - : T - : never - type ClientResponseOfEndpoint = T extends { output: infer O outputFormat: infer F @@ -89,11 +80,7 @@ export interface ClientResponse< url: string redirect(url: string, status: number): Response clone(): Response - json(): F extends 'text' - ? Promise - : F extends 'json' - ? Promise> - : Promise + json(): F extends 'text' ? Promise : F extends 'json' ? Promise : Promise text(): F extends 'text' ? (T extends string ? Promise : Promise) : Promise blob(): Promise formData(): Promise