Fix InferGetStaticParamsType and InferGetStaticPropsType not working 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
This commit is contained in:
Erika 2023-03-30 11:41:36 +02:00 committed by GitHub
parent a0bdf4ce2f
commit c04ea0d43c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 7 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Fix InferGetStaticParamsType and InferGetStaticPropsType not working when getStaticPaths wasn't async

View file

@ -1197,11 +1197,13 @@ 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; }
@ -1209,7 +1211,7 @@ export type GetStaticPaths = (
* 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
@ -1222,7 +1224,9 @@ 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: {
@ -1230,15 +1234,15 @@ export type InferGetStaticParamsType<T> = T extends () => Promise<infer R>
* 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