-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Conversation
🦋 Changeset detectedLatest 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 |
// 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(); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I par
am very impressed! Great change 👍
While our API reference page does in fact say that
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 ) |
Thanks @sarah11918. The error reference looks correct to me as-is. |
Fixes #5442.
/cc @tony-sull & @bholmesdev
Changes
#3087 changed the type of
Params
fromRecord<string, string | undefined>
toRecord<string, string | number | undefined>
to formerly allowgetStaticPaths
to return numbers.However, as the change was made to
Params
and notGetStaticPathsResult
orGetStaticPathsItem
, it also affectsAstro.params
.This PR essentially moves the change made in #3087 from
Params
toGetStaticPathsItem
, reverting the type ofAstro.params
while keeping the type ofgetStaticPaths
.Note that while looking into this, I found this comment by @bholmesdev:
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:
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: