From 6700327f67b793cf28f77148ee734237d5382d7e Mon Sep 17 00:00:00 2001 From: pikax Date: Thu, 8 Apr 2021 18:21:29 +0100 Subject: [PATCH] some code changes to make the demo kinda work --- src/router.ts | 6 +++--- src/types/named.ts | 7 ++++--- test-dts/namedRoutes.test-d.ts | 21 ++++++++++++++------- 3 files changed, 21 insertions(+), 13 deletions(-) diff --git a/src/router.ts b/src/router.ts index f731505a..6e902a40 100644 --- a/src/router.ts +++ b/src/router.ts @@ -254,15 +254,15 @@ export interface Router { * * @param to - Route location to navigate to */ - push(to: RouteLocationRaw): Promise + // push(to: RouteLocationRaw): Promise /** * Programmatically navigate to a new URL by pushing an entry in the history * stack. * * @param to - typed route location */ - push( - to: RouteNamedLocation + push( + to: RouteNamedLocation ): Promise /** diff --git a/src/types/named.ts b/src/types/named.ts index c01521af..663c3b5b 100644 --- a/src/types/named.ts +++ b/src/types/named.ts @@ -33,10 +33,11 @@ export function defineRoutes< } export interface NamedLocationMap {} -export interface RouteNamedLocation - extends RouteLocationOptions { +export interface RouteNamedLocation< + T extends keyof NamedLocationMap = keyof NamedLocationMap +> extends RouteLocationOptions { name: T - params: NamedLocationMap[T] + // params: NamedLocationMap[T] } declare const r: [ diff --git a/test-dts/namedRoutes.test-d.ts b/test-dts/namedRoutes.test-d.ts index 92870cdf..93cc9541 100644 --- a/test-dts/namedRoutes.test-d.ts +++ b/test-dts/namedRoutes.test-d.ts @@ -1,9 +1,11 @@ -import { defineRoutes } from './index' +// NOTE `ExtractNamedRoutes` is not exposed on build, you might need to add export to the type manually + +import { ExtractNamedRoutes, Router } from './index' import { DefineComponent } from 'vue' declare const Comp: DefineComponent -const routes = defineRoutes([ +const routes = [ { path: 'my-path', name: 'test', @@ -19,11 +21,16 @@ const routes = defineRoutes([ // path: 'my-path', // component: Comp, // } as const, -]) +] + +type TypedRoutes = ExtractNamedRoutes declare module './index' { - interface RouteMeta { - requiresAuth?: boolean - nested: { foo: string } - } + interface NamedLocationMap extends TypedRoutes {} } + +declare const router: Router + +router.push({ + name: '', +}) -- 2.47.3