*
* @param to - Route location to navigate to
*/
- // push(to: RouteLocationRaw): Promise<NavigationFailure | void | undefined>
+ push(to: RouteLocationRaw): Promise<NavigationFailure | void | undefined>
/**
* Programmatically navigate to a new URL by pushing an entry in the history
* stack.
*
* @param to - typed route location
*/
- push(
- to: RouteNamedLocation
- ): Promise<NavigationFailure | void | undefined>
+ // push(to: RouteNamedLocation): Promise<NavigationFailure | void | undefined>
/**
* Programmatically navigate to a new URL by replacing the current entry in
import { RouteRecord, RouteRecordNormalized } from '../matcher/types'
import { HistoryState } from '../history/common'
import { NavigationFailure } from '../errors'
+import { NamedLocationMap } from './named'
export {
RouteNamedLocation,
* @internal
*/
export interface LocationAsRelativeRaw {
- name?: RouteRecordName
+ name?: {} extends NamedLocationMap ? RouteRecordName : keyof NamedLocationMap
params?: RouteParamsRaw
}
-import { RouteLocationOptions, RouteRecordRaw } from '.'
+import { RouteLocationOptions, RouteRecordRaw, _RouteRecordBase } from '.'
+
+// export type ExtractNameRoute<T extends Readonly<RouteRecordRaw>> =
+// | ([T] extends [{ name: string }] ? { [K in T['name']]: unknown } : never)
+// | ([T] extends [{ children: Readonly<RouteRecordRaw[]> }]
+// ? ExtractNamedRoutes<T['children']>
+// : never)
+
+export type ExtractNamedRoute<T extends Readonly<_RouteRecordBase>> = [
+ T
+] extends [{ name: string; readonly children?: any[] }]
+ ? { [K in T['name']]: unknown } &
+ ([T['children']] extends [Readonly<Array<_RouteRecordBase>>]
+ ? ExtractNamedRoutes<T['children']>
+ : {})
+ : never
export type ExtractNamedRoutes<
- T extends Array<RouteRecordRaw>
-> = T extends Array<infer R>
- ? [R] extends [{ name: string /*params?: infer Params*/ }]
- ? {
- [K in R['name']]: unknown /*TODO add params*/ /*R['params'] extends Params ? Params : Params*/
- }
- : never
+ T extends Readonly<Array<_RouteRecordBase>> | undefined
+> = T extends Readonly<Array<infer R>>
+ ? // ? [R] extends [{ name: string /*params?: infer Params*/ }]
+ [R] extends [_RouteRecordBase]
+ ? ExtractNamedRoute<R>
+ : {}
: never
+// const routes = [
+// {
+// path: 'my-path',
+// name: 'test',
+// component: Comp,
+// },
+// {
+// path: 'my-path',
+// name: 'my-other-path',
+// component: Comp,
+// },
+// {
+// path: 'random',
+// name: 'tt',
+// children: [
+// {
+// path: 'random-child',
+// name: 'random-child',
+// component: Comp,
+// },
+// ],
+// },
+// ] as const
+
+// type TypedRoutes = ExtractNamedRoutes<typeof routes>
+
+// declare const Comp: () => any
+
+// const routes = [
+// {
+// path: 'my-path',
+// name: 'test',
+// component: Comp,
+// },
+// {
+// path: 'my-path',
+// // name: 'my-other-path',
+// component: Comp,
+// },
+// ] as const
+
+// type XXX = ExtractNamedRoutes<
+// Readonly<
+// [
+// {
+// path: 'ttt'
+// name: 'sddsd'
+// component: any
+// children: [
+// {
+// path: 'ttt'
+// name: 'child'
+// component: any
+// },
+// {
+// path: 'ttt'
+// name: 'child-other'
+// component: any
+// }
+// ]
+// }
+// ]
+// >
+// >
+
+// interface XXW extends XXX {}
+
+// const xxx2: XXW
+// type TypedRoutes = ExtractNamedRoutes<typeof routes>
+
// export type ExtractNamedRoutes<
// T extends Array<RouteRecordRaw> | Readonly<Array<RouteRecordRaw>>
// > = T extends Array<infer R>
// params: NamedLocationMap[T]
}
-declare const r: [
- {
- name: 'test'
- params: {
- number: 1
- }
- },
- {
- name: 'LOL'
- params: {
- sss: 'sss'
- }
- },
- {
- name: 'other'
- },
- {
- path: 'ssss'
- }
-]
+// declare const r: [
+// {
+// name: 'test'
+// params: {
+// number: 1
+// }
+// },
+// {
+// name: 'LOL'
+// params: {
+// sss: 'sss'
+// }
+// },
+// {
+// name: 'other'
+// },
+// {
+// path: 'ssss'
+// }
+// ]
-declare const x: ExtractNamedRoutes<typeof r>
+// declare const x: ExtractNamedRoutes<typeof r>
-// 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'
path: 'my-path',
name: 'test',
component: Comp,
- } as const,
+ },
{
path: 'my-path',
name: 'my-other-path',
component: Comp,
- } as const,
-
- // {
- // path: 'my-path',
- // component: Comp,
- // } as const,
-]
+ },
+ {
+ path: 'random',
+ name: 'tt',
+ children: [
+ {
+ path: 'random-child',
+ name: 'random-child',
+ component: Comp,
+ },
+ ],
+ },
+] as const
type TypedRoutes = ExtractNamedRoutes<typeof routes>
declare const router: Router
router.push({
- name: '',
+ name: 'my-other-path',
+})
+
+router.push({
+ // @ts-expect-error location name does not exist
+ name: 'random-location',
})