Skip to content

Commit

Permalink
fix(types): honojs#2912: interfaces array's respond typed as never
Browse files Browse the repository at this point in the history
  • Loading branch information
NamesMT committed Jun 6, 2024
1 parent e4f7c59 commit f174818
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
25 changes: 25 additions & 0 deletions src/client/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,13 @@ describe('Merge path with `app.route()`', () => {
ok: true,
})
}),
http.get('http://localhost/api/searchArray', async () => {
return HttpResponse.json([
{
ok: true,
},
])
}),
http.get('http://localhost/api/foo', async () => {
return HttpResponse.json({
ok: true,
Expand Down Expand Up @@ -537,6 +544,24 @@ describe('Merge path with `app.route()`', () => {
expect(data.ok).toBe(true)
})

it('Should have correct types - with array of interfaces', async () => {
interface Result {
ok: boolean
okUndefined?: boolean
}
type Results = Result[]

const results: Results = [{ ok: true }]
const base = new Hono<Env>().basePath('/api')
const app = base.get('/searchArray', (c) => c.json(results))
type AppType = typeof app
const client = hc<AppType>('http://localhost')
const res = await client.api.searchArray.$get()
const data = await res.json()
type verify = Expect<Equal<Results, typeof data>>
expect(data[0].ok).toBe(true)
})

it('Should allow a Date object and return it as a string', async () => {
const app = new Hono()
const route = app.get('/api/foo', (c) => c.json({ datetime: new Date() }))
Expand Down
18 changes: 9 additions & 9 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { HonoRequest } from './request'
import type { Env, FetchEventLike, Input, NotFoundHandler, TypedResponse } from './types'
import { HtmlEscapedCallbackPhase, resolveCallback } from './utils/html'
import type { RedirectStatusCode, StatusCode } from './utils/http-status'
import type { IsAny, JSONParsed, JSONValue, Simplify } from './utils/types'
import type { DeepSimplify, IsAny, JSONParsed, JSONValue, Simplify } from './utils/types'

type HeaderRecord = Record<string, string | string[]>

Expand Down Expand Up @@ -127,30 +127,30 @@ interface TextRespond {
* @param {U} [status] - An optional status code for the response.
* @param {HeaderRecord} [headers] - An optional record of headers to include in the response.
*
* @returns {Response & TypedResponse<Simplify<T> extends JSONValue ? (JSONValue extends Simplify<T> ? never : JSONParsed<T>) : never, U, 'json'>} - The response after rendering the JSON object, typed with the provided object and status code types.
* @returns {Response & TypedResponse<DeepSimplify<T> extends JSONValue ? (JSONValue extends DeepSimplify<T> ? never : JSONParsed<T>) : never, U, 'json'>} - The response after rendering the JSON object, typed with the provided object and status code types.
*/
interface JSONRespond {
<T extends JSONValue | Simplify<unknown>, U extends StatusCode>(
<T extends JSONValue | DeepSimplify<unknown>, U extends StatusCode>(
object: T,
status?: U,
headers?: HeaderRecord
): Response &
TypedResponse<
Simplify<T> extends JSONValue
? JSONValue extends Simplify<T>
DeepSimplify<T> extends JSONValue
? JSONValue extends DeepSimplify<T>
? never
: JSONParsed<T>
: never,
U,
'json'
>
<T extends JSONValue | Simplify<unknown>, U extends StatusCode>(
object: Simplify<T> extends JSONValue ? T : Simplify<T>,
<T extends JSONValue | DeepSimplify<unknown>, U extends StatusCode>(
object: DeepSimplify<T> extends JSONValue ? T : DeepSimplify<T>,
init?: ResponseInit
): Response &
TypedResponse<
Simplify<T> extends JSONValue
? JSONValue extends Simplify<T>
DeepSimplify<T> extends JSONValue
? JSONValue extends DeepSimplify<T>
? never
: JSONParsed<T>
: never,
Expand Down

0 comments on commit f174818

Please sign in to comment.