Skip to content
Myr

fix(types): #2912: interfaces array's respond typed as `never` (#2915)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* feat: add `DeepSimplify` type * fix(types): #2912: interfaces array's respond typed as `never` * refactor: wording `DeepSimplify` => `SimplifyDeepArray` * docs(jsdoc): add jsdoc for `Simplify`, `SimplifyDeepArray`

authored by Trung Dang · committed by GitHub · parent fe0d787 · Browse files · 3 files+45 added−10 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+45 added−10 removed

  • tests:+25 added−0 removed
  • other:+20 added−10 removed

3 files+45 added−10 removed

modified:src/client/client.test.ts+25 added
459 unmodified lines
460 return HttpResponse.json({461 ok: true,462 })463 }),464 http.get('http://localhost/api/searchArray', async () => {465 return HttpResponse.json([466 {467 ok: true,468 },469 ])470 }),471 http.get('http://localhost/api/foo', async () => {472 return HttpResponse.json({473 ok: true,474 })
68 unmodified lines
543 type verify = Expect<Equal<Result, typeof data>>544 expect(data.ok).toBe(true)545 })546547 it('Should have correct types - with array of interfaces', async () => {548 interface Result {549 ok: boolean550 okUndefined?: boolean551 }552 type Results = Result[]553554 const results: Results = [{ ok: true }]555 const base = new Hono<Env>().basePath('/api')556 const app = base.get('/searchArray', (c) => c.json(results))557 type AppType = typeof app558 const client = hc<AppType>('http://localhost')559 const res = await client.api.searchArray.$get()560 const data = await res.json()561 type verify = Expect<Equal<Results, typeof data>>562 expect(data[0].ok).toBe(true)563 })564565 it('Should allow a Date object and return it as a string', async () => {566 const app = new Hono()567 const route = app.get('/api/foo', (c) => c.json({ datetime: new Date() }))568 type AppType = typeof route
515 unmodified lines
modified:src/utils/types.ts+11 added-1 removed
modified:src/context.ts+9 added-9 removed
1import type { HonoRequest } from './request'2import type { Env, FetchEventLike, Input, NotFoundHandler, TypedResponse } from './types'3import { HtmlEscapedCallbackPhase, resolveCallback } from './utils/html'4import type { RedirectStatusCode, StatusCode } from './utils/http-status'5import type { IsAny, JSONParsed, JSONValue, Simplify } from './utils/types'5import type { IsAny, JSONParsed, JSONValue, Simplify, SimplifyDeepArray } from './utils/types'67type HeaderRecord = Record<string, string | string[]>89/**
116 unmodified lines
126 * @param {T} object - The JSON object to be included in the response.127 * @param {U} [status] - An optional status code for the response.128 * @param {HeaderRecord} [headers] - An optional record of headers to include in the response.129 *130 * @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.130 * @returns {Response & TypedResponse<SimplifyDeepArray<T> extends JSONValue ? (JSONValue extends SimplifyDeepArray<T> ? never : JSONParsed<T>) : never, U, 'json'>} - The response after rendering the JSON object, typed with the provided object and status code types.131 */132interface JSONRespond {133 <T extends JSONValue | Simplify<unknown>, U extends StatusCode>(133 <T extends JSONValue | SimplifyDeepArray<unknown>, U extends StatusCode>(134 object: T,135 status?: U,136 headers?: HeaderRecord137 ): Response &138 TypedResponse<139 Simplify<T> extends JSONValue140 ? JSONValue extends Simplify<T>139 SimplifyDeepArray<T> extends JSONValue140 ? JSONValue extends SimplifyDeepArray<T>141 ? never142 : JSONParsed<T>143 : never,144 U,145 'json'146 >147 <T extends JSONValue | Simplify<unknown>, U extends StatusCode>(148 object: Simplify<T> extends JSONValue ? T : Simplify<T>,147 <T extends JSONValue | SimplifyDeepArray<unknown>, U extends StatusCode>(148 object: SimplifyDeepArray<T> extends JSONValue ? T : SimplifyDeepArray<T>,149 init?: ResponseInit150 ): Response &151 TypedResponse<152 Simplify<T> extends JSONValue153 ? JSONValue extends Simplify<T>152 SimplifyDeepArray<T> extends JSONValue153 ? JSONValue extends SimplifyDeepArray<T>154 ? never155 : JSONParsed<T>156 : never,157 U,
583 unmodified lines