From: pikax Date: Fri, 9 Apr 2021 12:09:34 +0000 (+0100) Subject: almost working X-Git-Tag: v4.1.0~120 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=47177b00f3c921f5786242d035c375163ecec9b6;p=thirdparty%2Fvuejs%2Frouter.git almost working --- diff --git a/src/index.ts b/src/index.ts index d5975392..34ba620e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -47,7 +47,6 @@ export type { RouteLocationMatched, RouteLocationOptions, RouteRecordRedirectOption, - RouteNamedLocation, defineRoutes, NamedLocationMap, ExtractNamedRoutes, diff --git a/src/types/index.ts b/src/types/index.ts index 6d313ab2..21abc29b 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -6,12 +6,7 @@ import { HistoryState } from '../history/common' import { NavigationFailure } from '../errors' import { NamedLocationMap } from './named' -export { - RouteNamedLocation, - NamedLocationMap, - defineRoutes, - ExtractNamedRoutes, -} from './named' +export { NamedLocationMap, defineRoutes, ExtractNamedRoutes } from './named' export type Lazy = () => Promise export type Override = Pick> & U diff --git a/src/types/named.ts b/src/types/named.ts index 750febe2..e3d83264 100644 --- a/src/types/named.ts +++ b/src/types/named.ts @@ -1,148 +1,45 @@ -import { RouteLocationOptions, RouteRecordRaw, _RouteRecordBase } from '.' +import { RouteRecordRaw, _RouteRecordBase } from '.' -// export type ExtractNameRoute> = -// | ([T] extends [{ name: string }] ? { [K in T['name']]: unknown } : never) -// | ([T] extends [{ children: Readonly }] -// ? ExtractNamedRoutes -// : never) - -export type ExtractNamedRoute> = [ - T -] extends [{ name: string; readonly children?: any[] }] - ? { [K in T['name']]: unknown } & - ([T['children']] extends [Readonly>] +export type ExtractNamedRoutes = [T] extends [ReadonlyArray] + ? ExtractNamedRoutes + : ([T] extends [{ name: string }] ? { [K in T['name']]: unknown } : {}) & + ([T] extends [{ children?: undefined | unknown | any }] ? ExtractNamedRoutes : {}) - : never - -export type ExtractNamedRoutes< - T extends Readonly> | undefined -> = T extends Readonly> - ? // ? [R] extends [{ name: string /*params?: infer Params*/ }] - [R] extends [_RouteRecordBase] - ? ExtractNamedRoute - : {} - : 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 - -// 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 -// } -// ] -// } -// ] -// > +// declare const test: ExtractNamedRoutes< +// [ +// { +// path: 'my-path' +// name: 'test' +// children: [] +// }, +// { +// path: 'my-path' +// name: 'my-other-path' +// // children: [] +// }, +// { +// path: 'random' +// name: 'tt' +// children: [ +// { +// path: 'random-child' +// name: 'random-child' +// } +// ] +// } +// ] // > - -// interface XXW extends XXX {} - -// const xxx2: XXW -// type TypedRoutes = ExtractNamedRoutes - -// export type ExtractNamedRoutes< -// T extends Array | Readonly> -// > = T extends Array -// ? [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> -// ? [R] extends [{ name: string /*params?: infer Params*/ }] -// ? { -// [K in R['name']]: unknown /*TODO add params*/ /*R['params'] extends Params ? Params : Params*/ -// } -// : never -// : never +// test +// test['my-other-path'] +// test.test, test.tt +// test['random-child'] export function defineRoutes< T extends Array> >(routes: T): ExtractNamedRoutes { return routes as any } -export interface NamedLocationMap {} - -export interface RouteNamedLocation< - T extends keyof NamedLocationMap = keyof NamedLocationMap -> extends RouteLocationOptions { - name: T - // params: NamedLocationMap[T] -} -// declare const r: [ -// { -// name: 'test' -// params: { -// number: 1 -// } -// }, -// { -// name: 'LOL' -// params: { -// sss: 'sss' -// } -// }, -// { -// name: 'other' -// }, -// { -// path: 'ssss' -// } -// ] - -// declare const x: ExtractNamedRoutes +export interface NamedLocationMap {} diff --git a/test-dts/namedRoutes.test-d.ts b/test-dts/namedRoutes.test-d.ts index ffe62f10..6ad366d6 100644 --- a/test-dts/namedRoutes.test-d.ts +++ b/test-dts/namedRoutes.test-d.ts @@ -25,14 +25,36 @@ const routes = [ }, ], }, -] as const +] -type TypedRoutes = ExtractNamedRoutes +type TypedRoutes = ExtractNamedRoutes< [ + { + path: 'my-path', + name: 'test', + // component: , + }, + { + path: 'my-path', + name: 'my-other-path', + // component: Comp, + }, + { + path: 'random', + name: 'tt', + children: [ + { + path: 'random-child', + name: 'random-child', + component: {}, + }, + ], + }, +]> declare module './index' { - interface NamedLocationMap { + interface NamedLocationMap { 'my-other-path': { - sss: number + sss: number } } } @@ -43,9 +65,9 @@ router.push({ name: 'my-other-path', params: { sss: 1, - // @ts-expect-error - xxxx: '22', - }, + // @ts-expect-error does not exist + xxxx: '22' + } }) router.push({