Skip to content

Commit

Permalink
Narrow the type of Params (#5442) (#5484)
Browse files Browse the repository at this point in the history
A PR merged back in April changed the type of Params, allowing numbers to be provided in addition to strings. See #3087. However, as said PR changed the type of Params instead of GetStaticPathsItem, it also affects Astro.params. This commit moves the change to GetStaticPathsItem, reverting the type of Astro.params.
  • Loading branch information
Pimm committed Nov 28, 2022
1 parent 5e693c2 commit 731e99d
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/nervous-laws-knock.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Narrow type of `Params`, as its values cannot be numbers
4 changes: 2 additions & 2 deletions packages/astro/src/@types/astro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1047,7 +1047,7 @@ export type GetHydrateCallback = () => Promise<() => void | Promise<void>>;
rss(): never;
}

export type GetStaticPathsItem = { params: Params; props?: Props };
export type GetStaticPathsItem = { params: { [K in keyof Params]: Params[K] | number }; props?: Props };
export type GetStaticPathsResult = GetStaticPathsItem[];
export type GetStaticPathsResultKeyed = GetStaticPathsResult & {
keyed: Map<string, GetStaticPathsItem>;
Expand Down Expand Up @@ -1146,7 +1146,7 @@ export interface Page<T = any> {

export type PaginateFunction = (data: any[], args?: PaginateOptions) => GetStaticPathsResult;

export type Params = Record<string, string | number | undefined>;
export type Params = Record<string, string | undefined>;

export interface AstroAdapter {
name: string;
Expand Down
6 changes: 3 additions & 3 deletions packages/astro/src/core/routing/params.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Params } from '../../@types/astro';
import type { GetStaticPathsItem, Params } from '../../@types/astro';
import { validateGetStaticPathsParameter } from './validation.js';

/**
Expand Down Expand Up @@ -27,12 +27,12 @@ export function getParams(array: string[]) {
* values and create a stringified key for the route
* that can be used to match request routes
*/
export function stringifyParams(params: Params, routeComponent: string) {
export function stringifyParams(params: GetStaticPathsItem['params'], routeComponent: string) {
// validate parameter values then stringify each value
const validatedParams = Object.entries(params).reduce((acc, next) => {
validateGetStaticPathsParameter(next, routeComponent);
const [key, value] = next;
acc[key] = typeof value === 'undefined' ? undefined : `${value}`;
acc[key] = value?.toString();
return acc;
}, {} as Params);

Expand Down

0 comments on commit 731e99d

Please sign in to comment.