From: pikax Date: Fri, 9 Apr 2021 15:22:36 +0000 (+0100) Subject: fixed it X-Git-Tag: v4.1.0~117 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=349ff5064196ee72114a2233f6fb340c65de273f;p=thirdparty%2Fvuejs%2Frouter.git fixed it --- diff --git a/src/types/named.ts b/src/types/named.ts index c478605d..c2ea5b54 100644 --- a/src/types/named.ts +++ b/src/types/named.ts @@ -1,12 +1,100 @@ 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 }] - ? ExtractNamedRoutes + ? T['children'] extends undefined + ? {} + : ExtractNamedRoutes : {}) +type RouteFiller = T extends { name: string; children: any } + ? T + : T extends { name: string } + ? T & { children: never[] } + : T extends { children: any } + ? T & { name: '' } + : { name: ''; children: never } + +export type NamedRoutes = RouteFiller + +// 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 {