Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add children route names generic to generated RouteNamedMap and adjust docs and tests #602

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions docs/.vitepress/twoslash-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,44 @@ declare module 'vue-router/auto-routes' {
ParamValueOneOrMore,
ParamValueZeroOrMore,
ParamValueZeroOrOne,
RouteMeta,
} from 'vue-router'

/**
* Route name map generated by unplugin-vue-router
*/
export interface RouteNamedMap {
'/': RouteRecordInfo<'/', '/', Record<never, never>, Record<never, never>>
'/': RouteRecordInfo<
'/',
'/',
Record<never, never>,
Record<never, never>,
RouteMeta,
never
>
'/users': RouteRecordInfo<
'/users',
'/users',
Record<never, never>,
Record<never, never>
Record<never, never>,
RouteMeta,
never
>
'/users/[id]': RouteRecordInfo<
'/users/[id]',
'/users/:id',
{ id: ParamValue<true> },
{ id: ParamValue<false> }
{ id: ParamValue<false> },
RouteMeta,
'/users/[id]/edit'
>
'/users/[id]/edit': RouteRecordInfo<
'/users/[id]/edit',
'/users/:id/edit',
{ id: ParamValue<true> },
{ id: ParamValue<false> }
{ id: ParamValue<false> },
RouteMeta,
never
>
}
}
Expand Down
22 changes: 18 additions & 4 deletions docs/.vitepress/twoslash/code/typed-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,41 @@ declare module 'vue-router/auto-routes' {
ParamValueOneOrMore,
ParamValueZeroOrMore,
ParamValueZeroOrOne,
RouteMeta,
} from 'vue-router'

