Unverified Commit c17ee73d authored by Jane Manchun Wong's avatar Jane Manchun Wong Committed by GitHub

Generic Type for GetStaticPaths (#11430)

* Generic Type for GetStaticPaths

* Add generic typing for params to GSSP and GSP too

* Added basic tests
Co-authored-by: 's avatarLuis Alvarez <luis@zeit.co>
parent 22638769
......@@ -66,9 +66,10 @@ 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<{
......@@ -76,17 +77,20 @@ export type GetStaticProps<
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
......
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>
}
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>
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment