Skip to content
This repository has been archived by the owner on Jun 21, 2023. It is now read-only.

Commit

Permalink
fix(types): allow nonpromise return types for static functions (verce…
Browse files Browse the repository at this point in the history
…l#24685)

## Bug

- [x] fixes vercel#24684
- [x] Integration tests added

Intentionally omitted changing the types for `GetServerSideProps` etc. as its imo less reasonable to leverage SSR with a sync `getServerSideProps`. Can of course change the type too if you consider that also a valid case.
  • Loading branch information
ljosberinn authored May 25, 2021
1 parent 37a880a commit b054f3b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
8 changes: 5 additions & 3 deletions packages/next/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,15 @@ export type GetStaticPropsResult<P> =
export type GetStaticProps<
P extends { [key: string]: any } = { [key: string]: any },
Q extends ParsedUrlQuery = ParsedUrlQuery
> = (context: GetStaticPropsContext<Q>) => Promise<GetStaticPropsResult<P>>
> = (
context: GetStaticPropsContext<Q>
) => Promise<GetStaticPropsResult<P>> | GetStaticPropsResult<P>

export type InferGetStaticPropsType<T> = T extends GetStaticProps<infer P, any>
? P
: T extends (
context?: GetStaticPropsContext<any>
) => Promise<GetStaticPropsResult<infer P>>
) => Promise<GetStaticPropsResult<infer P>> | GetStaticPropsResult<infer P>
? P
: never

Expand All @@ -131,7 +133,7 @@ export type GetStaticPathsResult<P extends ParsedUrlQuery = ParsedUrlQuery> = {

export type GetStaticPaths<P extends ParsedUrlQuery = ParsedUrlQuery> = (
context: GetStaticPathsContext
) => Promise<GetStaticPathsResult<P>>
) => Promise<GetStaticPathsResult<P>> | GetStaticPathsResult<P>

export type GetServerSidePropsContext<
Q extends ParsedUrlQuery = ParsedUrlQuery
Expand Down
11 changes: 11 additions & 0 deletions test/integration/typescript/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,5 +111,16 @@ export default function EvilPage(): JSX.Element {
errorPage.restore()
}
})

it('should compile sync getStaticPaths & getStaticProps', async () => {
const page = new File(join(appDir, 'pages/ssg/[slug].tsx'))
try {
page.replace(/async \(/g, '(')
const output = await nextBuild(appDir, [], { stdout: true })
expect(output.stdout).toMatch(/Compiled successfully/)
} finally {
page.restore()
}
})
})
})

0 comments on commit b054f3b

Please sign in to comment.