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
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
Provenance
- Commit
00f32b7- Tree
06f9262- Author
Trung Dang <dangquoctrung123@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
731d7ae
3 files changed+61 added−29 removed
modified:src/client/client.test.ts+31 added-1 removed
5 unmodified lines6 import { expectTypeOf, vi } from 'vitest'7 import { upgradeWebSocket } from '../adapter/deno/websocket'8 import { Hono } from '../hono'9 import { parse } from '../utils/cookie'10-import type { Equal, Expect } from '../utils/types'10+import type { Equal, Expect, JSONValue, SimplifyDeepArray } from '../utils/types'11 import { validator } from '../validator'12 import { hc } from './client'13 import type { ClientResponse, InferRequestType, InferResponseType } from './types'14 526 unmodified lines541 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)545+546+ // 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 })562 563 it('Should have correct types - with array of interfaces', async () => {564 interface Result {10 unmodified lines575 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)579+580+ // 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 })594 595 it('Should allow a Date object and return it as a string', async () => {596 const app = new Hono()571 unmodified linesmodified:src/utils/types.ts+3 added-3 removed
46 unmodified lines4748495051515253545554555657585911 unmodified lines46 unmodified lines */export type Simplify<T> = { [KeyType in keyof T]: T[KeyType] } & {}
/** * A simple extension of Simplify that works for array of interfaces. * A simple extension of Simplify that will deeply traverse array elements. */export type SimplifyDeepArray<T> = T extends any[] ? { [E in keyof T]: Simplify<T[E]> } : { [KeyType in keyof T]: T[KeyType] } & {} ? { [E in keyof T]: SimplifyDeepArray<T[E]> } : Simplify<T>
export type InterfaceToType<T> = T extends Function ? T : { [K in keyof T]: InterfaceToType<T[K]> }
export type RequiredKeysOf<BaseType extends object> = Exclude<11 unmodified linesmodified:src/context.ts+27 added-25 removed
1 import type { HonoRequest } from './request'2 import type { Env, FetchEventLike, Input, NotFoundHandler, TypedResponse } from './types'3 import { HtmlEscapedCallbackPhase, resolveCallback } from './utils/html'4 import type { RedirectStatusCode, StatusCode } from './utils/http-status'5-import type { IsAny, JSONParsed, JSONValue, Simplify, SimplifyDeepArray } from './utils/types'5+import type { IsAny, JSONParsed, JSONValue, SimplifyDeepArray } from './utils/types'6 7 type HeaderRecord = Record<string, string | string[]>8 9 /**116 unmodified lines126 * @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 */132 interface 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 }143 144 /**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+ */150+type 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+ >163+164+/**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 lines660 * 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