diff --git a/.changeset/nervous-laws-knock.md b/.changeset/nervous-laws-knock.md new file mode 100644 index 000000000..b9b979fb9 --- /dev/null +++ b/.changeset/nervous-laws-knock.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Narrow type of `Params`, as its values cannot be numbers diff --git a/packages/astro/src/@types/astro.ts b/packages/astro/src/@types/astro.ts index d24b194c8..b0b19eb17 100644 --- a/packages/astro/src/@types/astro.ts +++ b/packages/astro/src/@types/astro.ts @@ -1047,7 +1047,7 @@ export type GetHydrateCallback = () => Promise<() => void | Promise>; 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; @@ -1146,7 +1146,7 @@ export interface Page { export type PaginateFunction = (data: any[], args?: PaginateOptions) => GetStaticPathsResult; -export type Params = Record; +export type Params = Record; export interface AstroAdapter { name: string; diff --git a/packages/astro/src/core/routing/params.ts b/packages/astro/src/core/routing/params.ts index 0b9275170..6a822861f 100644 --- a/packages/astro/src/core/routing/params.ts +++ b/packages/astro/src/core/routing/params.ts @@ -1,4 +1,4 @@ -import type { Params } from '../../@types/astro'; +import type { GetStaticPathsItem, Params } from '../../@types/astro'; import { validateGetStaticPathsParameter } from './validation.js'; /** @@ -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);