Skip to content
Myr

fix(types): `SimplifyDeepArray` should now actually be "deep" (#2920)

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

* fix(types): `SimplifyDeepArray` should now actually be "deep" Also now uses `Simplify` instead of inline declare the type * tests(types): extra tests for `Should have correct types - interfaces` * style: apply lint&style fixes * docs: rewording SimplifyDeepArray jsdoc * fix: type is possibly infinite * fix: mismatching implementation of interface * refactor: split JSONRespond return declaration to it's own type DRY-er code and fixes a weird type is possibly infinite yelling

authored by Trung Dang · committed by GitHub · parent 731d7ae · Browse files · 3 files+61 added−29 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+61 added−29 removed

  • tests:+31 added−1 removed
  • other:+30 added−28 removed

3 files+61 added−29 removed

modified:src/client/client.test.ts+31 added-1 removed
5 unmodified lines
6import { expectTypeOf, vi } from 'vitest'7import { upgradeWebSocket } from '../adapter/deno/websocket'8import { Hono } from '../hono'9import { parse } from '../utils/cookie'10import type { Equal, Expect } from '../utils/types'10import type { Equal, Expect, JSONValue, SimplifyDeepArray } from '../utils/types'11import { validator } from '../validator'12import { hc } from './client'13import type { ClientResponse, InferRequestType, InferResponseType } from './types'14
526 unmodified lines
541 const res = await client.api.search.$get()542 const data = await res.json()543 type verify = Expect<Equal<Result, typeof data>>544 expect(data.ok).toBe(true)545546 // A few more types only tests547 interface DeepInterface {548 l2: {549 l3: Result550 }551 }552 interface ExtraDeepInterface {553 l4: DeepInterface554 }555 type verifyDeepInterface = Expect<556 Equal<SimplifyDeepArray<DeepInterface> extends JSONValue ? true : false, true>557 >558 type verifyExtraDeepInterface = Expect<559 Equal<SimplifyDeepArray<ExtraDeepInterface> extends JSONValue ? true : false, true>560 >561 })562563 it('Should have correct types - with array of interfaces', async () => {564 interface Result {
10 unmodified lines
575 const res = await client.api.searchArray.$get()576 const data = await res.json()577 type verify = Expect<Equal<Results, typeof data>>578 expect(data[0].ok).toBe(true)579580 // A few more types only tests581 type verifyNestedArrayTyped = Expect<582 Equal<SimplifyDeepArray<[string, Results]> extends JSONValue ? true : false, true>583 >584 type verifyNestedArrayInterfaceArray = Expect<585 Equal<SimplifyDeepArray<[string, Result[]]> extends JSONValue ? true : false, true>586 >587 type verifyExtraNestedArrayTyped = Expect<588 Equal<SimplifyDeepArray<[string, Results[]]> extends JSONValue ? true : false, true>589 >590 type verifyExtraNestedArrayInterfaceArray = Expect<591 Equal<SimplifyDeepArray<[string, Result[][]]> extends JSONValue ? true : false, true>592 >593 })594595 it('Should allow a Date object and return it as a string', async () => {596 const app = new Hono()
571 unmodified lines
modified:src/utils/types.ts+3 added-3 removed
modified:src/context.ts+27 added-25 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, SimplifyDeepArray } from './utils/types'5import type { IsAny, JSONParsed, JSONValue, 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<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.130 * @returns {JSONRespondReturn<T, U>} - The response after rendering the JSON object, typed with the provided object and status code types.131 */132interface JSONRespond {133 <T extends JSONValue | SimplifyDeepArray<unknown>, U extends StatusCode>(134 object: T,135 status?: U,136 headers?: HeaderRecord137 ): Response &138 TypedResponse<139 SimplifyDeepArray<T> extends JSONValue140 ? JSONValue extends SimplifyDeepArray<T>141 ? never142 : JSONParsed<T>143 : never,144 U,145 'json'146 >137 ): JSONRespondReturn<T, U>138 <T extends JSONValue | SimplifyDeepArray<unknown>, U extends StatusCode>(148 object: SimplifyDeepArray<T> extends JSONValue ? T : SimplifyDeepArray<T>,139 object: T,140 init?: ResponseInit150 ): Response &151 TypedResponse<152 SimplifyDeepArray<T> extends JSONValue153 ? JSONValue extends SimplifyDeepArray<T>154 ? never155 : JSONParsed<T>156 : never,157 U,158 'json'159 >141 ): JSONRespondReturn<T, U>142}143144/**145 * @template T - The type of the JSON value or simplified unknown type.146 * @template U - The type of the status code.147 *148 * @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.149 */150type JSONRespondReturn<151 T extends JSONValue | SimplifyDeepArray<unknown>,152 U extends StatusCode153> = Response &154 TypedResponse<155 SimplifyDeepArray<T> extends JSONValue156 ? JSONValue extends SimplifyDeepArray<T>157 ? never158 : JSONParsed<T>159 : never,160 U,161 'json'162 >163164/**165 * Interface representing a function that responds with HTML content.166 *167 * @param html - The HTML content to respond with, which can be a string or a Promise that resolves to a string.168 * @param status - (Optional) The HTTP status code for the response.
491 unmodified lines
660 * return c.json({ message: 'Hello!' })661 * })662 * ```663 */662 json: JSONRespond = <T extends JSONValue | Simplify<unknown>, U extends StatusCode>(664 json: JSONRespond = <T extends JSONValue | SimplifyDeepArray<unknown>, U extends StatusCode>(665 object: T,666 arg?: U | ResponseInit,667 headers?: HeaderRecord666 ): ReturnType<JSONRespond> => {668 ): JSONRespondReturn<T, U> => {669 const body = JSON.stringify(object)670 this.#preparedHeaders ??= {}671 this.#preparedHeaders['content-type'] = 'application/json; charset=UTF-8'672 /* eslint-disable @typescript-eslint/no-explicit-any */
73 unmodified lines