Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arguments (route, from, next) types within route guard vue3

I export a function, which I pass to router.beforeEach:

export default function (route, from, next) {
  log.debug(route.path)

  if (!identity.state.authenticated) {
    log.debug('redirecting to "login" view...')
    next({ name: 'login' })
  } else {
    next()
  }
}

But this causes 3 TypeScript errors:

TS7006: Parameter 'route' implicitly has an 'any' type.
TS7006: Parameter 'from' implicitly has an 'any' type.
TS7006: Parameter 'next' implicitly has an 'any' type.

What types would be best for those? I can make them just objects, but aren't there some types to import from vue?

like image 235
BT101 Avatar asked Oct 20 '25 17:10

BT101


1 Answers

router.beforeEach is typed as:

beforeEach(guard: NavigationGuardWithThis<undefined>): () => void

and NavigationGuardWithThis is typed as:

export interface NavigationGuardWithThis<T> {
  (
    this: T,
    to: RouteLocationNormalized,
    from: RouteLocationNormalized,
    next: NavigationGuardNext
  ): NavigationGuardReturn | Promise<NavigationGuardReturn>
}

So you need these types to import from vue-router:

  • RouteLocationNormalized

  • NavigationGuardNext

  • I'm not sure how how to import NavigationGuardReturn, but since it includes void, omitting the return is also acceptable in TypeScript.

import { RouteLocationNormalized, NavigationGuardNext } from 'vue-router'

export default function(
  to: RouteLocationNormalized,
  from: RouteLocationNormalized,
  next: NavigationGuardNext
) {
  /*...*/
}
like image 135
tony19 Avatar answered Oct 23 '25 07:10

tony19



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!