Home > Software design >  How to Correctly Type Next.js getStaticPaths as a Function Declaration
How to Correctly Type Next.js getStaticPaths as a Function Declaration

Time:01-07

I'm working with typing getStaticPaths (guide docs, API reference docs) but all of the documentation that I can find relates to declaring it as a function expression. I'm looking to code it as a function declaration but can't find any documentation on it and am having trouble getting it to work.

As a function expression (this works):

import { GetStaticPaths } from 'next';

export const getStaticPaths: GetStaticPaths = async () => {
  return {
    paths: [{ params: { id: 'example' } }],
    fallback: false,
  }
}

How can I properly use a function declaration for getStaticPaths and get the typing right?

CodePudding user response:

This is achieved by making use of the GetStaticPathsResult type from next.

import {GetStaticPathsResult} from 'next';

export async function getStaticPaths(): Promise<GetStaticPathsResult> {
  return {
    paths: [{ params: { id: 'example' } }],
    fallback: false,
  }
}

CodePudding user response:

import type { GetStaticPathsContext, GetStaticPathsResult } from 'next';

type MyProps = {
res: any 
}

export async function getStaticPaths(context: GetStaticPathsContext): 
Promise<GetStaticPathsResult<MyProps>>  {
return {
    paths: {res}
};
}
  • Related