Skip to content

Commit

Permalink
Narrow the type of Params (withastro#5442)
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 withastro#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 27, 2022
1 parent 5fa1285 commit 8c81f69
Show file tree
Hide file tree
Showing 3 changed files with 14 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
8 changes: 6 additions & 2 deletions packages/astro/src/@types/astro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ export type {
} from '@astrojs/markdown-remark';
export type { SSRManifest } from '../core/app/types';

type UnionRecord<A extends Record<any, any>, B extends Record<any, any>> = {
[K in keyof A | keyof B]: A[K] | B[K];
};

export interface AstroBuiltinProps {
'client:load'?: boolean;
'client:idle'?: boolean;
Expand Down Expand Up @@ -1047,7 +1051,7 @@ export type GetHydrateCallback = () => Promise<() => void | Promise<void>>;
rss(): never;
}

export type GetStaticPathsItem = { params: Params; props?: Props };
export type GetStaticPathsItem = { params: UnionRecord<Params, Record<string, number>>; props?: Props };
export type GetStaticPathsResult = GetStaticPathsItem[];
export type GetStaticPathsResultKeyed = GetStaticPathsResult & {
keyed: Map<string, GetStaticPathsItem>;
Expand Down Expand Up @@ -1146,7 +1150,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 8c81f69

Please sign in to comment.