Issue
Let's say I have the following interface which defines a function:
export declare interface NavigationGuard {
(to: RouteLocationNormalized, from: RouteLocationNormalized, next: NavigationGuardNext): NavigationGuardReturn | Promise<NavigationGuardReturn>;
}
I'd like to end up with this type:
export SomeNavigationGuardType = (to: RouteLocationNormalized, from: RouteLocationNormalized): NavigationGuardReturn | Promise<NavigationGuardReturn>
Utilising the original NavigationGuard
type ...
I have tried utilising the utility type Parameters
, extracting and using Omit
to remove the next param, but the Parameters
utility type is extracting to a tuple.
I've tried utilising:
type NavigationGuardParams = Omit<Parameters<NavigationGuard>, 'next'>
export type UseRouterNivigationGuards = {
beforeEach?: (arg: NavigationGuardParams) => ReturnType<NavigationGuard>
beforeResolve?: (arg: NavigationGuardParams) => ReturnType<NavigationGuard>
afterEach?: NavigationHookAfter
}
and
type NavigationGuardParams = Omit<Parameters<NavigationGuard>, 'next'>
export type UseRouterNivigationGuards = {
beforeEach?: (...arg: NavigationGuardParams) => ReturnType<NavigationGuard>
beforeResolve?: (...arg: NavigationGuardParams) => ReturnType<NavigationGuard>
afterEach?: NavigationHookAfter
}
But both results in a usage / param error, the first giving this:
"Expected 1 arguments, but got 2."
The second attempt giving:
"Argument of type '[RouteLocationNormalized, RouteLocationNormalized]' is not assignable to parameter of type 'NavigationGuardParams'."
I'm therefore asking for help! Would anyone have a trick to be able to achieve this?
Solution
You can use a conditional type with infer
to remove the next
element from the tuple returned by Parameters
:
type SomeNavigationGuardType = (...args: Parameters<NavigationGuard> extends [...infer T, next: unknown] ? T : never) => ReturnType<NavigationGuard>
Answered By - VerZsuT
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.