Skip to content
Myr

perf(types): replace intersection with union to get better perf (#3443)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* perf(types): replace intersection with union to get better perf * revert intersection * format * refactor: move ExcludeEmptyObject to utils/type.ts

authored by m-shaka · committed by GitHub · parent 7c36339 · Browse files · 3 files+24 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

3 files+24 added−21 removed

  • other:+24 added−21 removed

3 files+24 added−21 removed

modified:src/utils/types.ts+2 added
modified:src/hono-base.ts+6 added-1 removed
24 unmodified lines
25 OnHandlerInterface,26 RouterRoute,27 Schema,28} from './types'29import type { ExcludeEmptyObject } from './utils/types'30import { getPath, getPathNoStrict, mergePath } from './utils/url'3132/**33 * Symbol used to mark a composed handler.
178 unmodified lines
212 SubBasePath extends string213 >(214 path: SubPath,215 app: Hono<SubEnv, SubSchema, SubBasePath>215 ): Hono<E, MergeSchemaPath<SubSchema, MergePath<BasePath, SubPath>> & S, BasePath> {216 ): Hono<217 E,218 ExcludeEmptyObject<MergeSchemaPath<SubSchema, MergePath<BasePath, SubPath>> | S>,219 BasePath220 > {221 const subApp = this.basePath(path)222 app.routes.map((r) => {223 let handler224 if (app.errorHandler === errorHandler) {
304 unmodified lines
modified:src/types.ts+16 added-20 removed
1815 unmodified lines
1816 : never18171818type FlattenIfIntersect<T> = T extends infer O ? { [K in keyof O]: O[K] } : never18191820export type MergeSchemaPath<OrigSchema extends Schema, SubPath extends string> = Simplify<{1821 [P in keyof OrigSchema as MergePath<SubPath, P & string>]: {1822 [M in keyof OrigSchema[P]]: MergeEndpointParamsWithPath<OrigSchema[P][M], SubPath>1823 }1824}>18251826type MergeEndpointParamsWithPath<T, SubPath extends string> = T extends {1827 input: infer Input1828 output: infer Output1829 outputFormat: infer OutputFormat1830 status: infer Status1820export type MergeSchemaPath<OrigSchema extends Schema, SubPath extends string> = {1821 [P in keyof OrigSchema as MergePath<SubPath, P & string>]: [OrigSchema[P]] extends [1822 Record<string, Endpoint>1823 ]1824 ? { [M in keyof OrigSchema[P]]: MergeEndpointParamsWithPath<OrigSchema[P][M], SubPath> }1825 : never1826}18271828type MergeEndpointParamsWithPath<T extends Endpoint, SubPath extends string> = T extends unknown1829 ? {1833 input: Input extends { param: infer _ }1830 input: T['input'] extends { param: infer _ }1831 ? ExtractParams<SubPath> extends never1835 ? Input1832 ? T['input']1833 : FlattenIfIntersect<1837 Input & {1834 T['input'] & {1835 param: {1836 // Maps extracted keys, stripping braces, to a string-typed record.1837 [K in keyof ExtractParams<SubPath> as K extends `${infer Prefix}{${infer _}}`1838 ? Prefix
1 unmodified line
1840 }1841 }1842 >1843 : RemoveBlankRecord<ExtractParams<SubPath>> extends never1847 ? Input1848 : Input & {1844 ? T['input']1845 : T['input'] & {1846 // Maps extracted keys, stripping braces, to a string-typed record.1847 param: {1848 [K in keyof ExtractParams<SubPath> as K extends `${infer Prefix}{${infer _}}`1849 ? Prefix1850 : K]: string1851 }1852 }1856 output: Output1857 outputFormat: OutputFormat1858 status: Status1853 output: T['output']1854 outputFormat: T['outputFormat']1855 status: T['status']1856 }1857 : never18611858export type AddParam<I, P extends string> = ParamKeys<P> extends never1859 ? I1860 : I extends { param: infer _ }1861 ? I
137 unmodified lines