Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Narrow the type of Params #5484

Merged
merged 1 commit into from
Nov 28, 2022
Merged

Narrow the type of Params #5484

merged 1 commit into from
Nov 28, 2022

Conversation

Pimm
Copy link
Contributor

@Pimm Pimm commented Nov 27, 2022

Fixes #5442.

/cc @tony-sull & @bholmesdev

Changes

#3087 changed the type of Params from Record<string, string | undefined> to Record<string, string | number | undefined> to formerly allow getStaticPaths to return numbers.

However, as the change was made to Params and not GetStaticPathsResult or GetStaticPathsItem, it also affects Astro.params.

This PR essentially moves the change made in #3087 from Params to GetStaticPathsItem, reverting the type of Astro.params while keeping the type of getStaticPaths.

Note that while looking into this, I found this comment by @bholmesdev:

  • if you pass a number to params.year in your getStaticPaths, it should still be a number when destructuring Astro.params. If not, that's a bug.
  • if you created an SSR dynamic route like [year].astro, year will always be a string (even if it could be parsed to a number). This is where string -> number conversion would be manual.

In my experience, Astro.params never contains numbers, not even in static mode. While reviewing, please consider that this PR changes the types to match the behaviour, although judging by this comment, we might want to change the behaviour to match the types instead.

Testing

This is testable like so:

const artist: string | undefined = Astro.params.artist;

I haven't found any type definition tests to which this one could be added.

Docs

From what I understand, this change is in line with the current docs:

params are encoded into the URL, so only strings are supported as values.

@Pimm Pimm requested a review from a team as a code owner November 27, 2022 17:18
@changeset-bot
Copy link

changeset-bot bot commented Nov 27, 2022

🦋 Changeset detected

Latest commit: 317660e

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions github-actions bot added the pkg: astro Related to the core `astro` package (scope) label Nov 27, 2022
// 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();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is more concise and works for undefined, numbers, and strings, but does require support for the ?. operator. Since this support was added in Node.js 14.0.0, this should be OK.

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.
Copy link
Contributor

@bholmesdev bholmesdev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I param very impressed! Great change 👍

@matthewp matthewp merged commit 731e99d into withastro:main Nov 28, 2022
@astrobot-houston astrobot-houston mentioned this pull request Nov 28, 2022
@sarah11918
Copy link
Member

While our API reference page does in fact say that params must be strings, we have this in our new Error reference guide:

Screenshot 2022-11-28 18 10 11
https://docs.astro.build/en/reference/error-reference/#invalid-value-for-getstaticpaths-route-parameter

So, no objections from docs if we're now all rallying behind Team String, but our error reference will have to change to match. (cc @Princesseuh @matthewp )

@Pimm
Copy link
Contributor Author

Pimm commented Nov 29, 2022

Thanks @sarah11918.

The error reference looks correct to me as-is. getStaticPaths may still return numbers (although those numbers will reincarnate as strings in Astro.params).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg: astro Related to the core `astro` package (scope)
Projects
None yet
5 participants