Skip to content

Commit

Permalink
fix(client): infer null correctly (#2469)
Browse files Browse the repository at this point in the history
* fix(client): infer `null` correctly

* denoify
  • Loading branch information
yusukebe authored Apr 5, 2024
1 parent 35f15b7 commit 837f16b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
8 changes: 7 additions & 1 deletion deno_dist/client/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,13 @@ export type ClientRequest<S extends Schema> = {
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
type BlankRecordToNever<T> = T extends any ? (keyof T extends never ? never : T) : never
type BlankRecordToNever<T> = T extends any
? T extends null
? null
: keyof T extends never
? never
: T
: never

export interface ClientResponse<T> {
readonly body: ReadableStream | null
Expand Down
12 changes: 12 additions & 0 deletions src/client/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ describe('Basic - JSON', () => {
}
)
.get('/hello-not-found', (c) => c.notFound())
.get('/null', (c) => c.json(null))

type AppType = typeof route

Expand All @@ -69,6 +70,9 @@ describe('Basic - JSON', () => {
}),
rest.get('http://localhost/hello-not-found', (_req, res, ctx) => {
return res(ctx.status(404))
}),
rest.get('http://localhost/null', (_req, res, ctx) => {
return res(ctx.status(200), ctx.json(null))
})
)

Expand Down Expand Up @@ -111,6 +115,14 @@ describe('Basic - JSON', () => {
const res = await client['hello-not-found'].$get()
expect(res.status).toBe(404)
})

it('Should get a `null` content', async () => {
const client = hc<AppType>('http://localhost')
const res = await client.null.$get()
const data = await res.json()
expectTypeOf(data).toMatchTypeOf<null>()
expect(data).toBe(null)
})
})

describe('Basic - query, queries, form, path params, header and cookie', () => {
Expand Down
8 changes: 7 additions & 1 deletion src/client/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,13 @@ export type ClientRequest<S extends Schema> = {
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
type BlankRecordToNever<T> = T extends any ? (keyof T extends never ? never : T) : never
type BlankRecordToNever<T> = T extends any
? T extends null
? null
: keyof T extends never
? never
: T
: never

export interface ClientResponse<T> {
readonly body: ReadableStream | null
Expand Down

0 comments on commit 837f16b

Please sign in to comment.