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

types(useRoute): add generic for params #1159

Closed
wants to merge 1 commit into from
Closed
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
8 changes: 5 additions & 3 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export interface RouteLocationMatched extends RouteRecordNormalized {
*
* @internal
*/
export interface _RouteLocationBase {
export interface _RouteLocationBase<Params extends RouteParams = RouteParams> {
/**
* Percentage encoded pathname section of the URL.
*/
Expand All @@ -130,7 +130,7 @@ export interface _RouteLocationBase {
/**
* Object of decoded params extracted from the `path`.
*/
params: RouteParams
params: Params
/**
* Contains the location we were initially trying to access before ending up
* on the current location.
Expand All @@ -146,7 +146,9 @@ export interface _RouteLocationBase {
/**
* {@link RouteLocationRaw} with
*/
export interface RouteLocationNormalizedLoaded extends _RouteLocationBase {
export interface RouteLocationNormalizedLoaded<
Params extends RouteParams = RouteParams
> extends _RouteLocationBase<Params> {
/**
* Array of {@link RouteLocationMatched} containing only plain components (any
* lazy-loaded components have been loaded and were replaced inside of the
Expand Down
8 changes: 5 additions & 3 deletions src/useApi.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { inject } from 'vue'
import { routerKey, routeLocationKey } from './injectionSymbols'
import { Router } from './router'
import { RouteLocationNormalizedLoaded } from './types'
import { RouteLocationNormalizedLoaded, RouteParams } from './types'

/**
* Returns the router instance. Equivalent to using `$router` inside
Expand All @@ -15,6 +15,8 @@ export function useRouter(): Router {
* Returns the current route location. Equivalent to using `$route` inside
* templates.
*/
export function useRoute(): RouteLocationNormalizedLoaded {
return inject(routeLocationKey)!
export function useRoute<
Params extends RouteParams = RouteParams
>(): RouteLocationNormalizedLoaded<Params> {
return inject<RouteLocationNormalizedLoaded<Params>>(routeLocationKey)!
}