fix(client): show type error if `$get()` needs args (#937)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* fix(client): show type error if `$get()` needs args * denoify
authored by Yusuke Wada · committed by GitHub · parent 23a22e7 · Browse files · 9 files·+60 added−21 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
9 files+60 added−21 removed
- tests:+24 added−1 removed
- other:+36 added−20 removed
Provenance
- Commit
46c233e- Tree
cbcccf9- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
23a22e7
9 files changed+60 added−21 removed
modified:deno_dist/client/types.ts+10 added-2 removed
123456711 unmodified lines1920212223222425262728294 unmodified lines34353637353839404142434445464734 unmodified linesimport type { Hono } from '../hono.ts'import type { ValidationTargets } from '../types.ts'import type { RemoveBlankRecord } from '../utils/types.ts'
type MethodName = `$${string}`
type Endpoint = Record<MethodName, Data>11 unmodified lines}
type ClientRequest<S extends Data> = { [M in keyof S]: S[M] extends { input: infer R; output: infer O } ? RemoveBlankRecord<R> extends never ? (args?: R, options?: RequestOptions) => Promise<ClientResponse<O>> ? (args?: {}, options?: RequestOptions) => Promise<ClientResponse<O>> : (args: R, options?: RequestOptions) => Promise<ClientResponse<O>> : never}
export interface ClientResponse<T> extends Response {4 unmodified lines args?: InferRequestType<T>, opt?: RequestOptions) => Promise<ClientResponse<InferResponseType<T>>>
export type InferResponseType<T> = T extends () => Promise<ClientResponse<infer O>> ? O : neverexport type InferResponseType<T> = T extends ( args: any | undefined) => Promise<ClientResponse<infer O>> ? O : never
export type InferRequestType<T> = T extends (args: infer R) => Promise<ClientResponse<unknown>> ? NonNullable<R> : never
34 unmodified linesmodified:deno_dist/utils/types.ts+6 added
9 unmodified lines101112131415161718192021222313 unmodified lines9 unmodified lines) => void ? I : never
export type RemoveBlankRecord<T> = T extends Record<infer K, unknown> ? K extends string ? T : never : never
export type JSONPrimitive = string | boolean | number | null | undefinedexport type JSONArray = (JSONPrimitive | JSONObject | JSONArray)[]export type JSONObject = { [key: string]: JSONPrimitive | JSONArray | JSONObject }export type JSONValue = JSONObject | JSONArray | JSONPrimitive13 unmodified linesmodified:deno_dist/hono.ts+1 added-1 removed
18 unmodified lines19 OnHandlerInterface,20 TypedResponse,21 MergePath,22 MergeSchemaPath,23- RemoveBlankRecord,23 } from './types.ts'24+import type { RemoveBlankRecord } from './utils/types.ts'25 import { getPathFromURL, mergePath } from './utils/url.ts'26 27 type Methods = typeof METHODS[number] | typeof METHOD_NAME_ALL_LOWERCASE28 271 unmodified linesmodified:deno_dist/types.ts+1 added-7 removed
1 unmodified line2 /* eslint-disable @typescript-eslint/no-explicit-any */3 /* eslint-disable @typescript-eslint/ban-types */4 import type { Context } from './context.ts'5 import type { Hono } from './hono.ts'6-import type { UnionToIntersection } from './utils/types.ts'6+import type { UnionToIntersection, RemoveBlankRecord } from './utils/types.ts'7 8 ////////////////////////////////////////9 ////// //////10 ////// Values //////349 unmodified lines360 ////// //////361 ////////////////////////////////////////362 363 export type ExtractSchema<T> = T extends Hono<infer _, infer S> ? S : never364-365-export type RemoveBlankRecord<T> = T extends Record<infer K, unknown>366- ? K extends string367- ? T368- : never369- : nevermodified:src/client/client.test.ts+24 added-1 removed
229 unmodified lines230 expect(await res.text()).toMatch('Good Night')231 })232 })233 234-describe('Infer the request type', () => {234+describe('Infer the response/request type', () => {235 const app = new Hono()236 const route = app.get(237 '/',238 validator('query', () => {33 unmodified lines272 name: string273 }274 type verify = Expect<Equal<Expected, Actual['query']>>275 })276+277+ describe('Without input', () => {278+ const route = app.get('/', (c) => c.jsonT({ ok: true }))279+ type AppType = typeof route280+281+ it('Should infer response type the type correctly', () => {282+ const client = hc<AppType>('/')283+ const req = client.index.$get284+285+ type Actual = InferResponseType<typeof req>286+ type Expected = { ok: boolean }287+ type verify = Expect<Equal<Expected, Actual>>288+ })289+290+ it('Should infer request type the type correctly', () => {291+ const client = hc<AppType>('/')292+ const req = client.index.$get293+294+ type Actual = InferRequestType<typeof req>295+ type Expected = {}296+ type verify = Expect<Equal<Expected, Actual>>297+ })298+ })299 })300 301 describe('Merge path with `app.route()`', () => {302 const server = setupServer(114 unmodified linesmodified:src/client/types.ts+10 added-2 removed
123456711 unmodified lines1920212223222425262728294 unmodified lines34353637353839404142434445464734 unmodified linesimport type { Hono } from '../hono'import type { ValidationTargets } from '../types'import type { RemoveBlankRecord } from '../utils/types'
type MethodName = `$${string}`
type Endpoint = Record<MethodName, Data>11 unmodified lines}
type ClientRequest<S extends Data> = { [M in keyof S]: S[M] extends { input: infer R; output: infer O } ? RemoveBlankRecord<R> extends never ? (args?: R, options?: RequestOptions) => Promise<ClientResponse<O>> ? (args?: {}, options?: RequestOptions) => Promise<ClientResponse<O>> : (args: R, options?: RequestOptions) => Promise<ClientResponse<O>> : never}
export interface ClientResponse<T> extends Response {4 unmodified lines args?: InferRequestType<T>, opt?: RequestOptions) => Promise<ClientResponse<InferResponseType<T>>>
export type InferResponseType<T> = T extends () => Promise<ClientResponse<infer O>> ? O : neverexport type InferResponseType<T> = T extends ( args: any | undefined) => Promise<ClientResponse<infer O>> ? O : never
export type InferRequestType<T> = T extends (args: infer R) => Promise<ClientResponse<unknown>> ? NonNullable<R> : never
34 unmodified linesmodified:src/utils/types.ts+6 added
9 unmodified lines10 ) => void11 ? I12 : never13 14+export type RemoveBlankRecord<T> = T extends Record<infer K, unknown>15+ ? K extends string16+ ? T17+ : never18+ : never19+20 export type JSONPrimitive = string | boolean | number | null | undefined21 export type JSONArray = (JSONPrimitive | JSONObject | JSONArray)[]22 export type JSONObject = { [key: string]: JSONPrimitive | JSONArray | JSONObject }23 export type JSONValue = JSONObject | JSONArray | JSONPrimitive13 unmodified linesmodified:src/hono.ts+1 added-1 removed
18 unmodified lines19 OnHandlerInterface,20 TypedResponse,21 MergePath,22 MergeSchemaPath,23- RemoveBlankRecord,23 } from './types'24+import type { RemoveBlankRecord } from './utils/types'25 import { getPathFromURL, mergePath } from './utils/url'26 27 type Methods = typeof METHODS[number] | typeof METHOD_NAME_ALL_LOWERCASE28 271 unmodified linesmodified:src/types.ts+1 added-7 removed
1 unmodified line2 /* eslint-disable @typescript-eslint/no-explicit-any */3 /* eslint-disable @typescript-eslint/ban-types */4 import type { Context } from './context'5 import type { Hono } from './hono'6-import type { UnionToIntersection } from './utils/types'6+import type { UnionToIntersection, RemoveBlankRecord } from './utils/types'7 8 ////////////////////////////////////////9 ////// //////10 ////// Values //////349 unmodified lines360 ////// //////361 ////////////////////////////////////////362 363 export type ExtractSchema<T> = T extends Hono<infer _, infer S> ? S : never364-365-export type RemoveBlankRecord<T> = T extends Record<infer K, unknown>366- ? K extends string367- ? T368- : never369- : never