From f618e8a6f757f249fb1253d1fb3559e1bf7a31f0 Mon Sep 17 00:00:00 2001 From: m-shaka Date: Mon, 23 Sep 2024 16:12:18 +0900 Subject: [PATCH 1/4] perf(types): replace intersection with union to get better perf --- src/hono-base.ts | 4 +++- src/types.ts | 46 +++++++++++++++++++++++----------------------- 2 files changed, 26 insertions(+), 24 deletions(-) diff --git a/src/hono-base.ts b/src/hono-base.ts index 812a1d6ce..f58ceaae2 100644 --- a/src/hono-base.ts +++ b/src/hono-base.ts @@ -212,7 +212,7 @@ class Hono( path: SubPath, app: Hono - ): Hono> & S, BasePath> { + ): Hono> | S>, BasePath> { const subApp = this.basePath(path) app.routes.map((r) => { let handler @@ -519,5 +519,7 @@ class Hono = T extends {} ? ({} extends T ? never : T) : T + export { Hono as HonoBase } diff --git a/src/types.ts b/src/types.ts index fe8d86bed..3ac6d7ee3 100644 --- a/src/types.ts +++ b/src/types.ts @@ -146,7 +146,11 @@ export interface HandlerInterface< >( path: P, handler: H - ): Hono, I, MergeTypedResponse>, BasePath> + ): Hono< + E, + ExcludeEmptyObject, I, MergeTypedResponse>>, + BasePath + > // app.get(handler x 3) < @@ -1817,24 +1821,19 @@ type ExtractParams = string extends Path type FlattenIfIntersect = T extends infer O ? { [K in keyof O]: O[K] } : never -export type MergeSchemaPath = Simplify<{ - [P in keyof OrigSchema as MergePath]: { - [M in keyof OrigSchema[P]]: MergeEndpointParamsWithPath - } -}> - -type MergeEndpointParamsWithPath = T extends { - input: infer Input - output: infer Output - outputFormat: infer OutputFormat - status: infer Status +export type MergeSchemaPath = { + [P in keyof OrigSchema as MergePath]: + [OrigSchema[P]] extends [Record] + ? { [M in keyof OrigSchema[P]]: MergeEndpointParamsWithPath } + : never } - ? { - input: Input extends { param: infer _ } + +type MergeEndpointParamsWithPath = T extends unknown ? { + input: T['input'] extends { param: infer _ } ? ExtractParams extends never - ? Input + ? T['input'] : FlattenIfIntersect< - Input & { + T['input'] & { param: { // Maps extracted keys, stripping braces, to a string-typed record. [K in keyof ExtractParams as K extends `${infer Prefix}{${infer _}}` @@ -1844,8 +1843,8 @@ type MergeEndpointParamsWithPath = T extends { } > : RemoveBlankRecord> extends never - ? Input - : Input & { + ? T['input'] + : T['input'] & { // Maps extracted keys, stripping braces, to a string-typed record. param: { [K in keyof ExtractParams as K extends `${infer Prefix}{${infer _}}` @@ -1853,12 +1852,11 @@ type MergeEndpointParamsWithPath = T extends { : K]: string } } - output: Output - outputFormat: OutputFormat - status: Status + output: T['output'] + outputFormat: T['outputFormat'] + status: T['status'] } - : never - + : never export type AddParam = ParamKeys

extends never ? I : I extends { param: infer _ } @@ -1988,6 +1986,8 @@ type IntersectNonAnyTypes = T extends [infer Head, ...infer Res ? IfAnyThenEmptyObject> & IntersectNonAnyTypes : {} +type ExcludeEmptyObject = T extends {} ? ({} extends T ? never : T) : T + //////////////////////////////////////// ////// ////// ////// FetchEvent ////// From a07e25f31f9e4b59901ab55ed3ee19854f2d79a3 Mon Sep 17 00:00:00 2001 From: m-shaka Date: Sat, 28 Sep 2024 15:41:08 +0900 Subject: [PATCH 2/4] revert intersection --- src/types.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/types.ts b/src/types.ts index 3ac6d7ee3..c277133aa 100644 --- a/src/types.ts +++ b/src/types.ts @@ -148,7 +148,7 @@ export interface HandlerInterface< handler: H ): Hono< E, - ExcludeEmptyObject, I, MergeTypedResponse>>, + S & ToSchema, I, MergeTypedResponse>, BasePath > @@ -1986,8 +1986,6 @@ type IntersectNonAnyTypes = T extends [infer Head, ...infer Res ? IfAnyThenEmptyObject> & IntersectNonAnyTypes : {} -type ExcludeEmptyObject = T extends {} ? ({} extends T ? never : T) : T - //////////////////////////////////////// ////// ////// ////// FetchEvent ////// From 56b40fd70f13c9d5b644314a19f35bb1e6d33782 Mon Sep 17 00:00:00 2001 From: m-shaka Date: Sat, 28 Sep 2024 16:35:47 +0900 Subject: [PATCH 3/4] format --- src/hono-base.ts | 7 +++++-- src/types.ts | 20 +++++++++----------- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/hono-base.ts b/src/hono-base.ts index f58ceaae2..4f7d7dbe3 100644 --- a/src/hono-base.ts +++ b/src/hono-base.ts @@ -212,7 +212,11 @@ class Hono( path: SubPath, app: Hono - ): Hono> | S>, BasePath> { + ): Hono< + E, + ExcludeEmptyObject> | S>, + BasePath + > { const subApp = this.basePath(path) app.routes.map((r) => { let handler @@ -521,5 +525,4 @@ class Hono = T extends {} ? ({} extends T ? never : T) : T - export { Hono as HonoBase } diff --git a/src/types.ts b/src/types.ts index c277133aa..e5b240034 100644 --- a/src/types.ts +++ b/src/types.ts @@ -146,11 +146,7 @@ export interface HandlerInterface< >( path: P, handler: H - ): Hono< - E, - S & ToSchema, I, MergeTypedResponse>, - BasePath - > + ): Hono, I, MergeTypedResponse>, BasePath> // app.get(handler x 3) < @@ -1822,13 +1818,15 @@ type ExtractParams = string extends Path type FlattenIfIntersect = T extends infer O ? { [K in keyof O]: O[K] } : never export type MergeSchemaPath = { - [P in keyof OrigSchema as MergePath]: - [OrigSchema[P]] extends [Record] - ? { [M in keyof OrigSchema[P]]: MergeEndpointParamsWithPath } - : never + [P in keyof OrigSchema as MergePath]: [OrigSchema[P]] extends [ + Record + ] + ? { [M in keyof OrigSchema[P]]: MergeEndpointParamsWithPath } + : never } -type MergeEndpointParamsWithPath = T extends unknown ? { +type MergeEndpointParamsWithPath = T extends unknown + ? { input: T['input'] extends { param: infer _ } ? ExtractParams extends never ? T['input'] @@ -1856,7 +1854,7 @@ type MergeEndpointParamsWithPath = T outputFormat: T['outputFormat'] status: T['status'] } - : never + : never export type AddParam = ParamKeys

extends never ? I : I extends { param: infer _ } From e97c2e0e917c8eb39142eea3c140b8b65dec11b6 Mon Sep 17 00:00:00 2001 From: m-shaka Date: Fri, 11 Oct 2024 09:41:41 +0900 Subject: [PATCH 4/4] refactor: move ExcludeEmptyObject to utils/type.ts --- src/hono-base.ts | 2 +- src/utils/types.ts | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/hono-base.ts b/src/hono-base.ts index 4f7d7dbe3..9597a33b9 100644 --- a/src/hono-base.ts +++ b/src/hono-base.ts @@ -26,6 +26,7 @@ import type { RouterRoute, Schema, } from './types' +import type { ExcludeEmptyObject } from './utils/types' import { getPath, getPathNoStrict, mergePath } from './utils/url' /** @@ -523,6 +524,5 @@ class Hono = T extends {} ? ({} extends T ? never : T) : T export { Hono as HonoBase } diff --git a/src/utils/types.ts b/src/utils/types.ts index 0c1247851..4a9647e37 100644 --- a/src/utils/types.ts +++ b/src/utils/types.ts @@ -97,3 +97,5 @@ export type IsAny = boolean extends (T extends never ? true : false) ? true : * @see https://github.com/Microsoft/TypeScript/issues/29729 */ export type StringLiteralUnion = T | (string & Record) + +export type ExcludeEmptyObject = T extends {} ? ({} extends T ? never : T) : T