nextjs type_error ai_generated true

错误:路由 "[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

其他格式: JSON · Markdown 中文 · English
90%修复率
86%置信度
1证据数
2023-09-05首次发现

版本兼容性

版本状态引入弃用备注
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.

generic

官方文档

https://nextjs.org/docs/app/api-reference/functions/generate-static-params

解决方案

  1. Ensure generateStaticParams returns params as strings. Use String() or template literals to convert numbers.
  2. Add TypeScript validation to ensure params are strings. Use a type guard or runtime check in the page component.

无效尝试

常见但无效的做法:

  1. 90% 失败

    Next.js requires params to be strings for URL construction; numbers cause type errors.

  2. 60% 失败

    TypeScript and runtime validation will throw an error if the value is not a string or array of strings.

  3. 80% 失败

    The build will fail (if using static generation) or the route will not match correctly at runtime.