Next.js Discord

Discord Forum

Problem with generateStaticParams

Unanswered
Capo posted this in #help-forum
Open in Discord
Hello,

I'm trying to use generateStaticParams on my recipe pages, this is App Router.

export async function generateStaticParams() {
    const { data: recipes, error: recipesError } = await supabaseAdmin
        .from('recipes')
        .select('id');

    if (recipesError) {
        console.log(recipesError?.message);
    }

    return recipes?.map((data) => ({
        id: data.id,
    }));
}


Error
Type error: Type '{ __tag__: "generateStaticParams"; __return_type__: Promise<{ id: any; }[] | undefined>; }' does not satisfy the constraint '{ __tag__: "generateStaticParams"; __return_type__: any[] | Promise<any[]>; }'.
  Types of property '__return_type__' are incompatible.
    Type 'Promise<{ id: any; }[] | undefined>' is not assignable to type 'any[] | Promise<any[]>'.
      Type 'Promise<{ id: any; }[] | undefined>' is not assignable to type 'Promise<any[]>'.
        Type '{ id: any; }[] | undefined' is not assignable to type 'any[]'.
          Type 'undefined' is not assignable to type 'any[]'.



This completely works fine in development, upon running npm run build these are the errors that I get. I'm blatantly not sure what the problem is I double checked documentation and couldn't find where I'm going wrong. Any advice would be helpful.

2 Replies

i was able to bypass the build error with
return recipes?.map((data) => ({
        id: data.id,
    })) as any;


adding "as any" for the type

but what's the actual problem with the types?
4 month old thread so I won’t ping but the problem is that if recipes is nullish (which is possible from the TS type), the returned value will be undefined which is not allowed. Need to handle the nullish case separately like
if (!recipes)
  throw new Error("invariant: recipes is nullish")