feat: add generic to type params to Astro global (#8263)

* feat: add generic to type params to Astro global

* chore: changeset
This commit is contained in:
Erika 2023-08-29 16:04:25 +02:00 committed by GitHub
parent 04fa5a1453
commit 9e021a91c5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 9 deletions

View file

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

View file

@ -147,8 +147,9 @@ export interface CLIFlags {
export interface AstroGlobal< export interface AstroGlobal<
Props extends Record<string, any> = Record<string, any>, Props extends Record<string, any> = Record<string, any>,
Self = AstroComponentFactory, Self = AstroComponentFactory,
Params extends Record<string, string | undefined> = Record<string, string | undefined>,
> extends AstroGlobalPartial, > extends AstroGlobalPartial,
AstroSharedContext<Props> { AstroSharedContext<Props, Params> {
/** /**
* A full URL object of the request URL. * A full URL object of the request URL.
* Equivalent to: `new URL(Astro.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) * [Astro reference](https://docs.astro.build/en/reference/api-reference/#astroparams)
*/ */
params: AstroSharedContext['params']; params: AstroSharedContext<Props, Params>['params'];
/** List of props passed to this component /** 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: * 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) * [Astro reference](https://docs.astro.build/en/core-concepts/astro-components/#component-props)
*/ */
props: AstroSharedContext<Props>['props']; props: AstroSharedContext<Props, Params>['props'];
/** Information about the current request. This is a standard [Request](https://developer.mozilla.org/en-US/docs/Web/API/Request) object /** 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: * 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; export type ValidRedirectStatus = 300 | 301 | 302 | 303 | 304 | 307 | 308;
// Shared types between `Astro` global and API context object // Shared types between `Astro` global and API context object
interface AstroSharedContext<Props extends Record<string, any> = Record<string, any>> { interface AstroSharedContext<
Props extends Record<string, any> = Record<string, any>,
RouteParams extends Record<string, string | undefined> = Record<string, string | undefined>,
> {
/** /**
* The address (usually IP address) of the user. Used with SSR only. * The address (usually IP address) of the user. Used with SSR only.
*/ */
@ -1827,7 +1831,7 @@ interface AstroSharedContext<Props extends Record<string, any> = Record<string,
/** /**
* Route parameters for this request if this is a dynamic route. * Route parameters for this request if this is a dynamic route.
*/ */
params: Params; params: RouteParams;
/** /**
* List of props returned for this path by `getStaticPaths` (**Static Only**). * List of props returned for this path by `getStaticPaths` (**Static Only**).
*/ */
@ -1843,8 +1847,10 @@ interface AstroSharedContext<Props extends Record<string, any> = Record<string,
locals: App.Locals; locals: App.Locals;
} }
export interface APIContext<Props extends Record<string, any> = Record<string, any>> export interface APIContext<
extends AstroSharedContext<Props> { Props extends Record<string, any> = Record<string, any>,
APIParams extends Record<string, string | undefined> = Record<string, string | undefined>,
> extends AstroSharedContext<Props, Params> {
site: URL | undefined; site: URL | undefined;
generator: string; generator: string;
/** /**
@ -1876,7 +1882,7 @@ export interface APIContext<Props extends Record<string, any> = Record<string, a
* *
* [context reference](https://docs.astro.build/en/reference/api-reference/#contextparams) * [context reference](https://docs.astro.build/en/reference/api-reference/#contextparams)
*/ */
params: AstroSharedContext['params']; params: AstroSharedContext<Props, APIParams>['params'];
/** /**
* List of props passed from `getStaticPaths`. Only available to static builds. * List of props passed from `getStaticPaths`. Only available to static builds.
* *
@ -1899,7 +1905,7 @@ export interface APIContext<Props extends Record<string, any> = Record<string, a
* *
* [context reference](https://docs.astro.build/en/guides/api-reference/#contextprops) * [context reference](https://docs.astro.build/en/guides/api-reference/#contextprops)
*/ */
props: AstroSharedContext<Props>['props']; props: AstroSharedContext<Props, APIParams>['props'];
/** /**
* Redirect to another page. Only available in SSR builds. * Redirect to another page. Only available in SSR builds.
* *