I want to add a type for my dynamic route that I can replace with type "any" in code bellow written with Typescript .
export default async function DetailProduct({ params }: any) {
const { imageAddress, title, price } = await getProductByID(params.id);
return ...
}
Does nextjs have a type for it ? I use nextjs14
2
Answers
NextJS does not have a type for it. But hey, you can define the type according to your usage of keys right. If you are simply using the string
id
from params, just define the types as below:Example from docs
You define your own types either at the call or you can create an definition
You don’t need the await part you can just extract the id from the param object.
Source https://raynoppe.com/snippets/nextjs-app-router/app-structure-in-next-js/page-component/