Skip to content

Commit 15aa272

Browse files
committed
refactor: reduce types
1 parent f907baa commit 15aa272

File tree

3 files changed

+9
-36
lines changed

3 files changed

+9
-36
lines changed

packages/router/src/typed-routes/route-map.ts

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,15 @@ export interface RouteRecordInfo<
1818
ParamsRaw extends RouteParamsRawGeneric = RouteParamsRawGeneric,
1919
Params extends RouteParamsGeneric = RouteParamsGeneric,
2020
Meta extends RouteMeta = RouteMeta,
21-
_ChildrenRouteNames extends string | symbol = never,
21+
ChildrenNames extends string | symbol | never = never | string | symbol,
2222
> {
2323
name: Name
2424
path: Path
2525
paramsRaw: ParamsRaw
2626
params: Params
2727
// TODO: implement meta with a defineRoute macro
2828
meta: Meta
29+
childrenNames: ChildrenNames
2930
}
3031

3132
/**
@@ -40,28 +41,3 @@ export type RouteMap =
4041
* Generic version of the `RouteMap`.
4142
*/
4243
export type RouteMapGeneric = Record<string | symbol, RouteRecordInfo>
43-
44-
/**
45-
* Returns a union of route names from children of the route with given route name
46-
*/
47-
export type GetDeepChildrenRouteNames<Name extends keyof RouteMap> =
48-
RouteMap[Name] extends RouteRecordInfo<
49-
any,
50-
any,
51-
any,
52-
any,
53-
any,
54-
infer ChildrenNames
55-
>
56-
? ChildrenNames extends any
57-
? ChildrenNames | GetDeepChildrenRouteNames<ChildrenNames>
58-
: never
59-
: never
60-
61-
/**
62-
* Returns a union of given route name and the route names of children of that route
63-
*/
64-
export type RouteNameWithChildren<Name extends keyof RouteMap> =
65-
RouteMapGeneric extends RouteMap
66-
? Name
67-
: Name | GetDeepChildrenRouteNames<Name>

packages/router/src/useApi.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { inject } from 'vue'
22
import { routerKey, routeLocationKey } from './injectionSymbols'
33
import { Router } from './router'
4-
import { RouteMap, RouteNameWithChildren } from './typed-routes/route-map'
4+
import { RouteMap } from './typed-routes/route-map'
55
import { RouteLocationNormalizedLoaded } from './typed-routes'
66

77
/**
@@ -12,17 +12,14 @@ export function useRouter(): Router {
1212
return inject(routerKey)!
1313
}
1414

15-
type GetRouteLocationNormalizedLoaded<Name extends keyof RouteMap> =
16-
Name extends any ? RouteLocationNormalizedLoaded<Name> : never
17-
1815
/**
1916
* Returns the current route location. Equivalent to using `$route` inside
2017
* templates.
2118
*/
22-
export function useRoute<
23-
CurrentRouteName extends keyof RouteMap = keyof RouteMap,
24-
>(_currentRouteName?: CurrentRouteName) {
25-
return inject(routeLocationKey) as GetRouteLocationNormalizedLoaded<
26-
RouteNameWithChildren<CurrentRouteName>
19+
export function useRoute<Name extends keyof RouteMap = keyof RouteMap>(
20+
_name?: Name
21+
) {
22+
return inject(routeLocationKey) as RouteLocationNormalizedLoaded<
23+
Name | RouteMap[Name]['childrenNames']
2724
>
2825
}

packages/router/test-dts/typed-routes.test-d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export type RouteMap = {
3636
Record<never, never>,
3737
Record<never, never>,
3838
RouteMeta,
39-
'/a/b'
39+
'/a/b' | '/a/b/c'
4040
>
4141
'/a/b': RouteRecordInfo<
4242
'/a/b',

0 commit comments

Comments
 (0)