diff options
author | 2022-11-28 17:06:37 +0100 | |
---|---|---|
committer | 2022-11-28 11:06:37 -0500 | |
commit | 731e99df875d40e40f6b33feddd940c3122a5f83 (patch) | |
tree | 745f3543b698b6112f6823dd1315cb58fa6c4f46 | |
parent | 5e693c21438d3a4840cd1906a346d38f05fdb753 (diff) | |
download | astro-731e99df875d40e40f6b33feddd940c3122a5f83.tar.gz astro-731e99df875d40e40f6b33feddd940c3122a5f83.tar.zst astro-731e99df875d40e40f6b33feddd940c3122a5f83.zip |
Narrow the type of Params (#5442) (#5484)
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.
-rw-r--r-- | .changeset/nervous-laws-knock.md | 5 | ||||
-rw-r--r-- | packages/astro/src/@types/astro.ts | 4 | ||||
-rw-r--r-- | packages/astro/src/core/routing/params.ts | 6 |
3 files changed, 10 insertions, 5 deletions
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<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>; @@ -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; 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); |