Skip to content

Commit c1f2800

Browse files
authored
fix: improve type of values accepted by date/time/number formats (#2359)
1 parent 197bcd9 commit c1f2800

File tree

4 files changed

+30
-13
lines changed

4 files changed

+30
-13
lines changed

packages/core/src/formats.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,14 @@ function normalizeLocales(locales: Locales): string[] {
1313

1414
export type DateTimeFormatSize = "short" | "default" | "long" | "full"
1515

16+
// These types vary depending on the TypeScript target (e.g., modern ES versions
17+
// support passing a bigint or string to `Intl.NumberFormat.prototype.format`)
18+
export type DateTimeFormatValue = Parameters<Intl.DateTimeFormat["format"]>[0]
19+
export type NumberFormatValue = Parameters<Intl.NumberFormat["format"]>[0]
20+
1621
export function date(
1722
locales: Locales,
18-
value: string | Date,
23+
value: string | DateTimeFormatValue,
1924
format?: Intl.DateTimeFormatOptions | DateTimeFormatSize
2025
): string {
2126
const _locales = normalizeLocales(locales)
@@ -60,7 +65,7 @@ export function date(
6065

6166
export function time(
6267
locales: Locales,
63-
value: string | Date,
68+
value: string | DateTimeFormatValue,
6469
format?: Intl.DateTimeFormatOptions | DateTimeFormatSize
6570
): string {
6671
let o: Intl.DateTimeFormatOptions
@@ -95,7 +100,7 @@ export function time(
95100

96101
export function number(
97102
locales: Locales,
98-
value: number,
103+
value: NumberFormatValue,
99104
format?: Intl.NumberFormatOptions
100105
): string {
101106
const _locales = normalizeLocales(locales)

packages/core/src/i18n.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import { interpolate } from "./interpolate"
22
import { isString, isFunction } from "./essentials"
3-
import { date, defaultLocale, number } from "./formats"
3+
import {
4+
date,
5+
type DateTimeFormatValue,
6+
defaultLocale,
7+
number,
8+
type NumberFormatValue,
9+
} from "./formats"
410
import { EventEmitter } from "./eventEmitter"
511
import { compileMessage } from "@lingui/message-utils/compileMessage"
612
import type { CompiledMessage } from "@lingui/message-utils/compileMessage"
@@ -312,11 +318,14 @@ Please compile your catalog first.
312318
*/
313319
t: I18n["_"] = this._.bind(this)
314320

315-
date(value: string | Date, format?: Intl.DateTimeFormatOptions): string {
321+
date(
322+
value?: string | DateTimeFormatValue,
323+
format?: Intl.DateTimeFormatOptions
324+
): string {
316325
return date(this._locales || this._locale, value, format)
317326
}
318327

319-
number(value: number, format?: Intl.NumberFormatOptions): string {
328+
number(value: NumberFormatValue, format?: Intl.NumberFormatOptions): string {
320329
return number(this._locales || this._locale, value, format)
321330
}
322331
}

packages/core/src/interpolate.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ import { CompiledMessage, Formats, Locales, Values } from "./i18n"
22
import {
33
date,
44
DateTimeFormatSize,
5+
type DateTimeFormatValue,
56
number,
7+
type NumberFormatValue,
68
plural,
79
type PluralOptions,
810
time,
@@ -51,7 +53,7 @@ const getDefaultFormats = (
5153
select: selectFormatter,
5254

5355
number: (
54-
value: number,
56+
value: NumberFormatValue,
5557
format: string | Intl.NumberFormatOptions
5658
): string =>
5759
number(
@@ -61,11 +63,11 @@ const getDefaultFormats = (
6163
),
6264

6365
date: (
64-
value: string,
66+
value: string | DateTimeFormatValue,
6567
format: Intl.DateTimeFormatOptions | string
6668
): string =>
6769
date(locales, value, style(format) || (format as DateTimeFormatSize)),
68-
time: (value: string, format: string): string =>
70+
time: (value: string | DateTimeFormatValue, format: string): string =>
6971
time(locales, value, style(format) || (format as DateTimeFormatSize)),
7072
} as const
7173
}

website/docs/ref/core.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -220,11 +220,12 @@ import { i18n } from "@lingui/core";
220220
i18n.t({ id: "Hello" });
221221
```
222222

223-
### `i18n.date(value: string | Date[, format: Intl.DateTimeFormatOptions])` {#i18n.date}
223+
### `i18n.date(value: string | Date | number[, format: Intl.DateTimeFormatOptions])` {#i18n.date}
224224

225225
Format a date using the conventional format for the active language.
226226

227-
- `date`: a `Date` object to be formatted. When `date` is a string, the `Date` object is created using `new Date(date)`.
227+
- `value`: the date to be formatted, as accepted by [`Intl.DateTimeFormat.prototype.format`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/format#parameters).
228+
When `value` is a string, a `Date` object is created using `new Date(date)`.
228229
- `format`: an optional object that is passed to the `options` argument of the [`Intl.DateTimeFormat`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat) constructor. This allows for customization of the date formatting.
229230

230231
```ts
@@ -247,11 +248,11 @@ i18n.date(d);
247248
// Returns "23. 7. 2021"
248249
```
249250

250-
### `i18n.number(value: number[, format: Intl.NumberFormatOptions])` {#i18n.number}
251+
### `i18n.number(value: number | bigint | Intl.StringNumericLiteral[, format: Intl.NumberFormatOptions])` {#i18n.number}
251252

252253
Format a number using the conventional format for the active language.
253254

254-
- `number`: a number to be formatted.
255+
- `value`: the number to be formatted, as accepted by [`Intl.NumberFormat.prototype.format`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/format#parameters).
255256
- `format`: an optional object that is passed to the `options` argument of the [`Intl.NumberFormat`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat) constructor. This allows for customization of the date formatting.
256257

257258
```ts

0 commit comments

Comments
 (0)