From: pikax Date: Fri, 9 Apr 2021 15:39:50 +0000 (+0100) Subject: add comment X-Git-Tag: v4.1.0~115 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=fcf953db900f293148633328b9e26729f3fe380a;p=thirdparty%2Fvuejs%2Frouter.git add comment --- diff --git a/src/types/named.ts b/src/types/named.ts index bf9097d0..d9e0ef05 100644 --- a/src/types/named.ts +++ b/src/types/named.ts @@ -1,5 +1,7 @@ -import { RouteRecordRaw, _RouteRecordBase } from '.' - +/** + * 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 + */ export type ExtractNamedRoutes = [T] extends [ReadonlyArray] ? ExtractNamedRoutes> : ([T] extends [{ name: string }] ? { [K in T['name']]: unknown } : {}) & @@ -13,70 +15,25 @@ export type ExtractNamedRoutes = [T] extends [ReadonlyArray] type RouteFix = T extends { name: string; children: any } ? T : T extends { name: string } - ? T & { children: never[] } + ? T & { children: never } : T extends { children: any } - ? T & { name: '' } - : { name: ''; children: never } - -// // declare const xxx: NamedRoutes< -// // | { -// // name: 'LOL' -// // } -// // | { name: 'xxx' } -// // | { children: {} } -// // > -// // xxx.name - -// declare const typed: ExtractNamedRoutes< -// [ -// { -// path: 'my-path' -// name: 'test' -// // children must be declared :( -// // children: [] -// }, -// { -// path: 'my-path' -// name: 'my-other-path' -// // children must be declared :( -// // children: [] -// }, -// { -// path: 'random' -// name: 'tt' -// children: [ -// { -// path: 'random-child' -// name: 'random-child' -// // children: [] -// } -// ] -// }, -// { -// name: '1' -// children: [ -// { -// name: '2' -// children: [{ name: '3'; children: [{ name: '4' }] }] -// } -// ] -// } -// ] -// > - -// typed['my-other-path'] -// typed['random-child'] -// typed.test -// typed.tt -// typed[1] -// typed[2] -// typed[3] -// typed[4] - -export function defineRoutes< - T extends Array> ->(routes: T): ExtractNamedRoutes { - return routes as any -} + ? T & { name: never } + : { name: never; children: never } +/** + * Used to define typed named locations + * @example + * ```ts + * declare module 'vue-router' { + * interface NamedLocationMap { + * // 'home' no params + * home: {} + * // 'product' `{id: string}` required parameter + * product: { + * id: string + * } + * } + * } + * ``` + */ export interface NamedLocationMap {} diff --git a/test-dts/namedRoutes.test-d.ts b/test-dts/namedRoutes.test-d.ts index 83bd8688..577040a1 100644 --- a/test-dts/namedRoutes.test-d.ts +++ b/test-dts/namedRoutes.test-d.ts @@ -30,7 +30,12 @@ const routes = [ children: [ { name: '2', - children: [{ name: '3', children: [{ name: '4' }] }], + children: [ + { + name: '3', + children: [{ name: '4' }, { path: '', children: [{ name: '5' }] }], + }, + ], }, ], }, @@ -46,6 +51,7 @@ typed[1] typed[2] typed[3] typed[4] +typed[5] //@ts-expect-error typed['non-existing']