Skip to content

Commit ef5fc0c

Browse files
committed
fix: update types for RTF.p.formatToParts() result
This commit updates the type of `RelativeTimeFormatPart` to clarify that the `unit` prop is always singular, unlike the plural or singular values that are accepted as inputs. This also changes `RelativeTimeFormatPart` to be a discriminated union type because the `unit` prop is only present if the `type` prop's value is not "literal". Fixes microsoft#46245
1 parent e88e596 commit ef5fc0c

File tree

2 files changed

+61
-10
lines changed

2 files changed

+61
-10
lines changed

lib/lib.es2020.intl.d.ts

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,28 @@ declare namespace Intl {
4646
| "second" | "seconds"
4747
;
4848

49+
/**
50+
* Value of the `unit` property in objects returned by
51+
* `Intl.RelativeTimeFormat.prototype.formatToParts()`. `formatToParts` and
52+
* `format` methods accept either singular or plural unit names as input,
53+
* but `formatToParts` only outputs singular (e.g. "day") not plural (e.g.
54+
* "days").
55+
*
56+
* [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/RelativeTimeFormat/formatToParts#Using_formatToParts).
57+
*
58+
* [Specification](https://tc39.es/ecma402/#sec-singularrelativetimeunit).
59+
*/
60+
type RelativeTimeFormatUnitSingular =
61+
| "year"
62+
| "quarter"
63+
| "month"
64+
| "week"
65+
| "day"
66+
| "hour"
67+
| "minute"
68+
| "second"
69+
;
70+
4971
/**
5072
* The locale matching algorithm to use.
5173
*
@@ -111,11 +133,16 @@ declare namespace Intl {
111133
*
112134
* [Specification](https://tc39.es/ecma402/#sec-FormatRelativeTimeToParts).
113135
*/
114-
interface RelativeTimeFormatPart {
115-
type: string;
116-
value: string;
117-
unit?: RelativeTimeFormatUnit;
118-
}
136+
type RelativeTimeFormatPart =
137+
| {
138+
type: "literal";
139+
value: string;
140+
}
141+
| {
142+
type: Exclude<NumberFormatPartTypes, "literal">;
143+
value: string;
144+
unit: RelativeTimeFormatUnitSingular;
145+
};
119146

120147
interface RelativeTimeFormat {
121148
/**

src/lib/es2020.intl.d.ts

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,25 @@ declare namespace Intl {
3030
| "second"
3131
| "seconds";
3232

33+
/**
34+
* Value of the `unit` property in objects returned by
35+
* `Intl.RelativeTimeFormat.prototype.formatToParts()`. `formatToParts` and
36+
* `format` methods accept either singular or plural unit names as input,
37+
* but `formatToParts` only outputs singular (e.g. "day") not plural (e.g.
38+
* "days").
39+
*
40+
* [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/RelativeTimeFormat/formatToParts#Using_formatToParts).
41+
*/
42+
type RelativeTimeFormatUnitSingular =
43+
| "year"
44+
| "quarter"
45+
| "month"
46+
| "week"
47+
| "day"
48+
| "hour"
49+
| "minute"
50+
| "second";
51+
3352
/**
3453
* The locale matching algorithm to use.
3554
*
@@ -93,11 +112,16 @@ declare namespace Intl {
93112
*
94113
* [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/RelativeTimeFormat/formatToParts#Using_formatToParts).
95114
*/
96-
interface RelativeTimeFormatPart {
97-
type: string;
98-
value: string;
99-
unit?: RelativeTimeFormatUnit;
100-
}
115+
type RelativeTimeFormatPart =
116+
| {
117+
type: "literal";
118+
value: string;
119+
}
120+
| {
121+
type: Exclude<NumberFormatPartTypes, "literal">;
122+
value: string;
123+
unit: RelativeTimeFormatUnitSingular;
124+
};
101125

102126
interface RelativeTimeFormat {
103127
/**

0 commit comments

Comments
 (0)