错误:路由 "[param]" 具有无效的参数类型。参数必须是字符串或字符串数组,但收到了数字。
Error: Route "[param]" has invalid parameter type. Parameters must be strings or arrays of strings, but received number.
ID: nextjs/invalid-app-path-param-type
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| Next.js 13.x | active | — | — | — |
| Next.js 14.x | active | — | — | — |
| Next.js 15.x | active | — | — | — |
根因分析
在动态路由(例如 app/[slug]/page.tsx)中,generateStaticParams 函数或来自路由的 params 对象为参数返回了非字符串值(例如数字),而 Next.js 期望该参数是字符串。
English
In dynamic routes (e.g., app/[slug]/page.tsx), the generateStaticParams function or the params object from the route returns a non-string value (e.g., number) for a parameter that Next.js expects to be a string.
官方文档
https://nextjs.org/docs/app/api-reference/functions/generate-static-params解决方案
-
Ensure generateStaticParams returns params as strings. Use String() or template literals to convert numbers.
-
Add TypeScript validation to ensure params are strings. Use a type guard or runtime check in the page component.
无效尝试
常见但无效的做法:
-
90% 失败
Next.js requires params to be strings for URL construction; numbers cause type errors.
-
60% 失败
TypeScript and runtime validation will throw an error if the value is not a string or array of strings.
-
80% 失败
The build will fail (if using static generation) or the route will not match correctly at runtime.