From: Eduardo San Martin Morote Date: Sat, 5 Sep 2020 10:54:20 +0000 (+0200) Subject: feat(types): allow extending meta fields (#407) X-Git-Tag: v4.0.0-beta.10~66 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=706e84f0099a2a04485dfa98449fdc875442bb49;p=thirdparty%2Fvuejs%2Frouter.git feat(types): allow extending meta fields (#407) --- diff --git a/src/index.ts b/src/index.ts index a4bd08a3..cf38b32d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -20,6 +20,7 @@ export { } from './matcher/pathParserRanker' export { + RouteMeta, _RouteLocationBase, _RouteRecordBase, RouteLocationRaw, diff --git a/src/types/index.ts b/src/types/index.ts index e858f554..026db0e8 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -121,7 +121,7 @@ export interface _RouteLocationBase { /** * Merged `meta` properties from all of the matched route records. */ - meta: Record + meta: RouteMeta } // matched contains resolved components @@ -216,9 +216,11 @@ export interface _RouteRecordBase extends PathParserOptions { /** * Arbitrary data attached to the record. */ - meta?: Record + meta?: RouteMeta } +export interface RouteMeta extends Record {} + export type RouteRecordRedirectOption = | RouteLocationRaw | ((to: RouteLocation) => RouteLocationRaw) diff --git a/test-dts/meta.test-d.ts b/test-dts/meta.test-d.ts new file mode 100644 index 00000000..ac94718b --- /dev/null +++ b/test-dts/meta.test-d.ts @@ -0,0 +1,43 @@ +import { createRouter, createWebHistory, expectType } from './index' +import { createApp, defineComponent } from 'vue' + +const component = defineComponent({}) + +declare module './index' { + interface RouteMeta { + requiresAuth?: boolean + nested: { foo: string } + } +} + +const router = createRouter({ + history: createWebHistory(), + routes: [ + { + path: '/', + component, + meta: { + requiresAuth: true, + lol: true, + nested: { + foo: 'bar', + }, + }, + }, + { + path: '/foo', + // @ts-ignore + component, + // @ts-expect-error + meta: {}, + }, + ], +}) + +router.beforeEach(to => { + expectType<{ requiresAuth?: Boolean; nested: { foo: string } }>(to.meta) + if (to.meta.nested.foo == 'foo' || to.meta.lol) return false +}) + +const app = createApp({}) +app.use(router)