vue-router
vue-router copied to clipboard
Creating a type declaration for `next`
What problem does this feature solve?
At the moment there is a type definition for NavigationGuard
in router.d.ts
. This works nicely. Within that, the next
argument is a function that accepts a union of different types as argument.
next: (to?: RawLocation | false | ((vm: V) => any) | void) => void
I think it'd be neat to easily use this just like the Route
interface declaration in, for example, a components navigation guard.
What does the proposed API look like?
Export a type definition maybe called NavigationGuardCallback
in router.d.ts
.
export type NavigationGuardCallback = (to?: RawLocation | false | ((vm: V) => any) | void) => void;
export type NavigationGuard<V extends Vue = Vue> = (
to: Route,
from: Route,
next: NavigationGuardCallback
) => any;
This will definitely be exposed, not sure about the wording yet
There was https://github.com/vuejs/vue-router/pull/2497, the wording isn't good though
@posva That PR has confilcts, but the creator of that pr no longer takes any action.
Fwiw, if anyone wants a workaround that doesn't involve creating their own copy of the next
interface, see https://github.com/vuejs/vue-router/pull/2497#issuecomment-474010032
As a workaround, to grab the
next
interface with Typescript you can doimport { NavigationGuard } from 'vue-router'; type Next = Parameters<NavigationGuard>[2];