本文介绍了在 Next.js 项目构建时遇到的 searchParams 类型不兼容错误的解决方案,该错误通常出现在 Next.js 14+ 版本中,需要将 searchParams 修改为 Promise 类型并以异步方式处理。
nextjs项目在 npm run build的时候报错了,报错信息如下 Type error: Type ‘{ searchParams: { paymentIntentId?: string | undefined; orderId?: string | undefined; }; }’ does not satisfy the constraint ‘PageProps’. Types of property ‘searchParams’ are incompatible. Type ‘{ paymentIntentId?: string | undefined; orderId?: string | undefined; }’ is missing the following properties from type ‘Promise
nextjs项目在 npm run build的时候报错了,报错信息如下 Type error: Type ‘{ searchParams: { paymentIntentId?: string | undefined; orderId?: string | undefined; }; }’ does not satisfy the constraint ‘PageProps’. Types of property ‘searchParams’ are incompatible. Type ‘{ paymentIntentId?: string | undefined; orderId?: string | undefined; }’ is missing the following properties from type ‘Promise
这个错误是因为 Next.js 14+ 对页面的 searchParams 类型定义发生了变化,searchParams现在应该是一个 Promise
类型。你需要修改你的页面组件来适应这个变化。
以下是修改后的 thank-you/page.tsx
代码:
1 | import { Text, Title, Card, SimpleGrid, Box, Group } from "@mantine/core"; |
主要变化:
- 将组件标记为
async
searchParams
类型改为Promise<{...}>
- 在组件内部先
await searchParams
获取参数 - 组件返回类型改为
Promise<ReactElement>
如果你的 Next.js 版本低于 14,你也可以考虑升级到最新版本,或者检查你的 next.d.ts
类型定义文件是否有正确的 PageProps
类型定义。
另外,如果你使用的是 Next.js 14 之前的版本,可能需要在 next.config.js
中添加以下配置:
1 | /** @type {import('next').NextConfig} */ |