Skip to content

Commit e3078a4

Browse files
authored
fix(types): allow simpler type check of route records (vuejs#1453)
Fix vuejs#1452
1 parent 4bb989d commit e3078a4

File tree

6 files changed

+45
-192
lines changed

6 files changed

+45
-192
lines changed

packages/router/__tests__/matcher/resolve.spec.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,9 @@ describe('RouterMatcher.resolve', () => {
4242
throw new Error('not handled')
4343
} else {
4444
// use one single record
45-
// @ts-expect-error: One way or the other it false
4645
if (!resolved.matched) resolved.matched = record.map(normalizeRouteRecord)
4746
// allow passing an expect.any(Array)
4847
else if (Array.isArray(resolved.matched))
49-
// @ts-expect-error: same as above
5048
resolved.matched = resolved.matched.map(m => ({
5149
...normalizeRouteRecord(m as any),
5250
aliasOf: m.aliasOf,

packages/router/src/matcher/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ export function createRouterMatcher(
159159
removeRoute(record.name)
160160
}
161161

162-
if ('children' in mainNormalizedRecord) {
162+
if (mainNormalizedRecord.children) {
163163
const children = mainNormalizedRecord.children
164164
for (let i = 0; i < children.length; i++) {
165165
addRoute(
@@ -353,7 +353,6 @@ export function normalizeRouteRecord(
353353
aliasOf: undefined,
354354
beforeEnter: record.beforeEnter,
355355
props: normalizeRecordProps(record),
356-
// @ts-expect-error: record.children only exists in some cases
357356
children: record.children || [],
358357
instances: {},
359358
leaveGuards: new Set(),

packages/router/src/matcher/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
_RouteRecordBase,
55
_RouteRecordProps,
66
NavigationGuardNextCallback,
7-
RouteRecordSingleViewWithChildren,
7+
RouteRecordRaw,
88
} from '../types'
99
import { ComponentPublicInstance } from 'vue'
1010

@@ -32,7 +32,7 @@ export interface RouteRecordNormalized {
3232
/**
3333
* Nested route records.
3434
*/
35-
children: RouteRecordSingleViewWithChildren['children']
35+
children: RouteRecordRaw[]
3636
/**
3737
* {@inheritDoc _RouteRecordBase.meta}
3838
*/

packages/router/src/types/index.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,11 @@ export interface _RouteRecordBase extends PathParserOptions {
262262
* Arbitrary data attached to the record.
263263
*/
264264
meta?: RouteMeta
265+
266+
/**
267+
* Array of nested routes.
268+
*/
269+
children?: RouteRecordRaw[]
265270
}
266271

267272
/**
@@ -298,6 +303,9 @@ export interface RouteRecordSingleView extends _RouteRecordBase {
298303
*/
299304
component: RawRouteComponent
300305
components?: never
306+
children?: never
307+
redirect?: never
308+
301309
/**
302310
* Allow passing down params as props to the component rendered by `router-view`.
303311
*/
@@ -314,9 +322,6 @@ export interface RouteRecordSingleViewWithChildren extends _RouteRecordBase {
314322
component?: RawRouteComponent | null | undefined
315323
components?: never
316324

317-
/**
318-
* Array of nested routes.
319-
*/
320325
children: RouteRecordRaw[]
321326

322327
/**
@@ -334,6 +339,8 @@ export interface RouteRecordMultipleViews extends _RouteRecordBase {
334339
*/
335340
components: Record<string, RawRouteComponent>
336341
component?: never
342+
children?: never
343+
redirect?: never
337344

338345
/**
339346
* Allow passing down params as props to the component rendered by
@@ -352,6 +359,7 @@ export interface RouteRecordMultipleViewsWithChildren extends _RouteRecordBase {
352359
*/
353360
components?: Record<string, RawRouteComponent> | null | undefined
354361
component?: never
362+
redirect?: never
355363

356364
children: RouteRecordRaw[]
357365

packages/router/test-dts/perfNamedRoutes.test-d.ts

Lines changed: 0 additions & 182 deletions
This file was deleted.

packages/router/test-dts/routeRecords.test-d.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,29 @@ routes.push({ path: '/', redirect: '/foo' })
1111
// @ts-expect-error cannot have components and component at the same time
1212
routes.push({ path: '/', components, component })
1313

14+
// a redirect record with children to point to a child
1415
routes.push({
1516
path: '/',
1617
redirect: '/foo',
17-
children: [],
18+
children: [
19+
{
20+
path: 'foo',
21+
component,
22+
},
23+
],
24+
})
25+
26+
// same but with a nested route
27+
routes.push({
28+
path: '/',
29+
component,
30+
redirect: '/foo',
31+
children: [
32+
{
33+
path: 'foo',
34+
component,
35+
},
36+
],
1837
})
1938

2039
routes.push({ path: '/', component, props: true })
@@ -29,3 +48,14 @@ routes.push({ path: '/', components, props: true })
2948
// component,
3049
// components,
3150
// }
51+
52+
export function filterNestedChildren(children: RouteRecordRaw[]) {
53+
return children.filter(r => {
54+
if (r.redirect) {
55+
r.children?.map(() => {})
56+
}
57+
if (r.children) {
58+
r.children = filterNestedChildren(r.children)
59+
}
60+
})
61+
}

0 commit comments

Comments
 (0)