fix: fix RelativeTimeFormat type definition (#39661)

* fix: fix RelativeTimeFormat type definition
Changes:
1. Change BCP47LanguageTag to UnicodeBCP47LocaleIdentifier: Those mean 2
different things. BCP47LangTag allows _ as separator while UTS35
doesn't. It also allows grandfathered locales and UTS35 doesn't.

2. Combine RelativeTimeFormat interface and const declaration into a
single class. The old way of declaring as `interface` & `const` permits
calling `Intl.RelativeTimeFormat` without `new` which is no longer
possible after `Intl.DateTimeFormat` & `Intl.NumberFormat`. The spec
explicitly forbids it in
http://ecma-international.org/ecma-402/7.0/index.html#relativetimeformat-objects
where:

> If NewTarget is undefined, throw a TypeError exception.

Intl.RelativeTimeFormat is also extensible per spec. This is closer to a
`class` than the current declaration.

* address feedbacks
This commit is contained in:
Long Ho 2021-02-16 18:56:56 -05:00 committed by GitHub
parent 4d1dfab9e0
commit 3910d9ede2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,13 +1,12 @@
declare namespace Intl {
/**
* [BCP 47 language tag](http://tools.ietf.org/html/rfc5646) definition.
* [Unicode BCP 47 Locale Identifiers](https://unicode.org/reports/tr35/#Unicode_Language_and_Locale_Identifiers) definition.
*
* [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl#locales_argument).
*
* [Wikipedia](https://en.wikipedia.org/wiki/IETF_language_tag).
*/
type BCP47LanguageTag = string;
type UnicodeBCP47LocaleIdentifier = string;
/**
* Unit to use in the relative time internationalized message.
@ -311,7 +310,7 @@ declare namespace Intl {
* [Specification](https://tc39.es/ecma402/#table-relativetimeformat-resolvedoptions-properties)
*/
interface ResolvedRelativeTimeFormatOptions {
locale: BCP47LanguageTag;
locale: UnicodeBCP47LocaleIdentifier;
style: RelativeTimeFormatStyle;
numeric: RelativeTimeFormatNumeric;
numberingSystem: string;
@ -454,7 +453,7 @@ declare namespace Intl {
* [Specification](https://tc39.es/ecma402/#sec-intl-relativetimeformat-constructor).
*/
new(
locales?: BCP47LanguageTag | BCP47LanguageTag[],
locales?: UnicodeBCP47LocaleIdentifier | UnicodeBCP47LocaleIdentifier[],
options?: RelativeTimeFormatOptions,
): RelativeTimeFormat;
@ -490,9 +489,9 @@ declare namespace Intl {
* [Specification](https://tc39.es/ecma402/#sec-Intl.RelativeTimeFormat.supportedLocalesOf).
*/
supportedLocalesOf(
locales: BCP47LanguageTag | BCP47LanguageTag[],
locales?: UnicodeBCP47LocaleIdentifier | UnicodeBCP47LocaleIdentifier[],
options?: RelativeTimeFormatOptions,
): BCP47LanguageTag[];
): UnicodeBCP47LocaleIdentifier[];
};
interface NumberFormatOptions {