Skip to content

Commit

Permalink
Add getStaticPaths type helpers to infer params and props (#6150)
Browse files Browse the repository at this point in the history
* feat(astro): add InferGetStaticParamsType and InferGetStaticPropsType type helpers

* chore(astro): added changeset
  • Loading branch information
morellodev committed Mar 6, 2023
1 parent 19fe4cb commit b087b83
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/flat-candles-glow.md
@@ -0,0 +1,5 @@
---
'astro': minor
---

Add getStaticPaths type helpers to infer params and props
54 changes: 54 additions & 0 deletions packages/astro/src/@types/astro.ts
Expand Up @@ -1094,6 +1094,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<typeof getStaticPaths>;
* // ^? { slug: string; }
*
* const { slug } = Astro.params as Params;
* ```
*/
export type InferGetStaticParamsType<T> = T extends () => Promise<infer R>
? R extends Array<infer U>
? 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<typeof getStaticPaths>;
* // ^? { propA: boolean; propB: number; }
*
* const { propA, propB } = Astro.props as Props;
* ```
*/
export type InferGetStaticPropsType<T> = T extends () => Promise<infer R>
? R extends Array<infer U>
? U extends { props: infer P }
? P
: never
: never
: never;

export interface HydrateOptions {
name: string;
value?: string;
Expand Down

0 comments on commit b087b83

Please sign in to comment.