Skip to content

Commit 9e021a9

Browse files
authored
feat: add generic to type params to Astro global (#8263)
* feat: add generic to type params to Astro global * chore: changeset
1 parent 04fa5a1 commit 9e021a9

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'astro': patch
3+
---
4+
5+
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`

packages/astro/src/@types/astro.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,9 @@ export interface CLIFlags {
147147
export interface AstroGlobal<
148148
Props extends Record<string, any> = Record<string, any>,
149149
Self = AstroComponentFactory,
150+
Params extends Record<string, string | undefined> = Record<string, string | undefined>,
150151
> extends AstroGlobalPartial,
151-
AstroSharedContext<Props> {
152+
AstroSharedContext<Props, Params> {
152153
/**
153154
* A full URL object of the request URL.
154155
* Equivalent to: `new URL(Astro.request.url)`
@@ -174,7 +175,7 @@ export interface AstroGlobal<
174175
*
175176
* [Astro reference](https://docs.astro.build/en/reference/api-reference/#astroparams)
176177
*/
177-
params: AstroSharedContext['params'];
178+
params: AstroSharedContext<Props, Params>['params'];
178179
/** List of props passed to this component
179180
*
180181
* 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<
184185
*
185186
* [Astro reference](https://docs.astro.build/en/core-concepts/astro-components/#component-props)
186187
*/
187-
props: AstroSharedContext<Props>['props'];
188+
props: AstroSharedContext<Props, Params>['props'];
188189
/** Information about the current request. This is a standard [Request](https://developer.mozilla.org/en-US/docs/Web/API/Request) object
189190
*
190191
* For example, to get a URL object of the current URL, you can use:
@@ -1807,7 +1808,10 @@ type Body = string;
18071808
export type ValidRedirectStatus = 300 | 301 | 302 | 303 | 304 | 307 | 308;
18081809

18091810
// Shared types between `Astro` global and API context object
1810-
interface AstroSharedContext<Props extends Record<string, any> = Record<string, any>> {
1811+
interface AstroSharedContext<
1812+
Props extends Record<string, any> = Record<string, any>,
1813+
RouteParams extends Record<string, string | undefined> = Record<string, string | undefined>,
1814+
> {
18111815
/**
18121816
* The address (usually IP address) of the user. Used with SSR only.
18131817
*/
@@ -1827,7 +1831,7 @@ interface AstroSharedContext<Props extends Record<string, any> = Record<string,
18271831
/**
18281832
* Route parameters for this request if this is a dynamic route.
18291833
*/
1830-
params: Params;
1834+
params: RouteParams;
18311835
/**
18321836
* List of props returned for this path by `getStaticPaths` (**Static Only**).
18331837
*/
@@ -1843,8 +1847,10 @@ interface AstroSharedContext<Props extends Record<string, any> = Record<string,
18431847
locals: App.Locals;
18441848
}
18451849

1846-
export interface APIContext<Props extends Record<string, any> = Record<string, any>>
1847-
extends AstroSharedContext<Props> {
1850+
export interface APIContext<
1851+
Props extends Record<string, any> = Record<string, any>,
1852+
APIParams extends Record<string, string | undefined> = Record<string, string | undefined>,
1853+
> extends AstroSharedContext<Props, Params> {
18481854
site: URL | undefined;
18491855
generator: string;
18501856
/**
@@ -1876,7 +1882,7 @@ export interface APIContext<Props extends Record<string, any> = Record<string, a
18761882
*
18771883
* [context reference](https://docs.astro.build/en/reference/api-reference/#contextparams)
18781884
*/
1879-
params: AstroSharedContext['params'];
1885+
params: AstroSharedContext<Props, APIParams>['params'];
18801886
/**
18811887
* List of props passed from `getStaticPaths`. Only available to static builds.
18821888
*
@@ -1899,7 +1905,7 @@ export interface APIContext<Props extends Record<string, any> = Record<string, a
18991905
*
19001906
* [context reference](https://docs.astro.build/en/guides/api-reference/#contextprops)
19011907
*/
1902-
props: AstroSharedContext<Props>['props'];
1908+
props: AstroSharedContext<Props, APIParams>['props'];
19031909
/**
19041910
* Redirect to another page. Only available in SSR builds.
19051911
*

0 commit comments

Comments
 (0)