Skip to content

Commit

Permalink
Fix InferGetStaticParamsType and InferGetStaticPropsType not working …
Browse files Browse the repository at this point in the history
…with sync getStaticPaths (#6711)

* fix(types): Fix InferGetStaticParamsType (and its props equivalent) not working when getStaticPaths wasn't async

* chore: changeset

* fix: use type imports
  • Loading branch information
Princesseuh committed Mar 30, 2023
1 parent a0bdf4c commit c04ea0d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/odd-falcons-exist.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fix InferGetStaticParamsType and InferGetStaticPropsType not working when getStaticPaths wasn't async
18 changes: 11 additions & 7 deletions packages/astro/src/@types/astro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1197,19 +1197,21 @@ export type GetStaticPaths = (
*
* @example
* ```ts
* export async function getStaticPaths() {
* import type { GetStaticPaths } from 'astro';
*
* export const getStaticPaths = (() => {
* return results.map((entry) => ({
* params: { slug: entry.slug },
* }));
* }
* }) satisfies GetStaticPaths;
*
* type Params = InferGetStaticParamsType<typeof getStaticPaths>;
* // ^? { slug: string; }
*
* const { slug } = Astro.params as Params;
* ```
*/
export type InferGetStaticParamsType<T> = T extends () => Promise<infer R>
export type InferGetStaticParamsType<T> = T extends () => infer R | Promise<infer R>
? R extends Array<infer U>
? U extends { params: infer P }
? P
Expand All @@ -1222,23 +1224,25 @@ export type InferGetStaticParamsType<T> = T extends () => Promise<infer R>
*
* @example
* ```ts
* export async function getStaticPaths() {
* import type { GetStaticPaths } from 'astro';
*
* export const getStaticPaths = (() => {
* return results.map((entry) => ({
* params: { slug: entry.slug },
* props: {
* propA: true,
* propB: 42
* },
* }));
* }
* }) satisfies GetStaticPaths;
*
* type Props = InferGetStaticPropsType<typeof getStaticPaths>;
* // ^? { propA: boolean; propB: number; }
*
* const { propA, propB } = Astro.props as Props;
* const { propA, propB } = Astro.props;
* ```
*/
export type InferGetStaticPropsType<T> = T extends () => Promise<infer R>
export type InferGetStaticPropsType<T> = T extends () => infer R | Promise<infer R>
? R extends Array<infer U>
? U extends { props: infer P }
? P
Expand Down

0 comments on commit c04ea0d

Please sign in to comment.