You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 22:53:14