From: pikax Date: Fri, 9 Apr 2021 15:25:51 +0000 (+0100) Subject: no need for name and children 😎 X-Git-Tag: v4.1.0~116 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=dbea732aa9c660e773f054e83d84a7d9f746ab43;p=thirdparty%2Fvuejs%2Frouter.git no need for name and children 😎 --- diff --git a/src/types/named.ts b/src/types/named.ts index c2ea5b54..bf9097d0 100644 --- a/src/types/named.ts +++ b/src/types/named.ts @@ -1,28 +1,7 @@ import { RouteRecordRaw, _RouteRecordBase } from '.' -type OptionalPropertyNames = { - [K in keyof T]-?: {} extends { [P in K]: T[K] } ? K : never -}[keyof T] - -type SpreadProperties = { - [P in K]: L[P] | Exclude -} - -type Id = T extends infer U ? { [K in keyof U]: U[K] } : never - -type SpreadTwo = Id< - Pick> & - Pick>> & - Pick, keyof L>> & - SpreadProperties & keyof L> -> - -type Spread = A extends [infer L, ...infer R] - ? SpreadTwo> - : unknown - export type ExtractNamedRoutes = [T] extends [ReadonlyArray] - ? ExtractNamedRoutes> + ? ExtractNamedRoutes> : ([T] extends [{ name: string }] ? { [K in T['name']]: unknown } : {}) & ([T] extends [{ children?: undefined | unknown | any }] ? T['children'] extends undefined @@ -30,7 +9,8 @@ export type ExtractNamedRoutes = [T] extends [ReadonlyArray] : ExtractNamedRoutes : {}) -type RouteFiller = T extends { name: string; children: any } +// Needed to populate the missing props +type RouteFix = T extends { name: string; children: any } ? T : T extends { name: string } ? T & { children: never[] } @@ -38,62 +18,60 @@ type RouteFiller = T extends { name: string; children: any } ? T & { name: '' } : { name: ''; children: never } -export type NamedRoutes = RouteFiller +// // declare const xxx: NamedRoutes< +// // | { +// // name: 'LOL' +// // } +// // | { name: 'xxx' } +// // | { children: {} } +// // > +// // xxx.name -// declare const xxx: NamedRoutes< -// | { -// name: 'LOL' +// 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' }] }] +// } +// ] // } -// | { 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] +// 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> diff --git a/test-dts/namedRoutes.test-d.ts b/test-dts/namedRoutes.test-d.ts index 806de2fe..83bd8688 100644 --- a/test-dts/namedRoutes.test-d.ts +++ b/test-dts/namedRoutes.test-d.ts @@ -8,15 +8,11 @@ const routes = [ path: 'my-path', name: 'test', component: Comp, - // children must be declared :( - children: [], }, { path: 'my-path', name: 'my-other-path', component: Comp, - // children must be declared :( - children: [], }, { path: 'random', @@ -26,7 +22,6 @@ const routes = [ path: 'random-child', name: 'random-child', component: Comp, - children: [], }, ], },