mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-04 21:53:42 -06:00
fix: update types for RTF.p.formatToParts() result (#46508)
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 #46245
This commit is contained in:
parent
6ee549075b
commit
8bf45a4f93
34
src/lib/es2020.intl.d.ts
vendored
34
src/lib/es2020.intl.d.ts
vendored
@ -30,6 +30,25 @@ declare namespace Intl {
|
||||
| "second"
|
||||
| "seconds";
|
||||
|
||||
/**
|
||||
* Value of the `unit` property in objects returned by
|
||||
* `Intl.RelativeTimeFormat.prototype.formatToParts()`. `formatToParts` and
|
||||
* `format` methods accept either singular or plural unit names as input,
|
||||
* but `formatToParts` only outputs singular (e.g. "day") not plural (e.g.
|
||||
* "days").
|
||||
*
|
||||
* [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/RelativeTimeFormat/formatToParts#Using_formatToParts).
|
||||
*/
|
||||
type RelativeTimeFormatUnitSingular =
|
||||
| "year"
|
||||
| "quarter"
|
||||
| "month"
|
||||
| "week"
|
||||
| "day"
|
||||
| "hour"
|
||||
| "minute"
|
||||
| "second";
|
||||
|
||||
/**
|
||||
* The locale matching algorithm to use.
|
||||
*
|
||||
@ -100,11 +119,16 @@ declare namespace Intl {
|
||||
*
|
||||
* [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/RelativeTimeFormat/formatToParts#Using_formatToParts).
|
||||
*/
|
||||
interface RelativeTimeFormatPart {
|
||||
type: string;
|
||||
value: string;
|
||||
unit?: RelativeTimeFormatUnit;
|
||||
}
|
||||
type RelativeTimeFormatPart =
|
||||
| {
|
||||
type: "literal";
|
||||
value: string;
|
||||
}
|
||||
| {
|
||||
type: Exclude<NumberFormatPartTypes, "literal">;
|
||||
value: string;
|
||||
unit: RelativeTimeFormatUnitSingular;
|
||||
};
|
||||
|
||||
interface RelativeTimeFormat {
|
||||
/**
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user