From 8c81f697c074332b3187dd0ba9a937beb62f23f9 Mon Sep 17 00:00:00 2001 From: "Pimm \"de Chinchilla\" Hogeling" Date: Sun, 27 Nov 2022 13:19:04 +0100 Subject: [PATCH] Narrow the type of Params (#5442) A PR merged back in April changed the type of Params, allowing numbers to be provided in addition to strings. See https://github.com/withastro/astro/pull/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. --- .changeset/nervous-laws-knock.md | 5 +++++ packages/astro/src/@types/astro.ts | 8 ++++++-- packages/astro/src/core/routing/params.ts | 6 +++--- 3 files changed, 14 insertions(+), 5 deletions(-) create mode 100644 .changeset/nervous-laws-knock.md 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);