Recently, I encountered an issue with my API route handler:
import { NextRequest, NextResponse } from "next/server";
import dbConnect from "@/lib/dbConnect";
import User from "@/models/User";
interface ErrorMessage {
message: string;
}
export async function GET(req: NextRequest, { params }: { params: { id: string } }) {
await dbConnect();
const { id } = params;
try {
const user = await User.findById(id)
return NextResponse.json({
success: true,
data: user,
}, {
status: 200,
})
} catch (error: ErrorMessage) {
return NextResponse.json({
success: false,
message: error.message
}, {
status: 404,
})
}
}
Upon encountering the TypeScript error
Catch clause variable type annotation must be 'any' or 'unknown' if specified.ts(1196)
, I realized that I still have a lot to learn about TypeScript best practices. Here's the screenshot of the error:
https://i.stack.imgur.com/Nsvqr.png
This occurred while working with Next.js version 13.4.16 and as a newcomer to TypeScript, I'm eager to improve my skills in dealing with such errors.