Skip to content

Commit

Permalink
Simplify flatten types a big
Browse files Browse the repository at this point in the history
  • Loading branch information
pleek91 committed Jan 21, 2024
1 parent 27ba10b commit e302aaf
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions src/types/flattened.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
import { MarkOptionalParams, MergeParams, RoutePathParams, RouteQueryParams } from '@/types/routeMethods'
import { Public, Route, Routes } from '@/types/routes'
import { Identity, UnionToIntersection } from '@/types/utilities'
import { UnionToIntersection } from '@/types/utilities'

export type Flattened<
TRoute extends Route | Routes,
TRoute extends Route,
TPrefix extends string = '',
TPathParams extends Record<string, unknown[]> = Record<never, never>,
TQueryParams extends Record<string, unknown[]> = Record<never, never>
> = RouteFlat<TRoute, TPrefix, TPathParams, TQueryParams> & RouteChildrenFlat<TRoute, TPrefix, TPathParams, TQueryParams>

export type FlattenedRoutes<
TRoute extends Routes,
TPrefix extends string = '',
TPathParams extends Record<string, unknown[]> = Record<never, never>,
TQueryParams extends Record<string, unknown[]> = Record<never, never>
> = Identity<
TRoute extends Route
? RouteFlat<TRoute, TPrefix, TPathParams, TQueryParams> & RouteChildrenFlat<TRoute, TPrefix, TPathParams, TQueryParams>
: TRoute extends Routes
? UnionToIntersection<{
[K in keyof TRoute]: TRoute[K] extends Route
? Flattened<TRoute[K], TPrefix, TPathParams, TQueryParams>
: Record<never, never>
}[number]>
> = UnionToIntersection<{
[K in keyof TRoute]: TRoute[K] extends Route
? Flattened<TRoute[K], TPrefix, TPathParams, TQueryParams>
: Record<never, never>
>
}[number]>

type RouteFlat<
TRoute extends Route,
Expand All @@ -35,8 +36,8 @@ type RouteChildrenFlat<
TQueryParams extends Record<string, unknown[]> = Record<never, never>
> = TRoute extends { children: infer Children extends Routes }
? TRoute extends Public<TRoute> & { name: infer Name extends string }
? Flattened<Children, Prefix<Name, TPrefix>, RoutePathParams<TRoute, TPathParams>, RouteQueryParams<TRoute, TQueryParams>>
: Flattened<Children, Prefix<'', TPrefix>, RoutePathParams<TRoute, TPathParams>, RouteQueryParams<TRoute, TQueryParams>>
? FlattenedRoutes<Children, Prefix<Name, TPrefix>, RoutePathParams<TRoute, TPathParams>, RouteQueryParams<TRoute, TQueryParams>>
: FlattenedRoutes<Children, TPrefix, RoutePathParams<TRoute, TPathParams>, RouteQueryParams<TRoute, TQueryParams>>
: Record<never, never>

type Prefix<
Expand Down

0 comments on commit e302aaf

Please sign in to comment.