diff --git a/.changeset/nervous-laws-knock.md b/.changeset/nervous-laws-knock.md new file mode 100644 index 0000000000000..b9b979fb9367a --- /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 d24b194c8c6d5..1c362117184ef 100644 --- a/packages/astro/src/@types/astro.ts +++ b/packages/astro/src/@types/astro.ts @@ -28,6 +28,10 @@ export type { } from '@astrojs/markdown-remark'; export type { SSRManifest } from '../core/app/types'; +type UnionRecord, B extends Record> = { + [K in keyof A | keyof B]: A[K] | B[K]; +}; + export interface AstroBuiltinProps { 'client:load'?: boolean; 'client:idle'?: boolean; @@ -1047,7 +1051,7 @@ export type GetHydrateCallback = () => Promise<() => void | Promise>; rss(): never; } -export type GetStaticPathsItem = { params: Params; props?: Props }; +export type GetStaticPathsItem = { params: UnionRecord>; props?: Props }; export type GetStaticPathsResult = GetStaticPathsItem[]; export type GetStaticPathsResultKeyed = GetStaticPathsResult & { keyed: Map; @@ -1146,7 +1150,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 0b92751708538..6a822861fecf7 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);