Skip to content

Commit a86f1f3

Browse files
committed
fix(experimental): resolve TS errors in resolver/router type hierarchy
Widen parent/aliasOf in ResolverRecord_Base from the narrow union type to the base interface, allowing router types to narrow them via standard interface extension instead of incompatible overrides.
1 parent 476c669 commit a86f1f3

File tree

3 files changed

+19
-23
lines changed

3 files changed

+19
-23
lines changed

packages/router/src/experimental/route-resolver/resolver-fixed.spec.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { describe, expect, it } from 'vitest'
2-
import { createFixedResolver } from './resolver-fixed'
2+
import {
3+
createFixedResolver,
4+
type EXPERIMENTAL_ResolverRecord_Matchable,
5+
} from './resolver-fixed'
36
import { NO_MATCH_LOCATION } from './resolver-abstract'
47
import {
58
EmptyParams,
@@ -1288,7 +1291,7 @@ describe('fixed resolver', () => {
12881291
})
12891292

12901293
it('original path still resolves normally', () => {
1291-
const original = {
1294+
const original: EXPERIMENTAL_ResolverRecord_Matchable = {
12921295
name: 'users',
12931296
path: new MatcherPatternPathStatic('/users'),
12941297
}
@@ -1305,7 +1308,7 @@ describe('fixed resolver', () => {
13051308
})
13061309

13071310
it('matched array entry has aliasOf pointing to original', () => {
1308-
const original = {
1311+
const original: EXPERIMENTAL_ResolverRecord_Matchable = {
13091312
name: 'users',
13101313
path: new MatcherPatternPathStatic('/users'),
13111314
}
@@ -1366,11 +1369,11 @@ describe('fixed resolver', () => {
13661369
})
13671370

13681371
it('child alias resolves with parent in matched', () => {
1369-
const parent = {
1372+
const parent: EXPERIMENTAL_ResolverRecord_Matchable = {
13701373
name: 'parent',
13711374
path: new MatcherPatternPathStatic('/parent'),
13721375
}
1373-
const child = {
1376+
const child: EXPERIMENTAL_ResolverRecord_Matchable = {
13741377
name: 'child',
13751378
path: new MatcherPatternPathStatic('/child'),
13761379
parent,

packages/router/src/experimental/route-resolver/resolver-fixed.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,14 @@ export interface EXPERIMENTAL_ResolverRecord_Base {
5454
* Parent record. The parent can be a group or a matchable record.
5555
* It will be included in the `matched` array of a resolved location.
5656
*/
57-
parent?: EXPERIMENTAL_ResolverRecord | null // the parent can be matchable or not
57+
parent?: EXPERIMENTAL_ResolverRecord_Base | null // the parent can be matchable or not
5858

5959
/**
6060
* If this record is an alias of another record, this points to the original
6161
* record. Alias records are not added to the name map and resolve to the
6262
* original record's name.
6363
*/
64-
aliasOf?: EXPERIMENTAL_ResolverRecord | null
64+
aliasOf?: EXPERIMENTAL_ResolverRecord_Base | null
6565
}
6666

6767
/**
@@ -115,7 +115,7 @@ export interface EXPERIMENTAL_ResolverFixed<
115115
/**
116116
* Build the `matched` array of a record that includes all parent records from the root to the current one.
117117
*/
118-
export function buildMatched<T extends EXPERIMENTAL_ResolverRecord>(
118+
export function buildMatched<T extends EXPERIMENTAL_ResolverRecord_Base>(
119119
record: T
120120
): T[] {
121121
const matched: T[] = []

packages/router/src/experimental/router.ts

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -193,12 +193,6 @@ export interface EXPERIMENTAL_RouteRecord_Base extends EXPERIMENTAL_ResolverReco
193193
*/
194194
redirect?: RouteRecordRedirectOption
195195

196-
// TODO:
197-
/**
198-
* References another record if this record is an alias of it.
199-
*/
200-
aliasOf?: this
201-
202196
// TODO: deprecate, expose utils to compare resolved routes, and document
203197
// how to create a meta field that does the same
204198
/**
@@ -224,6 +218,11 @@ export interface EXPERIMENTAL_RouteRecord_Base extends EXPERIMENTAL_ResolverReco
224218
*/
225219
parent?: EXPERIMENTAL_RouteRecordNormalized | null
226220

221+
/**
222+
* References another record if this record is an alias of it.
223+
*/
224+
aliasOf?: EXPERIMENTAL_RouteRecordNormalized | null
225+
227226
// TODO:
228227
/**
229228
* Allow passing down params as props to the component rendered by `router-view`.
@@ -235,12 +234,10 @@ export interface EXPERIMENTAL_RouteRecord_Redirect
235234
// preserve the values from the type EXPERIMENTAL_ResolverRecord_Matchable
236235
extends
237236
Omit<EXPERIMENTAL_RouteRecord_Base, 'name' | 'path'>,
238-
EXPERIMENTAL_ResolverRecord_Matchable {
237+
Omit<EXPERIMENTAL_ResolverRecord_Matchable, 'parent' | 'aliasOf'> {
239238
components?: Record<string, RawRouteComponent>
240239

241240
redirect: RouteRecordRedirectOption // must be defined
242-
243-
parent?: EXPERIMENTAL_RouteRecordNormalized | null // must be redifined because of ResolverRecord_Matchable
244241
}
245242

246243
export interface EXPERIMENTAL_RouteRecord_Group
@@ -250,22 +247,18 @@ export interface EXPERIMENTAL_RouteRecord_Group
250247
// preserve the values from the type EXPERIMENTAL_ResolverRecord_Group
251248
'name' | 'path' | 'query' | 'hash'
252249
>,
253-
EXPERIMENTAL_ResolverRecord_Group {
250+
Omit<EXPERIMENTAL_ResolverRecord_Group, 'parent' | 'aliasOf'> {
254251
components?: Record<string, RawRouteComponent>
255-
256-
parent?: EXPERIMENTAL_RouteRecordNormalized | null // must be redifined because of ResolverRecord_Matchable
257252
}
258253

259254
export interface EXPERIMENTAL_RouteRecord_Components
260255
// preserve the values from the type EXPERIMENTAL_ResolverRecord_Matchable
261256
extends
262257
Omit<EXPERIMENTAL_RouteRecord_Base, 'name' | 'path'>,
263-
EXPERIMENTAL_ResolverRecord_Matchable {
258+
Omit<EXPERIMENTAL_ResolverRecord_Matchable, 'parent' | 'aliasOf'> {
264259
components: Record<string, RawRouteComponent>
265260

266261
redirect?: never
267-
268-
parent?: EXPERIMENTAL_RouteRecordNormalized | null // must be redifined because of ResolverRecord_Matchable
269262
}
270263

271264
export type EXPERIMENTAL_RouteRecord_Matchable =

0 commit comments

Comments
 (0)