js-joda-locale
js-joda-locale copied to clipboard
TypeScript types for locales
We're using the en-US locale from TypeScript, and we got it to work, but I'm wondering if the way we did it is right, and if it is, whether the package could be improved to include its own types.
Here's what we did:
npm install js-joda js-joda-timezone @js-joda/locale_en-us
And then we used:
import { DateTimeFormatter } from 'js-joda'
import 'js-joda-timezone'
import { Locale } from '@js-joda/locale_en-us'
const dateFormatter = DateTimeFormatter.ofPattern('EEEE dd MMMM').withLocale(Locale.US)
And here's the bit that could maybe improve the project; we created a type definition for @js-joda/locale_en-us:
js-joda-locale_en-us.d.ts:
declare module '@js-joda/locale_en-us' {
import { Locale } from 'js-joda'
export const Locale: {
US: Locale
}
}
It seems that a type definition like this could be adapted for each of the generated packages, to declare the locales that are included in each. That would sure be neat, unless I've missed something else that solves this!
Hm... sounds like a good idea... we will have a look...
We are also happy to accept PRs :)
Regards, Pattrick
@phueper Vielen Dank Pattrick! I wasn't immediately sure how to introduce these types for the generate packages. They're generated by a script? Or could we just create a type definition for each of the currently generated packages and include it?
Ah... i see what you mean... dynamically creating the locale definitions would be trickier since the script to generate them would need to be modified... however, since we currently hardcode some locales anyway and don't really dynamically create them (https://github.com/js-joda/js-joda-locale/blob/master/src/Locale.js ) ... i think a static typescript definition file for these hardcoded Locale definitions might be enough?
This is badly needed given how common Typescript is becoming.