From f1a7267948ccd1015bea40bf0bd0ec2660e5d5f2 Mon Sep 17 00:00:00 2001 From: m-shaka Date: Fri, 11 Oct 2024 18:21:00 +0900 Subject: [PATCH] perf(types): replace intersection with union to get better perf (#3443) * perf(types): replace intersection with union to get better perf * revert intersection * format * refactor: move ExcludeEmptyObject to utils/type.ts --- src/hono-base.ts | 7 ++++++- src/types.ts | 36 ++++++++++++++++-------------------- src/utils/types.ts | 2 ++ 3 files changed, 24 insertions(+), 21 deletions(-) diff --git a/src/hono-base.ts b/src/hono-base.ts index 812a1d6c..9597a33b 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' /** @@ -212,7 +213,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 diff --git a/src/types.ts b/src/types.ts index fe8d86be..e5b24003 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1817,24 +1817,21 @@ 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 } + +type MergeEndpointParamsWithPath = T extends unknown ? { - input: Input extends { param: infer _ } + 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 +1841,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 +1850,11 @@ type MergeEndpointParamsWithPath = T extends { : K]: string } } - output: Output - outputFormat: OutputFormat - status: Status + output: T['output'] + outputFormat: T['outputFormat'] + status: T['status'] } : never - export type AddParam = ParamKeys

extends never ? I : I extends { param: infer _ } diff --git a/src/utils/types.ts b/src/utils/types.ts index 0c124785..4a9647e3 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