-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
update ChangeFreq
to support typescript configurations with string literal
#6262
Conversation
🦋 Changeset detectedLatest commit: d953771 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 |
@@ -32,7 +32,7 @@ export type SitemapOptions = | |||
entryLimit?: number; | |||
|
|||
// sitemap specific | |||
changefreq?: ChangeFreq; | |||
changefreq?: EnumChangefreq; |
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 can be technically be replaced with
changefreq?: EnumChangefreq; | |
changefreq?: typeof ChangeFreq[keyof typeof ChangeFreq]; |
if we don't want to reuse EnumChangefreq
but EnumChangefreq
is way cleaner and readable
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 seems reasonable to me! cc @Princesseuh for a TypeScript sanity check.
Seems like allowing Lowercase<keyof typeof EnumChangefreq>
would be another way to go here, but might not always be correct if the sitemap
package changes the internal logic.
I have to say I didn't think about the |
I'm cool with either solution, but the I wonder if we can use a utility type like this? type EnumOrValue<E extends Record<string|number|symbol, any>> = E | `${E[keyof E]}`;
type ChangeFreq = EnumOrValue<typeof EnumChangefreq> |
I frankly prefer avoiding the The utility type also work (if using |
I'm not sure if I 100% get it, but you can just use `${EnumChangefreq}` as a type, and it'll accept both using the enum directly and the value. And you get auto completion as well (my real sanity check here is that enums in TypeScript are Not-So-Good:tm:) |
D'oh. That is way easier than either of our suggestions—thanks! |
Completely agree about enums not being good. |
build error came from |
This reverts commit a1e5660.
Since I finally got back access to my pc I tested @natemoo-re suggestions type ChangeFreq = Lowercase<keyof typeof EnumChangefreq> and type EnumOrValue<E extends Record<string|number|symbol, any>> = E | `${E[keyof E]}`;
type ChangeFreq = EnumOrValue<typeof EnumChangefreq> Unfortunately both caused the exact same error. Did some more testing and even my solution doesn't actually work. Marking the PR as draft for now |
only solution I could find is to use Princesseuh's solution and using a changefreq: changefreq as EnumChangefreq, I don't like the fact of using an |
only solution I could find is to use Princesseuh's solution and using a changefreq: changefreq as EnumChangefreq, I don't like the fact of using an |
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'm okay with this cast since it allows a better UX with the string literals! Would love one more approval before merging, just as a gut check.
…literal (withastro#6262) * update `ChangeFreq` * `pnpm exec changeset` * use @Princesseuh suggested change * Revert "use @Princesseuh suggested change" This reverts commit a1e5660. * use @Princesseuh suggested change and an `as`
ATM if you wish to configure the integration in a typescript file like so you get an error :
This is due to how enums (and types made from enums) work in typescript.
To get it working the user has to use an
as ChangeFreq
which isn't great.Or he can access
EnumChangefreq
by addingsitemap
as a project dependencyI'm not a big fan of it since
sitemap
is a dependency of this integration (could lead to version mismatch).I'd like to get autocomplete without risking an version mismatch, to do so I thought of two solutions :
EnumChangefreq
enum fromsitemap
.Re-exporting the enum will force the config to be
changefreq: EnumChangefreq.<DURATION>
and prevent the use of a string literal (and theChangeFreq
becomes useless). It's fine but would, IMO, require a note in the readme and an edit to the CLIastro add sitemap
.The proposed change allows to set
changefreq
with a string literal or viaChangeFreq.<DURATION>
.Testing
I simply tested it by modifying the
index.d.ts
inside thenode_modules
of one of my project. Since the type is never used outside of the config type definition it can't really affect anything.It wasn't enough hence the force push and failed first build, sorry about that.
Docs
I don't think this change need any documentation since everything should be transparent to the user since it's just adding support for the string literal.