export interface RouteNamedMap {
'/': RouteRecordInfo<'/', '/', Record<never, never>, Record<never, never>>
'/': RouteRecordInfo<
'/',
'/',
Record<never, never>,
Record<never, never>,
RouteMeta,
never
>
'/users': RouteRecordInfo<
'/users',
'/users',
Record<never, never>,
Record<never, never>
Record<never, never>,
RouteMeta,
never
>
'/users/[id]': RouteRecordInfo<
'/users/[id]',
'/users/:id',
{ id: ParamValue<true> },
{ id: ParamValue<false> }
{ id: ParamValue<false> },
RouteMeta,
'/users/[id]/edit'
>
'/users/[id]/edit': RouteRecordInfo<
'/users/[id]/edit',
'/users/:id/edit',
{ id: ParamValue<true> },
{ id: ParamValue<false> }
{ id: ParamValue<false> },
RouteMeta,
never
>
}
}
40 changes: 30 additions & 10 deletions docs/guide/typescript.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import type {
ParamValueOneOrMore,
ParamValueZeroOrMore,
ParamValueZeroOrOne,
RouteMeta,
} from 'vue-router'
declare module 'vue-router/auto-routes' {
export interface RouteNamedMap {
Expand All @@ -60,7 +61,20 @@ declare module 'vue-router/auto-routes' {
// these are the raw param types (accept numbers, strings, booleans, etc)
{ path: ParamValue<true> },
// these are the normalized params as found in useRoute().params
{ path: ParamValue<false> }
{ path: ParamValue<false> },
// these are the `meta` fields
RouteMeta,
// this is a union of all children route names
// if the route does not have nested routes, pass `never` or omit this generic entirely
'custom-dynamic-child-name'
>
'custom-dynamic-child-name': RouteRecordInfo<
'custom-dynamic-child-name',
'/added-during-runtime/[...path]/child',
{ path: ParamValue<true> },
{ path: ParamValue<false> },
RouteMeta,
never
>
}
}
Expand All @@ -76,13 +90,19 @@ import { useRoute, type RouteLocationNormalizedLoaded } from 'vue-router'
// ---cut-end---
// @errors: 2322 2339
// @moduleResolution: bundler
// these are all valid
const userWithIdCasted = useRoute() as RouteLocationNormalizedLoaded<'/users/[id]'>
userWithIdCasted.params.id
const userWithIdTypeParam = useRoute<'/users/[id]'>()
userWithIdTypeParam.params.id
// 👇 this one is the easiest to write because it autocompletes
const userWithIdParam = useRoute('/users/[id]')
userWithIdParam.params
// ^?
// These are all valid ways to get a typed route and return the
// provided route's and any of its child routes' typings.
// Note that `/users/[id]/edit` is a child route
// of `/users/[id]` in this example.

// Not recommended, since this leaves out any child routes' typings.
const userRouteWithIdCasted = useRoute() as RouteLocationNormalizedLoaded<'/users/[id]'>
userRouteWithIdCasted.params.id
// Better way, but no autocompletion.
const userRouteWithIdTypeParam = useRoute<'/users/[id]'>()
userRouteWithIdTypeParam.params.id
// 👇 This one is the easiest to write because it autocompletes.
const userRouteWithIdParam = useRoute('/users/[id]')
userRouteWithIdParam.name
// ^?
```
38 changes: 25 additions & 13 deletions docs/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,31 +102,43 @@ After adding this plugin, **start the dev server** (usually `npm run dev`) **to
// It's recommended to commit this file.
// Make sure to add this file to your tsconfig.json file as an "includes" or "files" entry.

import type {
RouteRecordInfo,
ParamValue,
ParamValueOneOrMore,
ParamValueZeroOrMore,
ParamValueZeroOrOne,
} from 'vue-router'

declare module 'vue-router/auto-routes' {
import type {
RouteRecordInfo,
ParamValue,
ParamValueOneOrMore,
ParamValueZeroOrMore,
ParamValueZeroOrOne,
RouteMeta,
} from 'vue-router'

/**
* Route name map generated by unplugin-vue-router
*/
export interface RouteNamedMap {
'/': RouteRecordInfo<'/', '/', Record<never, never>, Record<never, never>>
'/': RouteRecordInfo<
'/',
'/',
Record<never, never>,
Record<never, never>,
RouteMeta,
never
>
'/about': RouteRecordInfo<
'/about',
'/about',
Record<never, never>,
Record<never, never>
Record<never, never>,
RouteMeta,
never
>
'/users/[id]': RouteRecordInfo<
'/[id]',
'/:id',
'/users/[id]',
'/users/:id',
{ id: ParamValue<true> },
{ id: ParamValue<false> }
{ id: ParamValue<false> },
RouteMeta,
never
>
}
}
Expand Down
13 changes: 13 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@
"vitest": "vitest --typecheck",
"docs": "vitepress dev docs",
"docs:build": "vitepress build docs",
"docs:preview": "vitepress preview docs",
"lint": "prettier -c '{src,test,e2e,examples,playground}/**/*.{ts,vue}'",
"play": "npm -C playground run dev",
"play:build": "npm -C playground run build",
Expand Down Expand Up @@ -198,5 +199,17 @@
"vuefire": "^3.2.1",
"webpack": "^5.97.1",
"yorkie": "^2.0.0"
},
"pnpm": {
"onlyBuiltDependencies": [
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this should be in this PR. With pnpm v10, builds have to be approved. After you do, pnpm adds "approved builds" to package.json.

"@parcel/watcher",
"esbuild",
"protobufjs",
"vue-demi",
"yorkie"
],
"patchedDependencies": {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This temporary patch needs to be removed before merging.

"vue-router": "patches/vue-router.patch"
}
}
}
73 changes: 73 additions & 0 deletions patches/vue-router.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
diff --git a/dist/vue-router.d.ts b/dist/vue-router.d.ts
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This temporary patch needs to be removed before merging.

index 8f2c2e5a39d50cf517b4aa512d363f402ce2255a..0c9864363cae71d34327a78025629d34240e38e7 100644
--- a/dist/vue-router.d.ts
+++ b/dist/vue-router.d.ts
@@ -871,6 +871,22 @@ export declare type RouteMap = TypesConfig extends Record<'RouteNamedMap', infer
*/
export declare type RouteMapGeneric = Record<string | symbol, RouteRecordInfo>;

+/**
+ * Returns a union of route names from children of the route with given route name
+ */
+export type GetDeepChildrenRouteNames<T extends keyof RouteMap> =
+ RouteMap[T] extends RouteRecordInfo<any, any, any, any, any, infer N>
+ ? N extends any
+ ? N | GetDeepChildrenRouteNames<N>
+ : never
+ : never
+
+/**
+ * Returns a union of given route name and the route names of children of that route
+ */
+export type RouteNameWithChildren<T extends keyof RouteMap> =
+ RouteMapGeneric extends RouteMap ? T : T | GetDeepChildrenRouteNames<T>
+
/**
* Interface to type `meta` fields in route records.
*
@@ -1148,7 +1164,14 @@ export declare interface _RouteRecordBase extends PathParserOptions {
* Helper type to define a Typed `RouteRecord`
* @see {@link RouteRecord}
*/
-export declare interface RouteRecordInfo<Name extends string | symbol = string, Path extends string = string, ParamsRaw extends RouteParamsRawGeneric = RouteParamsRawGeneric, Params extends RouteParamsGeneric = RouteParamsGeneric, Meta extends RouteMeta = RouteMeta> {
+export declare interface RouteRecordInfo<
+ Name extends string | symbol = string,
+ Path extends string = string,
+ ParamsRaw extends RouteParamsRawGeneric = RouteParamsRawGeneric,
+ Params extends RouteParamsGeneric = RouteParamsGeneric,
+ Meta extends RouteMeta = RouteMeta,
+ _ChildrenRouteNames extends string | symbol = never,
+> {
name: Name;
path: Path;
paramsRaw: ParamsRaw;
@@ -1740,11 +1763,14 @@ export declare interface UseLinkReturn<Name extends keyof RouteMap = keyof Route
navigate(e?: MouseEvent): Promise<void | NavigationFailure>;
}

-/**
+type GetRouteLocationNormalizedLoaded<Name extends keyof RouteMap> =
+ Name extends any ? RouteLocationNormalizedLoaded<Name> : never;
+
+/**
* Returns the current route location. Equivalent to using `$route` inside
* templates.
*/
-export declare function useRoute<Name extends keyof RouteMap = keyof RouteMap>(_name?: Name): RouteLocationNormalizedLoaded<Name>;
+export declare function useRoute<CurrentRouteName extends keyof RouteMap = keyof RouteMap>(_currentRouteName?: CurrentRouteName): GetRouteLocationNormalizedLoaded<RouteNameWithChildren<CurrentRouteName>>;

/**
* Returns the router instance. Equivalent to using `$router` inside
diff --git a/dist/vue-router.mjs b/dist/vue-router.mjs
index 399f57cd8150fe8bb96686110b859d7745e5fc8d..b5fbc8ecf857a5b0ce9dda54c323e59785700766 100644
--- a/dist/vue-router.mjs
+++ b/dist/vue-router.mjs
@@ -3774,7 +3774,7 @@ function useRouter() {
* Returns the current route location. Equivalent to using `$route` inside
* templates.
*/
-function useRoute(_name) {
+function useRoute(_currentRouteName) {
return inject(routeLocationKey);
}

14 changes: 12 additions & 2 deletions playground/src/router.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createRouter, createWebHistory } from 'vue-router'
import { routes, handleHotUpdate } from 'vue-router/auto-routes'
import type { RouteRecordInfo, ParamValue } from 'vue-router'
import type { RouteRecordInfo, ParamValue, RouteMeta } from 'vue-router'

export const router = createRouter({
history: createWebHistory(),
Expand Down Expand Up @@ -31,7 +31,17 @@ declare module 'vue-router/auto-routes' {
'custom-dynamic-name',
'/added-during-runtime/[...path]',
{ path: ParamValue<true> },
{ path: ParamValue<false> }
{ path: ParamValue<false> },
RouteMeta,
'custom-dynamic-child-name'
>
'custom-dynamic-child-name': RouteRecordInfo<
'custom-dynamic-child-name',
'/added-during-runtime/[...path]/child',
{ path: ParamValue<true> },
{ path: ParamValue<false> },
RouteMeta,
never
>
}
}
Loading
Loading