Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(types): allow nonpromise return types for static functions #24685

Merged
merged 6 commits into from
May 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
}
})
})
})