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
Provenance
- Commit
f1a7267- Tree
4a263fa- Author
m-shaka <buddha0818@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
7c36339
3 files changed+24 added−21 removed
modified:src/utils/types.ts+2 added
95 unmodified lines9697989910010195 unmodified lines * String literal types with auto-completion * @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) : Tmodified:src/hono-base.ts+6 added-1 removed
24 unmodified lines25 OnHandlerInterface,26 RouterRoute,27 Schema,28 } from './types'29+import type { ExcludeEmptyObject } from './utils/types'30 import { getPath, getPathNoStrict, mergePath } from './utils/url'31 32 /**33 * Symbol used to mark a composed handler.178 unmodified lines212 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 linesmodified:src/types.ts+16 added-20 removed
1815 unmodified lines1816 : never1817 1818 type FlattenIfIntersect<T> = T extends infer O ? { [K in keyof O]: O[K] } : never1819 1820-export 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-}>1825-1826-type MergeEndpointParamsWithPath<T, SubPath extends string> = T extends {1827- input: infer Input1828- output: infer Output1829- outputFormat: infer OutputFormat1830- status: infer Status1820+export 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 }1827+1828+type 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 ? Prefix1 unmodified line1840 }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 : never1861-1858 export type AddParam<I, P extends string> = ParamKeys<P> extends never1859 ? I1860 : I extends { param: infer _ }1861 ? I137 unmodified lines