Skip to content

Commit 150ccd1

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 150ccd1

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

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)