From 150ccd134264aabc9a3eb324567dc1ea11aa0037 Mon Sep 17 00:00:00 2001 From: Justin Grant Date: Mon, 15 Nov 2021 16:11:59 -0800 Subject: [PATCH] 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 #46245 --- src/lib/es2020.intl.d.ts | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/src/lib/es2020.intl.d.ts b/src/lib/es2020.intl.d.ts index f351ee3c0cee6..11fc2e6a50c2c 100644 --- a/src/lib/es2020.intl.d.ts +++ b/src/lib/es2020.intl.d.ts @@ -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. * @@ -93,11 +112,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; + value: string; + unit: RelativeTimeFormatUnitSingular; + }; interface RelativeTimeFormat { /**