vue-router icon indicating copy to clipboard operation
vue-router copied to clipboard

Creating a type declaration for `next`

Open Midnighter opened this issue 5 years ago • 4 comments

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;

Midnighter avatar May 07 '19 12:05 Midnighter

This will definitely be exposed, not sure about the wording yet

posva avatar May 07 '19 13:05 posva

There was https://github.com/vuejs/vue-router/pull/2497, the wording isn't good though

posva avatar May 08 '19 17:05 posva

@posva That PR has confilcts, but the creator of that pr no longer takes any action.

MartinYounghoonKim avatar Sep 02 '19 13:09 MartinYounghoonKim

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 do

import { NavigationGuard } from 'vue-router';
type Next = Parameters<NavigationGuard>[2];

voool avatar Sep 10 '19 15:09 voool