From f4698cce66f22401423a2607fcc1748d6c9963be Mon Sep 17 00:00:00 2001 From: Ravi van Rooijen Date: Tue, 24 Nov 2020 14:32:55 +0100 Subject: [PATCH 1/3] types: Add RouteMeta interface to enable module augmentation --- types/router.d.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/types/router.d.ts b/types/router.d.ts index 80de5e224..8c40415d6 100644 --- a/types/router.d.ts +++ b/types/router.d.ts @@ -126,7 +126,7 @@ interface _RouteConfigBase { children?: RouteConfig[] redirect?: RedirectOption alias?: string | string[] - meta?: any + meta?: RouteMeta beforeEnter?: NavigationGuard caseSensitive?: boolean pathToRegexpOptions?: PathToRegexpOptions @@ -153,7 +153,7 @@ export interface RouteRecord { parent?: RouteRecord redirect?: RedirectOption matchAs?: string - meta: any + meta: RouteMeta beforeEnter?: ( route: Route, redirect: (location: RawLocation) => void, @@ -205,5 +205,8 @@ export interface Route { fullPath: string matched: RouteRecord[] redirectedFrom?: string - meta?: any + meta?: RouteMeta +} + +export interface RouteMeta { } From 1a8999930d92f5e638c7be0c1a42f7bd1cc41722 Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Sat, 23 Jan 2021 18:15:19 +0100 Subject: [PATCH 2/3] Update types/router.d.ts --- types/router.d.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/types/router.d.ts b/types/router.d.ts index 8c40415d6..f8588f42f 100644 --- a/types/router.d.ts +++ b/types/router.d.ts @@ -208,5 +208,4 @@ export interface Route { meta?: RouteMeta } -export interface RouteMeta { -} +export interface RouteMeta extends Record {} From dece3e689391701f822284378d9395eef5ced19d Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Mon, 21 Jun 2021 16:01:33 +0200 Subject: [PATCH 3/3] test: add routemeta test --- types/index.d.ts | 1 + types/test/index.ts | 8 +++++--- types/test/meta.ts | 45 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 3 deletions(-) create mode 100644 types/test/meta.ts diff --git a/types/index.d.ts b/types/index.d.ts index 41bcea125..2671ca8cb 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -5,6 +5,7 @@ export default VueRouter export { RouterMode, + RouteMeta, RawLocation, RedirectOption, RouterOptions, diff --git a/types/test/index.ts b/types/test/index.ts index 44de15098..bc7755c04 100644 --- a/types/test/index.ts +++ b/types/test/index.ts @@ -18,7 +18,9 @@ const Abc = { template: '
abc
' } const Async = () => Promise.resolve({ template: '
async
' }) let err: any -if (VueRouter.isNavigationFailure(err, VueRouter.NavigationFailureType.aborted)) { +if ( + VueRouter.isNavigationFailure(err, VueRouter.NavigationFailureType.aborted) +) { err.from.fullPath.split('/') } @@ -104,7 +106,7 @@ const router = new VueRouter({ abc: Abc, asyncComponent: Async }, - meta: { auth: true }, + meta: { auth: true, nested: { foo: '' } }, beforeEnter(to, from, next) { to.params from.params @@ -229,7 +231,7 @@ const Components: ( | AsyncComponent )[] = router.getMatchedComponents() -const match: Route = router.match('/more'); +const match: Route = router.match('/more') const vm = new Vue({ router, diff --git a/types/test/meta.ts b/types/test/meta.ts new file mode 100644 index 000000000..e320ba3dd --- /dev/null +++ b/types/test/meta.ts @@ -0,0 +1,45 @@ +import VueRouter from '../index' + +const component = { template: '
home
' } + +declare module '../index' { + export interface RouteMeta { + requiresAuth?: boolean + nested: { foo: string } + } +} + +const router = new VueRouter({ + routes: [ + { + path: '/', + component, + meta: { + requiresAuth: true, + // still allowed + other: true, + nested: { + foo: 'bar' + } + } + }, + { + path: '/foo', + component, + // @ts-expect-error + meta: {} + } + ] +}) + +router.beforeEach(to => { + // should pass + if (to.meta!.requiresAuth === true) { + } + // still pass because any + if (to.meta!.lol === true) { + } + // @ts-expect-error: nested will never be true + if (to.meta!.nested === true) { + } +})