Skip to content

Commit 05d7667

Browse files
authored
release: typegen fixes (#12443)
* typegen current route info into `matches` * child matches may be undefined
1 parent 1007cc7 commit 05d7667

File tree

3 files changed

+24
-8
lines changed

3 files changed

+24
-8
lines changed

.changeset/red-eagles-stare.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
"react-router": patch
44
---
55

6-
Generate wide `matches` and `params` types for child routes
6+
Generate wide `matches` and `params` types for current route and child routes
77

88
At runtime, `matches` includes child route matches and `params` include child route path parameters.
9-
But previously, we only generated types for parent routes and the current route in `matches` and `params`.
9+
But previously, we only generated types for parent routes in `matches`; for `params`, we only considered the parent routes and the current route.
1010
To align our generated types more closely to the runtime behavior, we now generate more permissive, wider types when accessing child route information.

integration/typegen-test.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,15 +282,25 @@ test.describe("typegen", () => {
282282
import { Expect, Equal } from "../expect-type"
283283
import type { Route } from "./+types/current"
284284
285+
export function loader() {
286+
return { current: 3 }
287+
}
288+
285289
export function meta({ matches }: Route.MetaArgs) {
286290
const parent1 = matches[1]
287291
type Test1 = Expect<Equal<typeof parent1.data, { parent1: number }>>
288292
289293
const parent2 = matches[2]
290294
type Test2 = Expect<Equal<typeof parent2.data, { parent2: number }>>
291295
296+
const current = matches[3]
297+
type Test3 = Expect<Equal<typeof current.data, { current: number }>>
298+
292299
const child1 = matches[4]
293-
type Test3 = Expect<Equal<typeof child1.data, unknown>>
300+
type Test4a = Expect<undefined extends typeof child1 ? true : false>
301+
if (child1) {
302+
type Test4b = Expect<Equal<typeof child1.data, unknown>>
303+
}
294304
return []
295305
}
296306
@@ -301,8 +311,14 @@ test.describe("typegen", () => {
301311
const parent2 = matches[2]
302312
type Test2 = Expect<Equal<typeof parent2.data, { parent2: number }>>
303313
314+
const current = matches[3]
315+
type Test3 = Expect<Equal<typeof current.data, { current: number }>>
316+
304317
const child1 = matches[4]
305-
type Test3 = Expect<Equal<typeof child1.data, unknown>>
318+
type Test4a = Expect<undefined extends typeof child1 ? true : false>
319+
if (child1) {
320+
type Test4b = Expect<Equal<typeof child1.data, unknown>>
321+
}
306322
}
307323
`,
308324
});

packages/react-router/lib/types/route-module.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,14 @@ type MetaMatch<T extends RouteInfo> = Pretty<
4848
type MetaMatches<T extends RouteInfo[]> =
4949
T extends [infer F extends RouteInfo, ...infer R extends RouteInfo[]]
5050
? [MetaMatch<F>, ...MetaMatches<R>]
51-
: MetaMatch<RouteInfo>[];
51+
: Array<MetaMatch<RouteInfo> | undefined>;
5252

5353
export type CreateMetaArgs<T extends RouteInfo> = {
5454
location: Location;
5555
params: T["params"];
5656
data: T["loaderData"];
5757
error?: unknown;
58-
matches: MetaMatches<T["parents"]>;
58+
matches: MetaMatches<[...T["parents"], T]>;
5959
};
6060
export type MetaDescriptors = MetaDescriptor[];
6161

@@ -149,13 +149,13 @@ type Match<T extends RouteInfo> = Pretty<
149149
type Matches<T extends RouteInfo[]> =
150150
T extends [infer F extends RouteInfo, ...infer R extends RouteInfo[]]
151151
? [Match<F>, ...Matches<R>]
152-
: Match<RouteInfo>[];
152+
: Array<Match<RouteInfo> | undefined>;
153153

154154
export type CreateComponentProps<T extends RouteInfo> = {
155155
params: T["params"];
156156
loaderData: T["loaderData"];
157157
actionData?: T["actionData"];
158-
matches: Matches<T["parents"]>;
158+
matches: Matches<[...T["parents"], T]>;
159159
};
160160

161161
export type CreateErrorBoundaryProps<T extends RouteInfo> = {

0 commit comments

Comments
 (0)