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:
parent
19fe4cb629
commit
b087b83fe2
2 changed files with 59 additions and 0 deletions
5
.changeset/flat-candles-glow.md
Normal file
5
.changeset/flat-candles-glow.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'astro': minor
|
||||||
|
---
|
||||||
|
|
||||||
|
Add getStaticPaths type helpers to infer params and props
|
|
@ -1094,6 +1094,60 @@ export type GetStaticPaths = (
|
||||||
| GetStaticPathsResult
|
| GetStaticPathsResult
|
||||||
| 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 {
|
export interface HydrateOptions {
|
||||||
name: string;
|
name: string;
|
||||||
value?: string;
|
value?: string;
|
||||||
|
|
Loading…
Reference in a new issue