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 { ...@@ -66,9 +66,10 @@ export {
} }
export type GetStaticProps< export type GetStaticProps<
P extends { [key: string]: any } = { [key: string]: any } P extends { [key: string]: any } = { [key: string]: any },
Q extends ParsedUrlQuery = ParsedUrlQuery
> = (ctx: { > = (ctx: {
params?: ParsedUrlQuery params?: Q
preview?: boolean preview?: boolean
previewData?: any previewData?: any
}) => Promise<{ }) => Promise<{
...@@ -76,17 +77,20 @@ export type GetStaticProps< ...@@ -76,17 +77,20 @@ export type GetStaticProps<
revalidate?: number | boolean revalidate?: number | boolean
}> }>
export type GetStaticPaths = () => Promise<{ export type GetStaticPaths<
paths: Array<string | { params: ParsedUrlQuery }> P extends ParsedUrlQuery = ParsedUrlQuery
> = () => Promise<{
paths: Array<string | { params: P }>
fallback: boolean fallback: boolean
}> }>
export type GetServerSideProps< export type GetServerSideProps<
P extends { [key: string]: any } = { [key: string]: any } P extends { [key: string]: any } = { [key: string]: any },
Q extends ParsedUrlQuery = ParsedUrlQuery
> = (context: { > = (context: {
req: IncomingMessage req: IncomingMessage
res: ServerResponse res: ServerResponse
params?: ParsedUrlQuery params?: Q
query: ParsedUrlQuery query: ParsedUrlQuery
preview?: boolean preview?: boolean
previewData?: any 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