Next.js+TypeScript构建报错:'param_type.params'与'ParamCheck<RouteContext>'不兼容
Next.js 15 + TypeScript + Prisma 构建失败:类型不匹配错误
错误信息
执行npm run build时触发TypeScript类型错误:
Type error: Type '{ __tag__: "GET"; __param_position__: "second"; __param_type__: { params: { id: string; }; }; }' does not satisfy the constraint 'ParamCheck<RouteContext>'. The types of '__param_type__.params' are incompatible between these types. Type '{ id: string; }' is missing the following properties from type 'Promise<any>': then, catch, finally, [Symbol.toStringTag]
错误上下文
问题出在app/api/projects/[id]/route.ts文件,相关代码如下:
import { NextResponse } from "next/server"; import { prisma } from "@/lib/prisma"; export async function GET( request: Request, { params }: { params: { id: string } } ) { try { const project = await prisma.project.findUnique({ where: { id: params.id }, }); if (!project) { return NextResponse.json({ error: "Project not found" }, { status: 404 }); } return NextResponse.json(project); } catch (error) { console.error("Error fetching project:", error); return NextResponse.json({ error: "Failed to fetch project" }, { status: 500 }); } }
已尝试的操作
- 检查
tsconfig.json配置(已启用严格模式):{ "compilerOptions": { "strict": true, "moduleResolution": "node", "skipLibCheck": true } } - 重新安装依赖并生成Prisma客户端:
rm -rf node_modules package-lock.json npm install npx prisma generate
项目版本
- Next.js: 15.1.2
- Prisma: 6.2.1
- TypeScript: 5.x
解决方法
1. 修正tsconfig.json模块解析策略
Next.js 15推荐使用bundler模块解析,替换原有的node配置:
{ "compilerOptions": { "strict": true, "moduleResolution": "bundler", "skipLibCheck": true, "target": "ES2022", "module": "ESNext", "lib": ["ES2022", "DOM", "DOM.Iterable"] } }
2. 使用Next.js官方路由参数类型
显式导入Params类型,避免类型推断错误:
import { NextResponse, type Params } from "next/server"; import { prisma } from "@/lib/prisma"; export async function GET( request: Request, { params }: { params: Params<{ id: string }> } ) { // 原有业务逻辑不变 }
3. 升级Prisma至兼容版本
Next.js 15属于较新版本,尝试升级Prisma到最新稳定版以确保兼容性:
npm install prisma@latest npx prisma generate
4. 强制重新生成Prisma类型
清除类型缓存,强制重新生成:
npx prisma generate --force
内容的提问来源于stack exchange,提问作者K khan
相关产品推荐
相关产品推荐

