From 9e021a91c57d10809f588dd47968fc0e7f8b4d5c Mon Sep 17 00:00:00 2001 From: Erika <3019731+Princesseuh@users.noreply.github.com> Date: Tue, 29 Aug 2023 16:04:25 +0200 Subject: [PATCH] feat: add generic to type params to Astro global (#8263) * feat: add generic to type params to Astro global * chore: changeset --- .changeset/heavy-countries-wonder.md | 5 +++++ packages/astro/src/@types/astro.ts | 24 +++++++++++++++--------- 2 files changed, 20 insertions(+), 9 deletions(-) create mode 100644 .changeset/heavy-countries-wonder.md diff --git a/.changeset/heavy-countries-wonder.md b/.changeset/heavy-countries-wonder.md new file mode 100644 index 000000000000..599f0d8bb66b --- /dev/null +++ b/.changeset/heavy-countries-wonder.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Add a type param to AstroGlobal to type params. This will eventually be used automatically by our tooling to provide typing and completions for `Astro.params` diff --git a/packages/astro/src/@types/astro.ts b/packages/astro/src/@types/astro.ts index 86d7bc984497..4a2ad52644c2 100644 --- a/packages/astro/src/@types/astro.ts +++ b/packages/astro/src/@types/astro.ts @@ -147,8 +147,9 @@ export interface CLIFlags { export interface AstroGlobal< Props extends Record = Record, Self = AstroComponentFactory, + Params extends Record = Record, > extends AstroGlobalPartial, - AstroSharedContext { + AstroSharedContext { /** * A full URL object of the request URL. * Equivalent to: `new URL(Astro.request.url)` @@ -174,7 +175,7 @@ export interface AstroGlobal< * * [Astro reference](https://docs.astro.build/en/reference/api-reference/#astroparams) */ - params: AstroSharedContext['params']; + params: AstroSharedContext['params']; /** List of props passed to this component * * A common way to get specific props is through [destructuring](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment), ex: @@ -184,7 +185,7 @@ export interface AstroGlobal< * * [Astro reference](https://docs.astro.build/en/core-concepts/astro-components/#component-props) */ - props: AstroSharedContext['props']; + props: AstroSharedContext['props']; /** Information about the current request. This is a standard [Request](https://developer.mozilla.org/en-US/docs/Web/API/Request) object * * For example, to get a URL object of the current URL, you can use: @@ -1807,7 +1808,10 @@ type Body = string; export type ValidRedirectStatus = 300 | 301 | 302 | 303 | 304 | 307 | 308; // Shared types between `Astro` global and API context object -interface AstroSharedContext = Record> { +interface AstroSharedContext< + Props extends Record = Record, + RouteParams extends Record = Record, +> { /** * The address (usually IP address) of the user. Used with SSR only. */ @@ -1827,7 +1831,7 @@ interface AstroSharedContext = Record = Record = Record> - extends AstroSharedContext { +export interface APIContext< + Props extends Record = Record, + APIParams extends Record = Record, +> extends AstroSharedContext { site: URL | undefined; generator: string; /** @@ -1876,7 +1882,7 @@ export interface APIContext = Record['params']; /** * List of props passed from `getStaticPaths`. Only available to static builds. * @@ -1899,7 +1905,7 @@ export interface APIContext = Record['props']; + props: AstroSharedContext['props']; /** * Redirect to another page. Only available in SSR builds. *