From: pikax Date: Wed, 21 Apr 2021 09:59:19 +0000 (+0100) Subject: chore: add createRouter templated options X-Git-Tag: v4.1.0~112 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=47452982a399361c60086ad4e3e6dd42b13836f8;p=thirdparty%2Fvuejs%2Frouter.git chore: add createRouter templated options --- diff --git a/src/index.ts b/src/index.ts index 83ac531e..46ead1ad 100644 --- a/src/index.ts +++ b/src/index.ts @@ -49,6 +49,7 @@ export type { RouteRecordRedirectOption, NamedLocationMap, ExtractNamedRoutes, + ExtractRoutes, // route records _RouteRecordBase, RouteMeta, diff --git a/src/router.ts b/src/router.ts index 68b0da8f..76c720b7 100644 --- a/src/router.ts +++ b/src/router.ts @@ -185,7 +185,7 @@ export interface RouterOptions extends PathParserOptions { /** * Router instance */ -export interface Router { +export interface Router { /** * @internal */ @@ -197,7 +197,7 @@ export interface Router { /** * Original options object passed to create the Router */ - readonly options: RouterOptions + readonly options: Options /** * Allows turning off the listening of history events. This is a low level api for micro-frontends. @@ -360,7 +360,9 @@ export interface Router { * * @param options - {@link RouterOptions} */ -export function createRouter(options: RouterOptions): Router { +export function createRouter( + options: Options +): Router { const matcher = createRouterMatcher(options.routes, options) const parseQuery = options.parseQuery || originalParseQuery const stringifyQuery = options.stringifyQuery || originalStringifyQuery @@ -1157,7 +1159,7 @@ export function createRouter(options: RouterOptions): Router { let started: boolean | undefined const installedApps = new Set() - const router: Router = { + const router: Router = { currentRoute, listening: true, diff --git a/src/types/index.ts b/src/types/index.ts index 85a60deb..7ececb4c 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -6,7 +6,7 @@ import { HistoryState } from '../history/common' import { NavigationFailure } from '../errors' import { NamedLocationMap } from './named' -export { NamedLocationMap, ExtractNamedRoutes } from './named' +export { NamedLocationMap, ExtractNamedRoutes, ExtractRoutes } from './named' export type Lazy = () => Promise export type Override = Pick> & U diff --git a/src/types/named.ts b/src/types/named.ts index d9e0ef05..d238da59 100644 --- a/src/types/named.ts +++ b/src/types/named.ts @@ -1,3 +1,5 @@ +import { Router } from '../router' + /** * This will flat the routes into an object with `key` === `router.name` * and the value will be `unknown` since we don't have way to describe params types @@ -20,6 +22,10 @@ type RouteFix = T extends { name: string; children: any } ? T & { name: never } : { name: never; children: never } +export type ExtractRoutes = ExtractNamedRoutes< + T['options']['routes'] +> + /** * Used to define typed named locations * @example diff --git a/test-dts/namedRoutes.test-d.ts b/test-dts/namedRoutes.test-d.ts index 577040a1..5fea37fa 100644 --- a/test-dts/namedRoutes.test-d.ts +++ b/test-dts/namedRoutes.test-d.ts @@ -1,4 +1,9 @@ -import { ExtractNamedRoutes, Router } from './index' +import { + ExtractNamedRoutes, + Router, + ExtractRoutes, + createRouter, +} from './index' import { DefineComponent } from 'vue' declare const Comp: DefineComponent @@ -58,7 +63,7 @@ typed['non-existing'] declare module './index' { interface NamedLocationMap { 'my-other-path': { - sss: number + id: string } } } @@ -68,9 +73,9 @@ declare const router: Router router.push({ name: 'my-other-path', params: { - sss: 1, + id: '222', // @ts-expect-error does not exist - xxxx: '22', + nonExistent: '22', }, }) @@ -78,3 +83,14 @@ router.push({ // @ts-expect-error location name does not exist name: 'random-location', }) + +const otherRouter = createRouter({ + history: {} as any, + routes: [{ path: 'e', name: 'test', component: Comp }] as const, +}) + +declare const otherRoutes: ExtractRoutes + +otherRoutes.test +// @ts-expect-error +otherRoutes.test2