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] 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) { + } +})