Skip to content

Commit

Permalink
typegen current route info into matches
Browse files Browse the repository at this point in the history
  • Loading branch information
pcattori committed Dec 2, 2024
1 parent 2478d27 commit 748e9eb
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
4 changes: 2 additions & 2 deletions .changeset/red-eagles-stare.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"react-router": patch
---

Generate wide `matches` and `params` types for child routes
Generate wide `matches` and `params` types for current route and 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`.
But previously, we only generated types for parent routes in `matches`; for `params`, we only considered the parent routes and the current route.
To align our generated types more closely to the runtime behavior, we now generate more permissive, wider types when accessing child route information.
14 changes: 12 additions & 2 deletions integration/typegen-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,15 +282,22 @@ test.describe("typegen", () => {
import { Expect, Equal } from "../expect-type"
import type { Route } from "./+types/current"
export function loader() {
return { current: 3 }
}
export function meta({ matches }: Route.MetaArgs) {
const parent1 = matches[1]
type Test1 = Expect<Equal<typeof parent1.data, { parent1: number }>>
const parent2 = matches[2]
type Test2 = Expect<Equal<typeof parent2.data, { parent2: number }>>
const current = matches[3]
type Test3 = Expect<Equal<typeof current.data, { current: number }>>
const child1 = matches[4]
type Test3 = Expect<Equal<typeof child1.data, unknown>>
type Test4 = Expect<Equal<typeof child1.data, unknown>>
return []
}
Expand All @@ -301,8 +308,11 @@ test.describe("typegen", () => {
const parent2 = matches[2]
type Test2 = Expect<Equal<typeof parent2.data, { parent2: number }>>
const current = matches[3]
type Test3 = Expect<Equal<typeof current.data, { current: number }>>
const child1 = matches[4]
type Test3 = Expect<Equal<typeof child1.data, unknown>>
type Test4 = Expect<Equal<typeof child1.data, unknown>>
}
`,
});
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 @@ -55,7 +55,7 @@ export type CreateMetaArgs<T extends RouteInfo> = {
params: T["params"];
data: T["loaderData"];
error?: unknown;
matches: MetaMatches<T["parents"]>;
matches: MetaMatches<[...T["parents"], T]>;
};
export type MetaDescriptors = MetaDescriptor[];

Expand Down Expand Up @@ -155,7 +155,7 @@ export type CreateComponentProps<T extends RouteInfo> = {
params: T["params"];
loaderData: T["loaderData"];
actionData?: T["actionData"];
matches: Matches<T["parents"]>;
matches: Matches<[...T["parents"], T]>;
};

export type CreateErrorBoundaryProps<T extends RouteInfo> = {
Expand Down

0 comments on commit 748e9eb

Please sign in to comment.