Skip to content

Commit 294f4a0

Browse files
committed
fix : intersection to variable
1 parent adca381 commit 294f4a0

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

packages/react-router/tests/link.test-d.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2162,6 +2162,20 @@ test('when navigating from a route with params to a route with an additional par
21622162
}>()
21632163
})
21642164

2165+
test('when resolving parent-relative to values from a union of routes', () => {
2166+
const DefaultRouterLink = Link<
2167+
DefaultRouter,
2168+
'/invoices/$invoiceId/details' | '/posts/$postId/preview',
2169+
'../'
2170+
>
2171+
2172+
expectTypeOf(DefaultRouterLink)
2173+
.parameter(0)
2174+
.toHaveProperty('to')
2175+
.toEqualTypeOf<'..' | '../..' | '../edit' | undefined>()
2176+
2177+
})
2178+
21652179
test('when navigating to a union of routes with params', () => {
21662180
const DefaultRouterLink = Link<
21672181
DefaultRouter,

packages/router-core/src/link.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ import type {
2323
MakeDifferenceOptional,
2424
NoInfer,
2525
NonNullableUpdater,
26+
ToObject,
27+
UnObject,
28+
UnionToIntersection,
2629
Updater,
2730
} from './utils'
2831
import type { ParsedLocation } from './location'
@@ -229,13 +232,27 @@ export type RelativeToPath<
229232
: ToPath<TRouter, TTo>)
230233
| `${RemoveTrailingSlashes<TTo>}/${InferDescendantToPaths<TRouter, RemoveTrailingSlashes<TResolvedPath>>}`
231234

235+
236+
type WrapRelativeToPathForIntersection<
237+
TRouter extends AnyRouter,
238+
TTo extends string,
239+
TResolvedPath extends string,
240+
> = TResolvedPath extends any
241+
? ToObject<RelativeToPath<TRouter, TTo, TResolvedPath>>
242+
: never
243+
232244
export type RelativeToParentPath<
233245
TRouter extends AnyRouter,
234246
TFrom extends string,
235247
TTo extends string,
236248
TResolvedPath extends string = ResolveRelativePath<TFrom, TTo>,
237249
> =
238-
| RelativeToPath<TRouter, TTo, TResolvedPath>
250+
| UnObject<
251+
UnionToIntersection<
252+
WrapRelativeToPathForIntersection<TRouter, TTo, TResolvedPath>
253+
>
254+
>
255+
// RelativeToPath<TRouter, TTo, TResolvedPath>
239256
| (TTo extends `${string}..` | `${string}../`
240257
? TResolvedPath extends '/' | ''
241258
? never

packages/router-core/src/utils.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,9 @@ export type UnionToIntersection<T> = (
145145
? T
146146
: never
147147

148+
export type UnObject<T> = T extends { foo: any } ? T["foo"] : never
149+
export type ToObject<T> = { foo: T }
150+
148151
/**
149152
* Merges everything in a union into one object.
150153
* This mapped type is homomorphic which means it preserves stuff! :)

0 commit comments

Comments
 (0)