Skip to content

Commit d11ceff

Browse files
authored
typegen: generate params type for splat params (#12218)
1 parent 4957d16 commit d11ceff

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

integration/typegen-test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,24 @@ test.describe("typegen", () => {
111111
expect(proc.stderr.toString()).toBe("");
112112
expect(proc.status).toBe(0);
113113
});
114+
115+
test("splat", async () => {
116+
const cwd = await createProject({
117+
"vite.config.ts": viteConfig,
118+
"app/routes/splat.$.tsx": tsx`
119+
import type { Route } from "./+types.splat.$"
120+
121+
function assertType<T>(t: T) {}
122+
123+
export function loader({ params }: Route.LoaderArgs) {
124+
assertType<string>(params["*"])
125+
return null
126+
}
127+
`,
128+
});
129+
const proc = typecheck(cwd);
130+
expect(proc.stdout.toString()).toBe("");
131+
expect(proc.stderr.toString()).toBe("");
132+
expect(proc.status).toBe(0);
133+
});
114134
});

packages/react-router-dev/typegen.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,12 +182,12 @@ function formattedParamsProperties(
182182
const properties = Object.entries(params).map(([name, values]) => {
183183
if (values.length === 1) {
184184
const isOptional = values[0];
185-
return indent + (isOptional ? `${name}?: string` : `${name}: string`);
185+
return indent + (isOptional ? `"${name}"?: string` : `"${name}": string`);
186186
}
187187
const items = values.map((isOptional) =>
188188
isOptional ? "string | undefined" : "string"
189189
);
190-
return indent + `${name}: [${items.join(", ")}]`;
190+
return indent + `"${name}": [${items.join(", ")}]`;
191191
});
192192

193193
// prettier-ignore
@@ -226,5 +226,8 @@ function parseParams(urlpath: string) {
226226
result[param].push(isOptional);
227227
return;
228228
});
229+
230+
const hasSplat = segments.at(-1) === "*";
231+
if (hasSplat) result["*"] = [false];
229232
return result;
230233
}

0 commit comments

Comments
 (0)