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

dayjs.locale() throws type error due to type update in v.1.8.30 #964

Closed
B1rch opened this issue Jul 22, 2020 · 11 comments · Fixed by #968
Closed

dayjs.locale() throws type error due to type update in v.1.8.30 #964

B1rch opened this issue Jul 22, 2020 · 11 comments · Fixed by #968
Labels
☢️Bug Something isn't working released

Comments

@B1rch
Copy link

B1rch commented Jul 22, 2020

Describe the bug
The locale() function used to accept a string up until v1.8.30, which updated the types to use LocalePresetType (73813ab). This has broken our CI/CD pipelines, as our i18n implementation returns the locale code as a string.

Expected behavior
Patch versions should not include type changes that are not backwards compatible with previous versions.

Information

  • Day.js Version [1.8.30]
  • OS: [Ubuntu 16.04]
  • Browser [n/a]

suggestion
Add back string as a valid type either in the function declarations or in the LocalePresetType union.

We can work around this issue by casting the string to a LocalePresetType but we would prefer if we didn't have to do this everywhere our i18n interacts with dayjs.

@iamkun
Copy link
Owner

iamkun commented Jul 22, 2020

Sorry for the inconvenience. We will fix this by adding a back string type in LocalePresetType in 1.8.31 shortly.

@iamkun
Copy link
Owner

iamkun commented Jul 22, 2020

One problem is if I added string as a backup type. It will be useless to add these preset strings?

e.g.

type localeName = 'en' | 'es' | string

Typescript will treat and string as valid, and IDE will have no auto-complete at all.

@B1rch
Copy link
Author

B1rch commented Jul 22, 2020

That is true, and there might be better way to achieve this. But I don't imagine people are hardcoding locale strings all to often. Regardless, not having breaking changes in a patch version has a higher priority over QOL changes if it were up to me.

I haven't looked at the source so I don't know what the previous behavior was, but maybe you can look into adding a warning if the locale code provided to the function is not supported, and perhaps adding the option for a fallback locale to be used in that case.

@iamkun
Copy link
Owner

iamkun commented Jul 22, 2020

Maybe we should roll back to string only instead of the listed locale names.

@muuvmuuv
Copy link

Make it a major release and mark it a breaking change would also do it. I prefer stricter typing.

@iamkun
Copy link
Owner

iamkun commented Jul 24, 2020

@muuvmuuv It just comes to me that there might be user-customized locale name, like 'aaa', maybe the only thing we could do is fall back to 'string' type.

@muuvmuuv
Copy link

I don't think so because you can extend the type with custom types like this:

type Locale<AdditionalTypes = void> = LocalePresetType | AdditionalTypes

type CustomLocales = 'aaa' | 'bbb'

const one: Locale = 'az'
const two: Locale<CustomLocales> = 'aaa'

I would fall back to string only if dayjs locale gets intelligent enough so that de-DE gets recognized as de, then typings are not longer needed. Or at least append string so the build in locales are typed. Momentjs does it that way like I mentioned in another issue.

@dyoshikawa
Copy link
Contributor

dyoshikawa commented Jul 24, 2020

I'm sorry that I suggested this type. #955
It caused the unpredictable inconvenience.

@muuvmuuv 's suggestion looks good to me in the long run.
But in near future, should add string to LocalePresetType as a interim solution, I think.

@iamkun
Copy link
Owner

iamkun commented Jul 27, 2020

This would be a good fix at this moment. #968

@umairhm
Copy link

umairhm commented Jul 28, 2020

This would be a good fix at this moment. #968

Folks, do we have any timeline for this to be merged and released? Any help would be highly appreciated, thank you!

@iamkun
Copy link
Owner

iamkun commented Jul 29, 2020

🎉 This issue has been resolved in version 1.8.31 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
☢️Bug Something isn't working released
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants