Skip to content

Commit

Permalink
typegen: matches and params for child routes (#12397)
Browse files Browse the repository at this point in the history
  • Loading branch information
pcattori authored Dec 2, 2024
1 parent 5ce8667 commit 180a120
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
10 changes: 10 additions & 0 deletions .changeset/red-eagles-stare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
"@react-router/dev": patch
"react-router": patch
---

Generate wide `matches` and `params` types for child routes

At runtime, `matches` includes child route matches and `params` include child route path parameters.
But previously, we only generated types for parent routes and the current route in `matches` and `params`.
To align our generated types more closely to the runtime behavior, we now generate more permissive, wider types when accessing child route information.
9 changes: 8 additions & 1 deletion integration/typegen-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ test.describe("typegen", () => {
import type { Route } from "./+types/product"
export function loader({ params }: Route.LoaderArgs) {
type Test = Expect<Equal<typeof params.id, string>>
type Test1 = Expect<Equal<typeof params.id, string>>
type Test2 = Expect<Equal<typeof params.asdf, string | undefined>>
return { planet: "world" }
}
Expand Down Expand Up @@ -287,6 +288,9 @@ test.describe("typegen", () => {
const parent2 = matches[2]
type Test2 = Expect<Equal<typeof parent2.data, { parent2: number }>>
const child1 = matches[4]
type Test3 = Expect<Equal<typeof child1.data, unknown>>
return []
}
Expand All @@ -296,6 +300,9 @@ test.describe("typegen", () => {
const parent2 = matches[2]
type Test2 = Expect<Equal<typeof parent2.data, { parent2: number }>>
const child1 = matches[4]
type Test3 = Expect<Equal<typeof child1.data, unknown>>
}
`,
});
Expand Down
4 changes: 3 additions & 1 deletion packages/react-router-dev/typegen/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ export function generate(ctx: Context, route: RouteManifestEntry): string {
id: "${route.id}"
file: "${route.file}"
path: "${route.path}"
params: {${formatParamProperties(urlpath)}}
params: {${formatParamProperties(
urlpath
)}} & { [key: string]: string | undefined }
module: Module
loaderData: T.CreateLoaderData<Module>
actionData: T.CreateActionData<Module>
Expand Down
4 changes: 2 additions & 2 deletions packages/react-router/lib/types/route-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ type MetaMatch<T extends RouteInfo> = Pretty<
type MetaMatches<T extends RouteInfo[]> =
T extends [infer F extends RouteInfo, ...infer R extends RouteInfo[]]
? [MetaMatch<F>, ...MetaMatches<R>]
: [];
: MetaMatch<RouteInfo>[];

export type CreateMetaArgs<T extends RouteInfo> = {
location: Location;
Expand Down Expand Up @@ -149,7 +149,7 @@ type Match<T extends RouteInfo> = Pretty<
type Matches<T extends RouteInfo[]> =
T extends [infer F extends RouteInfo, ...infer R extends RouteInfo[]]
? [Match<F>, ...Matches<R>]
: [];
: Match<RouteInfo>[];

export type CreateComponentProps<T extends RouteInfo> = {
params: T["params"];
Expand Down

0 comments on commit 180a120

Please sign in to comment.