From a0ac4de39af63fd2dcb14f1fa68e67db7dd0c1a2 Mon Sep 17 00:00:00 2001 From: Dennis Morello Date: Mon, 6 Feb 2023 14:28:18 +0100 Subject: [PATCH 1/2] feat(astro): add InferGetStaticParamsType and InferGetStaticPropsType type helpers --- packages/astro/src/@types/astro.ts | 54 ++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/packages/astro/src/@types/astro.ts b/packages/astro/src/@types/astro.ts index ea3e5cd3cf71..2ae59067b46f 100644 --- a/packages/astro/src/@types/astro.ts +++ b/packages/astro/src/@types/astro.ts @@ -1092,6 +1092,60 @@ export type GetStaticPaths = ( | GetStaticPathsResult | GetStaticPathsResult[]; +/** + * Infers the shape of the `params` property returned by `getStaticPaths()`. + * + * @example + * ```ts + * export async function getStaticPaths() { + * return results.map((entry) => ({ + * params: { slug: entry.slug }, + * })); + * } + * + * type Params = InferGetStaticParamsType; + * // ^? { slug: string; } + * + * const { slug } = Astro.params as Params; + * ``` + */ +export type InferGetStaticParamsType = T extends () => Promise + ? R extends Array + ? U extends { params: infer P } + ? P + : never + : never + : never; + +/** + * Infers the shape of the `props` property returned by `getStaticPaths()`. + * + * @example + * ```ts + * export async function getStaticPaths() { + * return results.map((entry) => ({ + * params: { slug: entry.slug }, + * props: { + * propA: true, + * propB: 42 + * }, + * })); + * } + * + * type Props = InferGetStaticPropsType; + * // ^? { propA: boolean; propB: number; } + * + * const { propA, propB } = Astro.props as Props; + * ``` + */ +export type InferGetStaticPropsType = T extends () => Promise + ? R extends Array + ? U extends { props: infer P } + ? P + : never + : never + : never; + export interface HydrateOptions { name: string; value?: string; From e9262b3899d51e2dbdfcb675830428a38d5a61e6 Mon Sep 17 00:00:00 2001 From: Dennis Morello Date: Mon, 6 Feb 2023 15:19:37 +0100 Subject: [PATCH 2/2] chore(astro): added changeset --- .changeset/flat-candles-glow.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/flat-candles-glow.md diff --git a/.changeset/flat-candles-glow.md b/.changeset/flat-candles-glow.md new file mode 100644 index 000000000000..75d69f51e4ed --- /dev/null +++ b/.changeset/flat-candles-glow.md @@ -0,0 +1,5 @@ +--- +'astro': minor +--- + +Add getStaticPaths type helpers to infer params and props