Add getStaticPaths type helpers to infer params and props (#6150)

* feat(astro): add InferGetStaticParamsType and InferGetStaticPropsType type helpers

* chore(astro): added changeset
This commit is contained in:
Dennis Morello 2023-03-06 17:38:42 +01:00 committed by GitHub
parent 19fe4cb629
commit b087b83fe2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 59 additions and 0 deletions

View file

@ -0,0 +1,5 @@
---
'astro': minor
---
Add getStaticPaths type helpers to infer params and props

View file

@ -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;