]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
feat(types): allow extending meta fields (#407)
authorEduardo San Martin Morote <posva@users.noreply.github.com>
Sat, 5 Sep 2020 10:54:20 +0000 (12:54 +0200)
committerGitHub <noreply@github.com>
Sat, 5 Sep 2020 10:54:20 +0000 (12:54 +0200)
src/index.ts
src/types/index.ts
test-dts/meta.test-d.ts [new file with mode: 0644]

index a4bd08a3a5d46459e46e9f4b972b9ff594c470c0..cf38b32df14f1fbd1c9746d7101e9e6bd1359fb8 100644 (file)
@@ -20,6 +20,7 @@ export {
 } from './matcher/pathParserRanker'
 
 export {
+  RouteMeta,
   _RouteLocationBase,
   _RouteRecordBase,
   RouteLocationRaw,
index e858f55498a654e14741b2c8e71ac28fc4e385e9..026db0e8e89a00fadf406901d41f43dd00683423 100644 (file)
@@ -121,7 +121,7 @@ export interface _RouteLocationBase {
   /**
    * Merged `meta` properties from all of the matched route records.
    */
-  meta: Record<string | number | symbol, any>
+  meta: RouteMeta
 }
 
 // matched contains resolved components
@@ -216,9 +216,11 @@ export interface _RouteRecordBase extends PathParserOptions {
   /**
    * Arbitrary data attached to the record.
    */
-  meta?: Record<string | number | symbol, any>
+  meta?: RouteMeta
 }
 
+export interface RouteMeta extends Record<string | number | symbol, any> {}
+
 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 (file)
index 0000000..ac94718
--- /dev/null
@@ -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)