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
Provenance
- Commit
5beac4e- Tree
a70de09- Author
Trung Dang <dangquoctrung123@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
fe0d787
3 files changed+45 added−10 removed
modified:src/client/client.test.ts+25 added
459 unmodified lines460 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 lines543 type verify = Expect<Equal<Result, typeof data>>544 expect(data.ok).toBe(true)545 })546 547+ it('Should have correct types - with array of interfaces', async () => {548+ interface Result {549+ ok: boolean550+ okUndefined?: boolean551+ }552+ type Results = Result[]553+554+ 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+ })564+565 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 route515 unmodified linesmodified:src/utils/types.ts+11 added-1 removed
39 unmodified lines4041424344454446474849505152535455565758596010 unmodified lines39 unmodified lines : T extends object ? { [K in keyof T]: JSONParsed<T[K]> } : never
/** * Useful to flatten the type output to improve type hints shown in editors. And also to transform an interface into a type to aide with assignability.// from sindresorhus/type-fest * @copyright from sindresorhus/type-fest */export type Simplify<T> = { [KeyType in keyof T]: T[KeyType] } & {}
/** * A simple extension of Simplify that works for array of interfaces. */export type SimplifyDeepArray<T> = T extends any[] ? { [E in keyof T]: Simplify<T[E]> } : { [KeyType in keyof T]: T[KeyType] } & {}
export type InterfaceToType<T> = T extends Function ? T : { [K in keyof T]: InterfaceToType<T[K]> }
export type RequiredKeysOf<BaseType extends object> = Exclude< {10 unmodified linesmodified:src/context.ts+9 added-9 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 } from './utils/types'5+import type { IsAny, JSONParsed, JSONValue, Simplify, 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<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 */132 interface 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