From be16d1e6268f16afd686352b669ea8825b1c2f93 Mon Sep 17 00:00:00 2001 From: Jane Manchun Wong Date: Sun, 29 Mar 2020 00:43:07 +0800 Subject: [PATCH 1/3] Generic Type for GetStaticPaths --- packages/next/types/index.d.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/next/types/index.d.ts b/packages/next/types/index.d.ts index 271e1b532d278..6a4c53d78c1f0 100644 --- a/packages/next/types/index.d.ts +++ b/packages/next/types/index.d.ts @@ -78,8 +78,10 @@ export type GetStaticProps< revalidate?: number | boolean }> -export type GetStaticPaths = () => Promise<{ - paths: Array +export type GetStaticPaths< + P extends ParsedUrlQuery = ParsedUrlQuery +> = () => Promise<{ + paths: Array fallback: boolean }> From 735ee6f96cb5ed1b72fd27a00c3a80b6c3d32e20 Mon Sep 17 00:00:00 2001 From: Jane Manchun Wong Date: Sun, 29 Mar 2020 01:31:44 +0800 Subject: [PATCH 2/3] Add generic typing for params to GSSP and GSP too --- packages/next/types/index.d.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/packages/next/types/index.d.ts b/packages/next/types/index.d.ts index 6a4c53d78c1f0..6091f716b65c5 100644 --- a/packages/next/types/index.d.ts +++ b/packages/next/types/index.d.ts @@ -67,9 +67,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 env: Env @@ -86,11 +87,12 @@ export type GetStaticPaths< }> 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 env: Env preview?: boolean From db3038bb8f10204deccd0ef72bf48f159003ba64 Mon Sep 17 00:00:00 2001 From: Luis Alvarez Date: Mon, 30 Mar 2020 21:31:32 -0500 Subject: [PATCH 3/3] Added basic tests --- .../typescript/pages/ssg/[slug].tsx | 29 +++++++++++++++++++ .../typescript/pages/ssr/[slug].tsx | 21 ++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 test/integration/typescript/pages/ssg/[slug].tsx create mode 100644 test/integration/typescript/pages/ssr/[slug].tsx diff --git a/test/integration/typescript/pages/ssg/[slug].tsx b/test/integration/typescript/pages/ssg/[slug].tsx new file mode 100644 index 0000000000000..5d6af8cfcc780 --- /dev/null +++ b/test/integration/typescript/pages/ssg/[slug].tsx @@ -0,0 +1,29 @@ +import { GetStaticPaths, GetStaticProps } from 'next' + +type Params = { + slug: string +} + +type Props = { + data: string +} + +export const getStaticPaths: GetStaticPaths = async () => { + return { + paths: [{ params: { slug: 'test' } }], + fallback: false, + } +} + +export const getStaticProps: GetStaticProps = async ({ + params, +}) => { + return { + props: { data: params!.slug }, + revalidate: false, + } +} + +export default function Page({ data }: Props) { + return

{data}

+} diff --git a/test/integration/typescript/pages/ssr/[slug].tsx b/test/integration/typescript/pages/ssr/[slug].tsx new file mode 100644 index 0000000000000..ac201cbf1984a --- /dev/null +++ b/test/integration/typescript/pages/ssr/[slug].tsx @@ -0,0 +1,21 @@ +import { GetServerSideProps } from 'next' + +type Params = { + slug: string +} + +type Props = { + data: string +} + +export const getServerSideProps: GetServerSideProps = async ({ + params, +}) => { + return { + props: { data: params!.slug }, + } +} + +export default function Page({ data }: Props) { + return

{data}

+}