0
0
mirror of https://github.com/honojs/hono.git synced 2024-11-24 11:07:29 +01:00

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
This commit is contained in:
m-shaka 2024-10-11 18:21:00 +09:00 committed by GitHub
parent 7c36339d14
commit f1a7267948
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 24 additions and 21 deletions

View File

@ -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<E extends Env = Env, S extends Schema = {}, BasePath extends string =
>(
path: SubPath,
app: Hono<SubEnv, SubSchema, SubBasePath>
): Hono<E, MergeSchemaPath<SubSchema, MergePath<BasePath, SubPath>> & S, BasePath> {
): Hono<
E,
ExcludeEmptyObject<MergeSchemaPath<SubSchema, MergePath<BasePath, SubPath>> | S>,
BasePath
> {
const subApp = this.basePath(path)
app.routes.map((r) => {
let handler

View File

@ -1817,24 +1817,21 @@ type ExtractParams<Path extends string> = string extends Path
type FlattenIfIntersect<T> = T extends infer O ? { [K in keyof O]: O[K] } : never
export type MergeSchemaPath<OrigSchema extends Schema, SubPath extends string> = Simplify<{
[P in keyof OrigSchema as MergePath<SubPath, P & string>]: {
[M in keyof OrigSchema[P]]: MergeEndpointParamsWithPath<OrigSchema[P][M], SubPath>
}
}>
type MergeEndpointParamsWithPath<T, SubPath extends string> = T extends {
input: infer Input
output: infer Output
outputFormat: infer OutputFormat
status: infer Status
export type MergeSchemaPath<OrigSchema extends Schema, SubPath extends string> = {
[P in keyof OrigSchema as MergePath<SubPath, P & string>]: [OrigSchema[P]] extends [
Record<string, Endpoint>
]
? { [M in keyof OrigSchema[P]]: MergeEndpointParamsWithPath<OrigSchema[P][M], SubPath> }
: never
}
type MergeEndpointParamsWithPath<T extends Endpoint, SubPath extends string> = T extends unknown
? {
input: Input extends { param: infer _ }
input: T['input'] extends { param: infer _ }
? ExtractParams<SubPath> extends never
? Input
? T['input']
: FlattenIfIntersect<
Input & {
T['input'] & {
param: {
// Maps extracted keys, stripping braces, to a string-typed record.
[K in keyof ExtractParams<SubPath> as K extends `${infer Prefix}{${infer _}}`
@ -1844,8 +1841,8 @@ type MergeEndpointParamsWithPath<T, SubPath extends string> = T extends {
}
>
: RemoveBlankRecord<ExtractParams<SubPath>> extends never
? Input
: Input & {
? T['input']
: T['input'] & {
// Maps extracted keys, stripping braces, to a string-typed record.
param: {
[K in keyof ExtractParams<SubPath> as K extends `${infer Prefix}{${infer _}}`
@ -1853,12 +1850,11 @@ type MergeEndpointParamsWithPath<T, SubPath extends string> = T extends {
: K]: string
}
}
output: Output
outputFormat: OutputFormat
status: Status
output: T['output']
outputFormat: T['outputFormat']
status: T['status']
}
: never
export type AddParam<I, P extends string> = ParamKeys<P> extends never
? I
: I extends { param: infer _ }

View File

@ -97,3 +97,5 @@ export type IsAny<T> = boolean extends (T extends never ? true : false) ? true :
* @see https://github.com/Microsoft/TypeScript/issues/29729
*/
export type StringLiteralUnion<T> = T | (string & Record<never, never>)
export type ExcludeEmptyObject<T> = T extends {} ? ({} extends T ? never : T) : T