I'm encountering an issue when trying to use asynchronous parameters in a Next.js 15 app. I want to extract the slug parameter from params, which is returned as a Promise.
Here's my current setup in page.tsx:
type tParams = Promise<{ slug: string[] }>;
export default async function Challenge({ params }: { params: tParams }) {
const { slug } = await params;
const productID = slug[1];
// my code here
}
When I run npm run build, I get this error:
Type error: Type '{ params: { id: string; }; }' does not satisfy the constraint 'PageProps'. Types of property 'params' are incompatible. Type '{ id: string; }' is missing the following properties from type 'Promise': then, catch, finally, [Symbol.toStringTag]
It seems like the type for params is causing issues, possibly because Next.js expects a different structure for params.
What I’ve Tried
- I initially defined
paramsas{ slug: string[] }, but I changed it toPromise<{ slug: string[] }>to try to handle the async behavior. - I also tried different configurations for the
paramstype without success.
Question
How can I correctly type the params object for asynchronous access in Next.js 15, or is there a better approach for handling this?