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

Generic Type for GetStaticPaths #11430

Merged
merged 5 commits into from
Mar 31, 2020
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
16 changes: 10 additions & 6 deletions packages/next/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,27 +66,31 @@ export {
}

export type GetStaticProps<
P extends { [key: string]: any } = { [key: string]: any }
P extends { [key: string]: any } = { [key: string]: any },
Q extends ParsedUrlQuery = ParsedUrlQuery
> = (ctx: {
params?: ParsedUrlQuery
params?: Q
preview?: boolean
previewData?: any
}) => Promise<{
props: P
revalidate?: number | boolean
}>

export type GetStaticPaths = () => Promise<{
paths: Array<string | { params: ParsedUrlQuery }>
export type GetStaticPaths<
P extends ParsedUrlQuery = ParsedUrlQuery
> = () => Promise<{
paths: Array<string | { params: P }>
fallback: boolean
}>

export type GetServerSideProps<
P extends { [key: string]: any } = { [key: string]: any }
P extends { [key: string]: any } = { [key: string]: any },
Q extends ParsedUrlQuery = ParsedUrlQuery
> = (context: {
req: IncomingMessage
res: ServerResponse
params?: ParsedUrlQuery
params?: Q
query: ParsedUrlQuery
preview?: boolean
previewData?: any
Expand Down
29 changes: 29 additions & 0 deletions test/integration/typescript/pages/ssg/[slug].tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { GetStaticPaths, GetStaticProps } from 'next'

type Params = {
slug: string
}

type Props = {
data: string
}

export const getStaticPaths: GetStaticPaths<Params> = async () => {
return {
paths: [{ params: { slug: 'test' } }],
fallback: false,
}
}

export const getStaticProps: GetStaticProps<Props, Params> = async ({
params,
}) => {
return {
props: { data: params!.slug },
revalidate: false,
}
}

export default function Page({ data }: Props) {
return <h1>{data}</h1>
}
21 changes: 21 additions & 0 deletions test/integration/typescript/pages/ssr/[slug].tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { GetServerSideProps } from 'next'

type Params = {
slug: string
}

type Props = {
data: string
}

export const getServerSideProps: GetServerSideProps<Props, Params> = async ({
params,
}) => {
return {
props: { data: params!.slug },
}
}

export default function Page({ data }: Props) {
return <h1>{data}</h1>
}