fix: improve type of values accepted by date/time/number formats#2359
fix: improve type of values accepted by date/time/number formats#2359andrii-bodnar merged 1 commit intolingui:mainfrom
Conversation
|
@YarnSphere is attempting to deploy a commit to the Crowdin Team on Vercel. A member of the Team first needs to authorize it. |
| t: I18n["_"] = this._.bind(this) | ||
|
|
||
| date(value: string | Date, format?: Intl.DateTimeFormatOptions): string { | ||
| date(value?: string | DateTimeFormatValue, format?: Intl.DateTimeFormatOptions): string { |
There was a problem hiding this comment.
Why value here become optional?
Does the DateTimeFormatValue already include a string?
There was a problem hiding this comment.
Why value here become optional?
I made it optional simply because it's supported, when the value is undefined, it returns the current date. Try out: new Intl.DateTimeFormat().format() (should return the current date), which should be similar to calling i18n.date().
Let me know if you'd prefer it to not be optional, but note that it would still work in JavaScript, it simply wouldn't type check anymore.
Does the
DateTimeFormatValuealready include astring?
No, see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/format#parameters (yet it does support numbers, even though MDN doesn't mention it).
By the way, you can test the changes in the Lingui repository itself just be changing the "target": "es2017" to something more modern in the root tsconfig.json (e.g., "target": "ESNext").
There was a problem hiding this comment.
Just to give you a little bit more context: with ESNext, the latest Intl.(DateTime|Number)Format.prototype.format types are the following (omitting all non-relevant definitions):
declare namespace Intl {
// Defined in `lib.es5.d.ts`:
interface DateTimeFormat {
format(date?: Date | number): string;
// ...
}
// Defined in `lib.es2023.intl.d.ts`:
type StringNumericLiteral = `${number}` | "Infinity" | "-Infinity" | "+Infinity";
interface NumberFormat {
format(value: number | bigint | StringNumericLiteral): string;
// ...
}
}I believe Temporal isn't yet supported (this will most likely change in the future), and this PR should make it so that Lingui supports Temporal as soon as the TypeScript ES lib types are updated to support it.
Also, note that the date in DateTimeFormat.prototype.format is optional (as opposed to the NumberFormat.prototype.format value), which is why I changed Lingui's i18n.date's value to be optional. Hope this helps. :)
Modern ES versions support more types of values when calling `Intl.(DateTime|Number)Format.prototype.format`: - Datetime formatting nowadays supports `Temporal` values (see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/format#parameters); - Number formatting nowadays supports `BigInt`s and strings (see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/format#parameters). This commit uses the types defined by TypeScript directly, rather than ad hoc types, which depend on the TypeScript target configured by the user.
|
Fixed Prettier issues in latest push. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #2359 +/- ##
==========================================
- Coverage 77.05% 76.27% -0.78%
==========================================
Files 84 99 +15
Lines 2157 2651 +494
Branches 555 692 +137
==========================================
+ Hits 1662 2022 +360
- Misses 382 505 +123
- Partials 113 124 +11 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Description
Modern ES versions support more types of values when calling
Intl.(DateTime|Number)Format.prototype.formatthan those allowed by Lingui:Temporalvalues (see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/format#parameters);BigInts and strings (see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/format#parameters).This commit uses the types defined by TypeScript directly, rather than ad hoc types, which depend on the TypeScript target configured by the user, allowing users to pass to
dateandnumberany values that are supported byIntl.(DateTime|Number)Format.prototype.format.Example of code that currently fails to type check (even though it runs correctly):
Types of changes
Fixes # (issue)
Checklist