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

feat: Adding generic typing for previewData #28668

Merged
merged 7 commits into from
Sep 3, 2021
Merged
Show file tree
Hide file tree
Changes from 5 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
22 changes: 14 additions & 8 deletions packages/next/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,13 @@ export {

export type PreviewData = string | false | object | undefined

export type GetStaticPropsContext<Q extends ParsedUrlQuery = ParsedUrlQuery> = {
export type GetStaticPropsContext<
Q extends ParsedUrlQuery = ParsedUrlQuery,
P extends PreviewData = PreviewData
> = {
params?: Q
preview?: boolean
previewData?: PreviewData
previewData?: P
locale?: string
locales?: string[]
defaultLocale?: string
Expand All @@ -115,9 +118,10 @@ export type GetStaticPropsResult<P> =

export type GetStaticProps<
P extends { [key: string]: any } = { [key: string]: any },
Q extends ParsedUrlQuery = ParsedUrlQuery
Q extends ParsedUrlQuery = ParsedUrlQuery,
S extends PreviewData = PreviewData
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use a consistent name for this, perhaps D instead of P or S?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure! I really don't mind I wasn't sure what capital letter to use 😅

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just pushed the changes :)

> = (
context: GetStaticPropsContext<Q>
context: GetStaticPropsContext<Q, S>
) => Promise<GetStaticPropsResult<P>> | GetStaticPropsResult<P>

export type InferGetStaticPropsType<T> = T extends GetStaticProps<infer P, any>
Expand All @@ -143,7 +147,8 @@ export type GetStaticPaths<P extends ParsedUrlQuery = ParsedUrlQuery> = (
) => Promise<GetStaticPathsResult<P>> | GetStaticPathsResult<P>

export type GetServerSidePropsContext<
Q extends ParsedUrlQuery = ParsedUrlQuery
Q extends ParsedUrlQuery = ParsedUrlQuery,
P extends PreviewData = PreviewData
> = {
req: IncomingMessage & {
cookies: NextApiRequestCookies
Expand All @@ -152,7 +157,7 @@ export type GetServerSidePropsContext<
params?: Q
query: ParsedUrlQuery
preview?: boolean
previewData?: PreviewData
previewData?: P
resolvedUrl: string
locale?: string
locales?: string[]
Expand All @@ -166,9 +171,10 @@ export type GetServerSidePropsResult<P> =

export type GetServerSideProps<
P extends { [key: string]: any } = { [key: string]: any },
Q extends ParsedUrlQuery = ParsedUrlQuery
Q extends ParsedUrlQuery = ParsedUrlQuery,
S extends PreviewData = PreviewData
> = (
context: GetServerSidePropsContext<Q>
context: GetServerSidePropsContext<Q, S>
) => Promise<GetServerSidePropsResult<P>>

export type InferGetServerSidePropsType<T> = T extends GetServerSideProps<
Expand Down
29 changes: 20 additions & 9 deletions test/integration/typescript/pages/ssg/[slug].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ type Params = {

type Props = {
data: string
title: string
}

type PreviewData = {
title: string
}

export const getStaticPaths: GetStaticPaths<Params> = async () => {
Expand All @@ -15,15 +20,21 @@ export const getStaticPaths: GetStaticPaths<Params> = async () => {
}
}

export const getStaticProps: GetStaticProps<Props, Params> = async ({
params,
}) => {
return {
props: { data: params!.slug },
revalidate: false,
export const getStaticProps: GetStaticProps<Props, Params, PreviewData> =
async ({ params, previewData }) => {
return {
props: {
data: params!.slug,
title: previewData?.title || 'default title',
},
revalidate: false,
}
}
}

export default function Page({ data }: Props) {
return <h1>{data}</h1>
export default function Page({ data, title }: Props) {
return (
<h1>
{data} {title}
</h1>
)
}
23 changes: 17 additions & 6 deletions test/integration/typescript/pages/ssr/[slug].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,27 @@ type Params = {

type Props = {
data: string
title: string
}

export const getServerSideProps: GetServerSideProps<Props, Params> = async ({
params,
}) => {
type PreviewData = {
title: string
}

export const getServerSideProps: GetServerSideProps<
Props,
Params,
PreviewData
> = async ({ params, previewData }) => {
return {
props: { data: params!.slug },
props: { data: params!.slug, title: previewData?.title || 'default title' },
}
}

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