From 408eac6aa9fa78e9536210c5c6700cd3bc102a5a Mon Sep 17 00:00:00 2001 From: Piotr Monwid-Olechnowicz Date: Fri, 13 Dec 2024 20:23:37 +0100 Subject: [PATCH] `NextPageProps` (#1867) --- .changeset/brave-sloths-film.md | 5 +++++ packages/components/src/index.ts | 1 + packages/components/src/next-types.ts | 27 +++++++++++++++++++++++++++ 3 files changed, 33 insertions(+) create mode 100644 .changeset/brave-sloths-film.md create mode 100644 packages/components/src/next-types.ts diff --git a/.changeset/brave-sloths-film.md b/.changeset/brave-sloths-film.md new file mode 100644 index 000000000..e0de04e3d --- /dev/null +++ b/.changeset/brave-sloths-film.md @@ -0,0 +1,5 @@ +--- +"@theguild/components": patch +--- + +Add `NextPageProps` utility type diff --git a/packages/components/src/index.ts b/packages/components/src/index.ts index 66bc5a3e8..379a64a06 100644 --- a/packages/components/src/index.ts +++ b/packages/components/src/index.ts @@ -19,6 +19,7 @@ export { PRODUCTS } from './products'; export * from './types/components'; export * from './logos'; export { cn } from './cn'; +export * from './next-types'; declare module 'react' { interface CSSProperties { diff --git a/packages/components/src/next-types.ts b/packages/components/src/next-types.ts new file mode 100644 index 000000000..a856346a9 --- /dev/null +++ b/packages/components/src/next-types.ts @@ -0,0 +1,27 @@ +/** + * Next.js page props type. + * @see https://nextjs.org/docs/app/api-reference/file-conventions/page#props + * @see https://nextjs.org/docs/app/building-your-application/routing/dynamic-routes#good-to-know + */ +export interface NextPageProps< + TParams extends string = never, + TSearchParams extends string = never, +> { + params: Promise< + UnionToIntersection< + { + [K in TParams]: { + [F in K extends `...${infer U}` ? U : K]: K extends `...${string}` ? string[] : string; + }; + }[TParams] + > + >; + searchParams: Promise<{ [K in TSearchParams]?: string | string[] }>; +} + +type Prettify = { [K in keyof T]: T[K] } & {}; + +type UnionToIntersection = Prettify< + // eslint-disable-next-line @typescript-eslint/no-explicit-any + (T extends any ? (x: T) => any : never) extends (x: infer R) => any ? R : never +>;