Skip to content

Commit

Permalink
Generic Type for GetStaticPaths (#11430)
Browse files Browse the repository at this point in the history
* Generic Type for GetStaticPaths

* Add generic typing for params to GSSP and GSP too

* Added basic tests

Co-authored-by: Luis Alvarez <[email protected]>
  • Loading branch information
wongmjane and Luis Alvarez committed Mar 31, 2020
1 parent 2263876 commit c17ee73
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 6 deletions.
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>
}

0 comments on commit c17ee73

Please sign in to comment.