I’m working on a Next.js 15 project using the App Router (/app/api/) and trying to implement a dynamic API route (/api/agencies/[id]) to fetch a specific agency from MongoDB.
However, during npm run build, I get this TypeScript error:
src/app/api/agencies/[id]/route.ts
Type error: Route "src/app/api/agencies/[id]/route.ts" has an invalid "GET" export:
Type "{ params: { id: string; }; }" is not a valid type for the function's second argument.
What I Have Tried So Far:
- Modified the function signature for GET, PATCH, and DELETE requests:
export async function GET(req: NextRequest, context: { params: { id: string } }) {
❌ Still getting the “invalid GET export” error.
- Changed params type to Record<string, string>:
export async function GET(req: NextRequest, context: { params: Record<string, string> }) {
❌ No luck.
- Tried wrapping params in an optional object:
export async function GET(req: NextRequest, context: { params?: { id?: string } }) {
❌ Still getting the same error.
- Checked TypeScript compatibility in tsconfig.json:
"target": "esnext",
"module": "esnext",
"strict": true,
• No improvements.
- Tried awaiting params before using it:
const { id } = await context.params;
❌ Still doesn’t work.