mirror of
https://github.com/microsoft/TypeScript.git
synced 2025-11-18 17:21:48 +00:00
Require tag parameter and allow Locale object as argument to Intl.Locale() constructor (#48019)
* mark tag as required, and can accept another Locale object * add tests and baselines * update baselines
This commit is contained in:
Vendored
+1
-1
@@ -288,7 +288,7 @@ declare namespace Intl {
|
||||
* [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale).
|
||||
*/
|
||||
const Locale: {
|
||||
new (tag?: BCP47LanguageTag, options?: LocaleOptions): Locale;
|
||||
new (tag: BCP47LanguageTag | Locale, options?: LocaleOptions): Locale;
|
||||
};
|
||||
|
||||
interface DisplayNamesOptions {
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
tests/cases/conformance/es2020/es2020IntlAPIs.ts(45,1): error TS2554: Expected 1-2 arguments, but got 0.
|
||||
|
||||
|
||||
==== tests/cases/conformance/es2020/es2020IntlAPIs.ts (1 errors) ====
|
||||
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl#Locale_identification_and_negotiation
|
||||
const count = 26254.39;
|
||||
const date = new Date("2012-05-24");
|
||||
|
||||
function log(locale: string) {
|
||||
console.log(
|
||||
`${new Intl.DateTimeFormat(locale).format(date)} ${new Intl.NumberFormat(locale).format(count)}`
|
||||
);
|
||||
}
|
||||
|
||||
log("en-US");
|
||||
// expected output: 5/24/2012 26,254.39
|
||||
|
||||
log("de-DE");
|
||||
// expected output: 24.5.2012 26.254,39
|
||||
|
||||
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/RelativeTimeFormat
|
||||
const rtf1 = new Intl.RelativeTimeFormat('en', { style: 'narrow' });
|
||||
|
||||
console.log(rtf1.format(3, 'quarter'));
|
||||
//expected output: "in 3 qtrs."
|
||||
|
||||
console.log(rtf1.format(-1, 'day'));
|
||||
//expected output: "1 day ago"
|
||||
|
||||
const rtf2 = new Intl.RelativeTimeFormat('es', { numeric: 'auto' });
|
||||
|
||||
console.log(rtf2.format(2, 'day'));
|
||||
//expected output: "pasado mañana"
|
||||
|
||||
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DisplayNames
|
||||
const regionNamesInEnglish = new Intl.DisplayNames(['en'], { type: 'region' });
|
||||
const regionNamesInTraditionalChinese = new Intl.DisplayNames(['zh-Hant'], { type: 'region' });
|
||||
|
||||
console.log(regionNamesInEnglish.of('US'));
|
||||
// expected output: "United States"
|
||||
|
||||
console.log(regionNamesInTraditionalChinese.of('US'));
|
||||
// expected output: "美國"
|
||||
|
||||
const locales1 = ['ban', 'id-u-co-pinyin', 'de-ID'];
|
||||
const options1 = { localeMatcher: 'lookup' } as const;
|
||||
console.log(Intl.DisplayNames.supportedLocalesOf(locales1, options1).join(', '));
|
||||
|
||||
new Intl.Locale(); // should error
|
||||
~~~~~~~~~~~~~~~~~
|
||||
!!! error TS2554: Expected 1-2 arguments, but got 0.
|
||||
!!! related TS6210 /.ts/lib.es2020.intl.d.ts:311:14: An argument for 'tag' was not provided.
|
||||
new Intl.Locale(new Intl.Locale('en-US'));
|
||||
@@ -41,7 +41,10 @@ console.log(regionNamesInTraditionalChinese.of('US'));
|
||||
|
||||
const locales1 = ['ban', 'id-u-co-pinyin', 'de-ID'];
|
||||
const options1 = { localeMatcher: 'lookup' } as const;
|
||||
console.log(Intl.DisplayNames.supportedLocalesOf(locales1, options1).join(', '));
|
||||
console.log(Intl.DisplayNames.supportedLocalesOf(locales1, options1).join(', '));
|
||||
|
||||
new Intl.Locale(); // should error
|
||||
new Intl.Locale(new Intl.Locale('en-US'));
|
||||
|
||||
//// [es2020IntlAPIs.js]
|
||||
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl#Locale_identification_and_negotiation
|
||||
@@ -73,3 +76,5 @@ console.log(regionNamesInTraditionalChinese.of('US'));
|
||||
const locales1 = ['ban', 'id-u-co-pinyin', 'de-ID'];
|
||||
const options1 = { localeMatcher: 'lookup' };
|
||||
console.log(Intl.DisplayNames.supportedLocalesOf(locales1, options1).join(', '));
|
||||
new Intl.Locale(); // should error
|
||||
new Intl.Locale(new Intl.Locale('en-US'));
|
||||
|
||||
@@ -147,3 +147,16 @@ console.log(Intl.DisplayNames.supportedLocalesOf(locales1, options1).join(', '))
|
||||
>options1 : Symbol(options1, Decl(es2020IntlAPIs.ts, 41, 5))
|
||||
>join : Symbol(Array.join, Decl(lib.es5.d.ts, --, --))
|
||||
|
||||
new Intl.Locale(); // should error
|
||||
>Intl.Locale : Symbol(Intl.Locale, Decl(lib.es2020.intl.d.ts, --, --), Decl(lib.es2020.intl.d.ts, --, --))
|
||||
>Intl : Symbol(Intl, Decl(lib.es5.d.ts, --, --), Decl(lib.es2017.intl.d.ts, --, --), Decl(lib.es2018.intl.d.ts, --, --), Decl(lib.es2020.bigint.d.ts, --, --), Decl(lib.es2020.intl.d.ts, --, --))
|
||||
>Locale : Symbol(Intl.Locale, Decl(lib.es2020.intl.d.ts, --, --), Decl(lib.es2020.intl.d.ts, --, --))
|
||||
|
||||
new Intl.Locale(new Intl.Locale('en-US'));
|
||||
>Intl.Locale : Symbol(Intl.Locale, Decl(lib.es2020.intl.d.ts, --, --), Decl(lib.es2020.intl.d.ts, --, --))
|
||||
>Intl : Symbol(Intl, Decl(lib.es5.d.ts, --, --), Decl(lib.es2017.intl.d.ts, --, --), Decl(lib.es2018.intl.d.ts, --, --), Decl(lib.es2020.bigint.d.ts, --, --), Decl(lib.es2020.intl.d.ts, --, --))
|
||||
>Locale : Symbol(Intl.Locale, Decl(lib.es2020.intl.d.ts, --, --), Decl(lib.es2020.intl.d.ts, --, --))
|
||||
>Intl.Locale : Symbol(Intl.Locale, Decl(lib.es2020.intl.d.ts, --, --), Decl(lib.es2020.intl.d.ts, --, --))
|
||||
>Intl : Symbol(Intl, Decl(lib.es5.d.ts, --, --), Decl(lib.es2017.intl.d.ts, --, --), Decl(lib.es2018.intl.d.ts, --, --), Decl(lib.es2020.bigint.d.ts, --, --), Decl(lib.es2020.intl.d.ts, --, --))
|
||||
>Locale : Symbol(Intl.Locale, Decl(lib.es2020.intl.d.ts, --, --), Decl(lib.es2020.intl.d.ts, --, --))
|
||||
|
||||
|
||||
@@ -207,3 +207,20 @@ console.log(Intl.DisplayNames.supportedLocalesOf(locales1, options1).join(', '))
|
||||
>join : (separator?: string) => string
|
||||
>', ' : ", "
|
||||
|
||||
new Intl.Locale(); // should error
|
||||
>new Intl.Locale() : Intl.Locale
|
||||
>Intl.Locale : new (tag: string | Intl.Locale, options?: Intl.LocaleOptions) => Intl.Locale
|
||||
>Intl : typeof Intl
|
||||
>Locale : new (tag: string | Intl.Locale, options?: Intl.LocaleOptions) => Intl.Locale
|
||||
|
||||
new Intl.Locale(new Intl.Locale('en-US'));
|
||||
>new Intl.Locale(new Intl.Locale('en-US')) : Intl.Locale
|
||||
>Intl.Locale : new (tag: string | Intl.Locale, options?: Intl.LocaleOptions) => Intl.Locale
|
||||
>Intl : typeof Intl
|
||||
>Locale : new (tag: string | Intl.Locale, options?: Intl.LocaleOptions) => Intl.Locale
|
||||
>new Intl.Locale('en-US') : Intl.Locale
|
||||
>Intl.Locale : new (tag: string | Intl.Locale, options?: Intl.LocaleOptions) => Intl.Locale
|
||||
>Intl : typeof Intl
|
||||
>Locale : new (tag: string | Intl.Locale, options?: Intl.LocaleOptions) => Intl.Locale
|
||||
>'en-US' : "en-US"
|
||||
|
||||
|
||||
@@ -2,25 +2,25 @@
|
||||
const enUS = new Intl.Locale("en-US");
|
||||
>enUS : Intl.Locale
|
||||
>new Intl.Locale("en-US") : Intl.Locale
|
||||
>Intl.Locale : new (tag?: string, options?: Intl.LocaleOptions) => Intl.Locale
|
||||
>Intl.Locale : new (tag: string | Intl.Locale, options?: Intl.LocaleOptions) => Intl.Locale
|
||||
>Intl : typeof Intl
|
||||
>Locale : new (tag?: string, options?: Intl.LocaleOptions) => Intl.Locale
|
||||
>Locale : new (tag: string | Intl.Locale, options?: Intl.LocaleOptions) => Intl.Locale
|
||||
>"en-US" : "en-US"
|
||||
|
||||
const deDE = new Intl.Locale("de-DE");
|
||||
>deDE : Intl.Locale
|
||||
>new Intl.Locale("de-DE") : Intl.Locale
|
||||
>Intl.Locale : new (tag?: string, options?: Intl.LocaleOptions) => Intl.Locale
|
||||
>Intl.Locale : new (tag: string | Intl.Locale, options?: Intl.LocaleOptions) => Intl.Locale
|
||||
>Intl : typeof Intl
|
||||
>Locale : new (tag?: string, options?: Intl.LocaleOptions) => Intl.Locale
|
||||
>Locale : new (tag: string | Intl.Locale, options?: Intl.LocaleOptions) => Intl.Locale
|
||||
>"de-DE" : "de-DE"
|
||||
|
||||
const jaJP = new Intl.Locale("ja-JP");
|
||||
>jaJP : Intl.Locale
|
||||
>new Intl.Locale("ja-JP") : Intl.Locale
|
||||
>Intl.Locale : new (tag?: string, options?: Intl.LocaleOptions) => Intl.Locale
|
||||
>Intl.Locale : new (tag: string | Intl.Locale, options?: Intl.LocaleOptions) => Intl.Locale
|
||||
>Intl : typeof Intl
|
||||
>Locale : new (tag?: string, options?: Intl.LocaleOptions) => Intl.Locale
|
||||
>Locale : new (tag: string | Intl.Locale, options?: Intl.LocaleOptions) => Intl.Locale
|
||||
>"ja-JP" : "ja-JP"
|
||||
|
||||
const now = new Date();
|
||||
|
||||
@@ -42,4 +42,7 @@ console.log(regionNamesInTraditionalChinese.of('US'));
|
||||
|
||||
const locales1 = ['ban', 'id-u-co-pinyin', 'de-ID'];
|
||||
const options1 = { localeMatcher: 'lookup' } as const;
|
||||
console.log(Intl.DisplayNames.supportedLocalesOf(locales1, options1).join(', '));
|
||||
console.log(Intl.DisplayNames.supportedLocalesOf(locales1, options1).join(', '));
|
||||
|
||||
new Intl.Locale(); // should error
|
||||
new Intl.Locale(new Intl.Locale('en-US'));
|
||||
Reference in New Issue
Block a user