Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow defining properties in meta #407

Merged
merged 1 commit into from
Sep 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export {
} from './matcher/pathParserRanker'

export {
RouteMeta,
_RouteLocationBase,
_RouteRecordBase,
RouteLocationRaw,
Expand Down
6 changes: 4 additions & 2 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
43 changes: 43 additions & 0 deletions test-dts/meta.test-d.ts
Original file line number Diff line number Diff line change
@@ -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)