The types to use
export type InferGSPRTWorkup<T> =
T extends Promise<readonly (infer U)[] | (infer U)[]> ? U : T;
export type InferGSPRT<T extends (...args: any) => any> = {
params: Promise<InferGSPRTWorkup<ReturnType<T>>>;
};
InferGSPRTWorkup<T>
export type InferGSPRTWorkup<T> =
T extends Promise<readonly (infer U)[] | (infer U)[]> ? U : T;
InferGSPRT<T>
export type InferGSPRT<T extends (...args: any) => any> = {
params: Promise<InferGSPRTWorkup<ReturnType<T>>>;
};
Usage
export default async function Example({ params }: InferGSPRT<typeof generateStaticParams>) {
const { slug } = await params;
// rest...
};
Summary
Copy-paste the two types defined initially into a file in your code base. Use InferGSPRT
within the page file of dynamic routes to infer the params object returned by generateStaticParams
effortlessly.
Nextjs versions 13 & 14
Remove the outer Promise in the InferGSPRT
type definition. The params object is synchronously resolved by the exported default function prior to version 15.
Top comments (0)