Skip to content

Commit 29b9b1d

Browse files
committed
fix Intl.DurationFormat types
1 parent ff0e67c commit 29b9b1d

File tree

2 files changed

+73
-123
lines changed

2 files changed

+73
-123
lines changed

src/lib/es2025.intl.d.ts

Lines changed: 65 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -1,152 +1,108 @@
1-
declare namespace Intl {
2-
type DurationTimeFormatLocaleMatcher = "lookup" | "best fit";
3-
/**
4-
* Value of the `unit` property in duration objects
5-
*
6-
* [MDN](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Intl/DurationFormat/format#duration).
7-
*/
8-
type DurationTimeFormatUnit =
9-
| "years"
10-
| "months"
11-
| "weeks"
12-
| "days"
13-
| "hours"
14-
| "minutes"
15-
| "seconds"
16-
| "milliseconds"
17-
| "microseconds"
18-
| "nanoseconds";
19-
20-
/**
21-
* The style of the formatted duration.
22-
*
23-
* [MDN](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Intl/DurationFormat/DurationFormat#style).
24-
*/
25-
type DurationFormatStyle = "long" | "short" | "narrow" | "digital";
26-
27-
type DurationFormatUnitSingular =
28-
| "year"
29-
| "quarter"
30-
| "month"
31-
| "week"
32-
| "day"
33-
| "hour"
34-
| "minute"
35-
| "second";
1+
/// <reference lib="es2018.intl" />
362

3+
declare namespace Intl {
374
/**
385
* An object representing the relative time format in parts
396
* that can be used for custom locale-aware formatting.
407
*
41-
* [MDN](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Intl/DurationFormat/formatToParts#examples).
8+
* [MDN](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Intl/DurationFormat/formatToParts).
429
*/
4310
type DurationFormatPart =
4411
| {
4512
type: "literal";
4613
value: string;
14+
unit?: "year" | "month" | "week" | "day" | "hour" | "minute" | "second" | "milliseconds" | "microseconds" | "nanoseconds";
4715
}
4816
| {
4917
type: Exclude<NumberFormatPartTypes, "literal">;
5018
value: string;
51-
unit: DurationFormatUnitSingular;
19+
unit: "year" | "month" | "week" | "day" | "hour" | "minute" | "second" | "milliseconds" | "microseconds" | "nanoseconds";
5220
};
5321

54-
type DurationFormatOption =
55-
| "long"
56-
| "short"
57-
| "narrow"
58-
| "numeric"
59-
| "2-digit";
60-
61-
type DurationFormatDisplayOption = "always" | "auto";
62-
6322
/**
64-
* Number of how many fractional second digits to display in the output.
23+
* An object with some or all properties of the `Intl.DurationFormat` constructor `options` parameter.
6524
*
66-
* [MDN](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Intl/DurationFormat/DurationFormat#fractionaldigits).
25+
* [MDN](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Intl/DurationFormat/DurationFormat#parameters)
6726
*/
68-
type fractionalDigitsOption = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9;
69-
70-
interface ResolvedDurationFormatOptions {
71-
locale?: UnicodeBCP47LocaleIdentifier;
72-
numberingSystem?: DateTimeFormatOptions["numberingSystem"];
73-
style?: DurationFormatStyle;
74-
years?: Exclude<DurationFormatOption, "numeric" | "2-digit">;
75-
yearsDisplay?: DurationFormatDisplayOption;
76-
months?: Exclude<DurationFormatOption, "numeric" | "2-digit">;
77-
monthsDisplay?: DurationFormatDisplayOption;
78-
weeks?: Exclude<DurationFormatOption, "numeric" | "2-digit">;
79-
weeksDisplay?: DurationFormatDisplayOption;
80-
days?: Exclude<DurationFormatOption, "numeric" | "2-digit">;
81-
daysDisplay?: DurationFormatDisplayOption;
82-
hours?: DurationFormatOptions;
83-
hoursDisplay?: DurationFormatDisplayOption;
84-
minutes?: DurationFormatOptions;
85-
minutesDisplay?: DurationFormatDisplayOption;
86-
seconds?: DurationFormatOptions;
87-
secondsDisplay?: DurationFormatDisplayOption;
88-
milliseconds?: DurationFormatOptions;
89-
millisecondsDisplay?: DurationFormatDisplayOption;
90-
microseconds?: DurationFormatOptions;
91-
microsecondsDisplay?: DurationFormatDisplayOption;
92-
nanoseconds?: DurationFormatOptions;
93-
nanosecondsDisplay?: DurationFormatDisplayOption;
94-
fractionalDigits?: fractionalDigitsOption;
95-
}
96-
9727
interface DurationFormatOptions {
98-
localeMatcher?: DurationTimeFormatLocaleMatcher;
99-
numberingSystem?: DateTimeFormatOptions["numberingSystem"];
100-
style?: DurationFormatStyle;
101-
years?: Exclude<DurationFormatOption, "numeric" | "2-digit">;
102-
yearsDisplay?: DurationFormatDisplayOption;
103-
months?: Exclude<DurationFormatOption, "numeric" | "2-digit">;
104-
monthsDisplay?: DurationFormatDisplayOption;
105-
weeks?: Exclude<DurationFormatOption, "numeric" | "2-digit">;
106-
weeksDisplay?: DurationFormatDisplayOption;
107-
days?: Exclude<DurationFormatOption, "numeric" | "2-digit">;
108-
daysDisplay?: DurationFormatDisplayOption;
109-
hours?: DurationFormatOption;
110-
hoursDisplay?: DurationFormatDisplayOption;
111-
minutes?: DurationFormatOption;
112-
minutesDisplay?: DurationFormatDisplayOption;
113-
seconds?: DurationFormatOption;
114-
secondsDisplay?: DurationFormatDisplayOption;
115-
milliseconds?: DurationFormatOption;
116-
millisecondsDisplay?: DurationFormatDisplayOption;
117-
microseconds?: DurationFormatOption;
118-
microsecondsDisplay?: DurationFormatDisplayOption;
119-
nanoseconds?: DurationFormatOption;
120-
nanosecondsDisplay?: DurationFormatDisplayOption;
121-
fractionalDigits?: fractionalDigitsOption;
28+
localeMatcher?: "lookup" | "best fit" | undefined;
29+
numberingSystem?: string | undefined;
30+
style?: "long" | "short" | "narrow" | "digital" | undefined;
31+
years?: "long" | "short" | "narrow" | undefined;
32+
yearsDisplay?: "always" | "auto" | undefined;
33+
months?: "long" | "short" | "narrow" | undefined;
34+
monthsDisplay?: "always" | "auto" | undefined;
35+
weeks?: "long" | "short" | "narrow" | undefined;
36+
weeksDisplay?: "always" | "auto" | undefined;
37+
days?: "long" | "short" | "narrow" | undefined;
38+
daysDisplay?: "always" | "auto" | undefined;
39+
hours?: "long" | "short" | "narrow" | "numeric" | "2-digit" | undefined;
40+
hoursDisplay?: "always" | "auto" | undefined;
41+
minutes?: "long" | "short" | "narrow" | "numeric" | "2-digit" | undefined;
42+
minutesDisplay?: "always" | "auto" | undefined;
43+
seconds?: "long" | "short" | "narrow" | "numeric" | "2-digit" | undefined;
44+
secondsDisplay?: "always" | "auto" | undefined;
45+
milliseconds?: "long" | "short" | "narrow" | "numeric" | undefined;
46+
millisecondsDisplay?: "always" | "auto" | undefined;
47+
microseconds?: "long" | "short" | "narrow" | "numeric" | undefined;
48+
microsecondsDisplay?: "always" | "auto" | undefined;
49+
nanoseconds?: "long" | "short" | "narrow" | "numeric" | undefined;
50+
nanosecondsDisplay?: "always" | "auto" | undefined;
51+
fractionalDigits?: 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | undefined;
12252
}
12353

12454
/**
125-
* The duration object to be formatted
55+
* The Intl.DurationFormat object enables language-sensitive duration formatting.
12656
*
127-
* [MDN](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Intl/DurationFormat/format#duration).
57+
* [MDN](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Intl/DurationFormat)
12858
*/
129-
type DurationType = Partial<Record<DurationTimeFormatUnit, number>>;
130-
13159
interface DurationFormat {
13260
/**
13361
* @param duration The duration object to be formatted. It should include some or all of the following properties: months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds.
13462
*
13563
* [MDN](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Intl/DurationFormat/format).
13664
*/
137-
format(duration: DurationType): string;
65+
format(duration: Partial<Record<"years" | "months" | "weeks" | "days" | "hours" | "minutes" | "seconds" | "milliseconds" | "microseconds" | "nanoseconds", number>>): string;
13866
/**
13967
* @param duration The duration object to be formatted. It should include some or all of the following properties: months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds.
14068
*
14169
* [MDN](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Intl/DurationFormat/formatToParts).
14270
*/
143-
formatToParts(duration: DurationType): DurationFormatPart[];
71+
formatToParts(duration: Partial<Record<"years" | "months" | "weeks" | "days" | "hours" | "minutes" | "seconds" | "milliseconds" | "microseconds" | "nanoseconds", number>>): DurationFormatPart[];
14472
/**
14573
* [MDN](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Intl/DurationFormat/resolvedOptions).
14674
*/
14775
resolvedOptions(): ResolvedDurationFormatOptions;
14876
}
14977

78+
79+
interface ResolvedDurationFormatOptions {
80+
locale: string;
81+
numberingSystem: string;
82+
style: "long" | "short" | "narrow" | "digital";
83+
years: "long" | "short" | "narrow";
84+
yearsDisplay: "always" | "auto";
85+
months: "long" | "short" | "narrow";
86+
monthsDisplay: "always" | "auto";
87+
weeks: "long" | "short" | "narrow";
88+
weeksDisplay: "always" | "auto";
89+
days: "long" | "short" | "narrow";
90+
daysDisplay: "always" | "auto";
91+
hours: "long" | "short" | "narrow" | "numeric" | "2-digit";
92+
hoursDisplay: "always" | "auto";
93+
minutes: "long" | "short" | "narrow" | "numeric" | "2-digit";
94+
minutesDisplay: "always" | "auto";
95+
seconds: "long" | "short" | "narrow" | "numeric" | "2-digit";
96+
secondsDisplay: "always" | "auto";
97+
milliseconds: "long" | "short" | "narrow" | "numeric";
98+
millisecondsDisplay: "always" | "auto";
99+
microseconds: "long" | "short" | "narrow" | "numeric";
100+
microsecondsDisplay: "always" | "auto";
101+
nanoseconds: "long" | "short" | "narrow" | "numeric";
102+
nanosecondsDisplay: "always" | "auto";
103+
fractionalDigits?: 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9;
104+
}
105+
150106
const DurationFormat: {
151107
prototype: DurationFormat;
152108

@@ -159,10 +115,7 @@ declare namespace Intl {
159115
*
160116
* [MDN](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Intl/DurationFormat/DurationFormat).
161117
*/
162-
new (
163-
locales?: LocalesArgument,
164-
options?: DurationFormatOptions,
165-
): DurationFormat;
118+
new (locales?: LocalesArgument, options?: DurationFormatOptions): DurationFormat;
166119

167120
/**
168121
* Returns an array containing those of the provided locales that are supported in display names without having to fall back to the runtime's default locale.
@@ -177,9 +130,6 @@ declare namespace Intl {
177130
*
178131
* [MDN](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Intl/DurationFormat/supportedLocalesOf).
179132
*/
180-
supportedLocalesOf(
181-
locales?: LocalesArgument,
182-
options?: { localeMatcher?: DurationTimeFormatLocaleMatcher; },
183-
): UnicodeBCP47LocaleIdentifier[];
133+
supportedLocalesOf(locales?: LocalesArgument, options?: Pick<DurationFormatOptions, "localeMatcher">): UnicodeBCP47LocaleIdentifier[];
184134
};
185135
}

tests/baselines/reference/intlDurationFormat.types

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,20 @@
44
new Intl.DurationFormat('en').format({
55
>new Intl.DurationFormat('en').format({ years: 1, hours: 20, minutes: 15, seconds: 35}) : string
66
> : ^^^^^^
7-
>new Intl.DurationFormat('en').format : (duration: Intl.DurationType) => string
8-
> : ^ ^^^^^^^^^^^^^^^^^^^^^^^^
7+
>new Intl.DurationFormat('en').format : (duration: Partial<Record<"years" | "months" | "weeks" | "days" | "hours" | "minutes" | "seconds" | "milliseconds" | "microseconds" | "nanoseconds", number>>) => string
8+
> : ^ ^^ ^^^^^
99
>new Intl.DurationFormat('en') : Intl.DurationFormat
1010
> : ^^^^^^^^^^^^^^^^^^^
11-
>Intl.DurationFormat : { new (locales?: Intl.LocalesArgument, options?: Intl.DurationFormatOptions): Intl.DurationFormat; prototype: Intl.DurationFormat; supportedLocalesOf(locales?: Intl.LocalesArgument, options?: { localeMatcher?: Intl.DurationTimeFormatLocaleMatcher; }): Intl.UnicodeBCP47LocaleIdentifier[]; }
12-
> : ^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^
11+
>Intl.DurationFormat : { new (locales?: Intl.LocalesArgument, options?: Intl.DurationFormatOptions): Intl.DurationFormat; prototype: Intl.DurationFormat; supportedLocalesOf(locales?: Intl.LocalesArgument, options?: Pick<Intl.DurationFormatOptions, "localeMatcher">): Intl.UnicodeBCP47LocaleIdentifier[]; }
12+
> : ^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^
1313
>Intl : typeof Intl
1414
> : ^^^^^^^^^^^
15-
>DurationFormat : { new (locales?: Intl.LocalesArgument, options?: Intl.DurationFormatOptions): Intl.DurationFormat; prototype: Intl.DurationFormat; supportedLocalesOf(locales?: Intl.LocalesArgument, options?: { localeMatcher?: Intl.DurationTimeFormatLocaleMatcher; }): Intl.UnicodeBCP47LocaleIdentifier[]; }
16-
> : ^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^
15+
>DurationFormat : { new (locales?: Intl.LocalesArgument, options?: Intl.DurationFormatOptions): Intl.DurationFormat; prototype: Intl.DurationFormat; supportedLocalesOf(locales?: Intl.LocalesArgument, options?: Pick<Intl.DurationFormatOptions, "localeMatcher">): Intl.UnicodeBCP47LocaleIdentifier[]; }
16+
> : ^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^
1717
>'en' : "en"
1818
> : ^^^^
19-
>format : (duration: Intl.DurationType) => string
20-
> : ^ ^^^^^^^^^^^^^^^^^^^^^^^^
19+
>format : (duration: Partial<Record<"years" | "months" | "weeks" | "days" | "hours" | "minutes" | "seconds" | "milliseconds" | "microseconds" | "nanoseconds", number>>) => string
20+
> : ^ ^^ ^^^^^
2121
>{ years: 1, hours: 20, minutes: 15, seconds: 35} : { years: number; hours: number; minutes: number; seconds: number; }
2222
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2323

0 commit comments

Comments
 (0)