Skip to content

Commit 1f52ca8

Browse files
authored
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
1 parent 39013f1 commit 1f52ca8

File tree

7 files changed

+100
-9
lines changed

7 files changed

+100
-9
lines changed

src/lib/es2020.intl.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ declare namespace Intl {
288288
* [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale).
289289
*/
290290
const Locale: {
291-
new (tag?: BCP47LanguageTag, options?: LocaleOptions): Locale;
291+
new (tag: BCP47LanguageTag | Locale, options?: LocaleOptions): Locale;
292292
};
293293

294294
interface DisplayNamesOptions {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
tests/cases/conformance/es2020/es2020IntlAPIs.ts(45,1): error TS2554: Expected 1-2 arguments, but got 0.
2+
3+
4+
==== tests/cases/conformance/es2020/es2020IntlAPIs.ts (1 errors) ====
5+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl#Locale_identification_and_negotiation
6+
const count = 26254.39;
7+
const date = new Date("2012-05-24");
8+
9+
function log(locale: string) {
10+
console.log(
11+
`${new Intl.DateTimeFormat(locale).format(date)} ${new Intl.NumberFormat(locale).format(count)}`
12+
);
13+
}
14+
15+
log("en-US");
16+
// expected output: 5/24/2012 26,254.39
17+
18+
log("de-DE");
19+
// expected output: 24.5.2012 26.254,39
20+
21+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/RelativeTimeFormat
22+
const rtf1 = new Intl.RelativeTimeFormat('en', { style: 'narrow' });
23+
24+
console.log(rtf1.format(3, 'quarter'));
25+
//expected output: "in 3 qtrs."
26+
27+
console.log(rtf1.format(-1, 'day'));
28+
//expected output: "1 day ago"
29+
30+
const rtf2 = new Intl.RelativeTimeFormat('es', { numeric: 'auto' });
31+
32+
console.log(rtf2.format(2, 'day'));
33+
//expected output: "pasado mañana"
34+
35+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DisplayNames
36+
const regionNamesInEnglish = new Intl.DisplayNames(['en'], { type: 'region' });
37+
const regionNamesInTraditionalChinese = new Intl.DisplayNames(['zh-Hant'], { type: 'region' });
38+
39+
console.log(regionNamesInEnglish.of('US'));
40+
// expected output: "United States"
41+
42+
console.log(regionNamesInTraditionalChinese.of('US'));
43+
// expected output: "美國"
44+
45+
const locales1 = ['ban', 'id-u-co-pinyin', 'de-ID'];
46+
const options1 = { localeMatcher: 'lookup' } as const;
47+
console.log(Intl.DisplayNames.supportedLocalesOf(locales1, options1).join(', '));
48+
49+
new Intl.Locale(); // should error
50+
~~~~~~~~~~~~~~~~~
51+
!!! error TS2554: Expected 1-2 arguments, but got 0.
52+
!!! related TS6210 /.ts/lib.es2020.intl.d.ts:311:14: An argument for 'tag' was not provided.
53+
new Intl.Locale(new Intl.Locale('en-US'));

tests/baselines/reference/es2020IntlAPIs.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@ console.log(regionNamesInTraditionalChinese.of('US'));
4141

4242
const locales1 = ['ban', 'id-u-co-pinyin', 'de-ID'];
4343
const options1 = { localeMatcher: 'lookup' } as const;
44-
console.log(Intl.DisplayNames.supportedLocalesOf(locales1, options1).join(', '));
44+
console.log(Intl.DisplayNames.supportedLocalesOf(locales1, options1).join(', '));
45+
46+
new Intl.Locale(); // should error
47+
new Intl.Locale(new Intl.Locale('en-US'));
4548

4649
//// [es2020IntlAPIs.js]
4750
// 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'));
7376
const locales1 = ['ban', 'id-u-co-pinyin', 'de-ID'];
7477
const options1 = { localeMatcher: 'lookup' };
7578
console.log(Intl.DisplayNames.supportedLocalesOf(locales1, options1).join(', '));
79+
new Intl.Locale(); // should error
80+
new Intl.Locale(new Intl.Locale('en-US'));

tests/baselines/reference/es2020IntlAPIs.symbols

+13
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,16 @@ console.log(Intl.DisplayNames.supportedLocalesOf(locales1, options1).join(', '))
147147
>options1 : Symbol(options1, Decl(es2020IntlAPIs.ts, 41, 5))
148148
>join : Symbol(Array.join, Decl(lib.es5.d.ts, --, --))
149149

150+
new Intl.Locale(); // should error
151+
>Intl.Locale : Symbol(Intl.Locale, Decl(lib.es2020.intl.d.ts, --, --), Decl(lib.es2020.intl.d.ts, --, --))
152+
>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, --, --))
153+
>Locale : Symbol(Intl.Locale, Decl(lib.es2020.intl.d.ts, --, --), Decl(lib.es2020.intl.d.ts, --, --))
154+
155+
new Intl.Locale(new Intl.Locale('en-US'));
156+
>Intl.Locale : Symbol(Intl.Locale, Decl(lib.es2020.intl.d.ts, --, --), Decl(lib.es2020.intl.d.ts, --, --))
157+
>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, --, --))
158+
>Locale : Symbol(Intl.Locale, Decl(lib.es2020.intl.d.ts, --, --), Decl(lib.es2020.intl.d.ts, --, --))
159+
>Intl.Locale : Symbol(Intl.Locale, Decl(lib.es2020.intl.d.ts, --, --), Decl(lib.es2020.intl.d.ts, --, --))
160+
>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, --, --))
161+
>Locale : Symbol(Intl.Locale, Decl(lib.es2020.intl.d.ts, --, --), Decl(lib.es2020.intl.d.ts, --, --))
162+

tests/baselines/reference/es2020IntlAPIs.types

+17
Original file line numberDiff line numberDiff line change
@@ -207,3 +207,20 @@ console.log(Intl.DisplayNames.supportedLocalesOf(locales1, options1).join(', '))
207207
>join : (separator?: string) => string
208208
>', ' : ", "
209209

210+
new Intl.Locale(); // should error
211+
>new Intl.Locale() : Intl.Locale
212+
>Intl.Locale : new (tag: string | Intl.Locale, options?: Intl.LocaleOptions) => Intl.Locale
213+
>Intl : typeof Intl
214+
>Locale : new (tag: string | Intl.Locale, options?: Intl.LocaleOptions) => Intl.Locale
215+
216+
new Intl.Locale(new Intl.Locale('en-US'));
217+
>new Intl.Locale(new Intl.Locale('en-US')) : Intl.Locale
218+
>Intl.Locale : new (tag: string | Intl.Locale, options?: Intl.LocaleOptions) => Intl.Locale
219+
>Intl : typeof Intl
220+
>Locale : new (tag: string | Intl.Locale, options?: Intl.LocaleOptions) => Intl.Locale
221+
>new Intl.Locale('en-US') : Intl.Locale
222+
>Intl.Locale : new (tag: string | Intl.Locale, options?: Intl.LocaleOptions) => Intl.Locale
223+
>Intl : typeof Intl
224+
>Locale : new (tag: string | Intl.Locale, options?: Intl.LocaleOptions) => Intl.Locale
225+
>'en-US' : "en-US"
226+

tests/baselines/reference/localesObjectArgument.types

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,25 @@
22
const enUS = new Intl.Locale("en-US");
33
>enUS : Intl.Locale
44
>new Intl.Locale("en-US") : Intl.Locale
5-
>Intl.Locale : new (tag?: string, options?: Intl.LocaleOptions) => Intl.Locale
5+
>Intl.Locale : new (tag: string | Intl.Locale, options?: Intl.LocaleOptions) => Intl.Locale
66
>Intl : typeof Intl
7-
>Locale : new (tag?: string, options?: Intl.LocaleOptions) => Intl.Locale
7+
>Locale : new (tag: string | Intl.Locale, options?: Intl.LocaleOptions) => Intl.Locale
88
>"en-US" : "en-US"
99

1010
const deDE = new Intl.Locale("de-DE");
1111
>deDE : Intl.Locale
1212
>new Intl.Locale("de-DE") : Intl.Locale
13-
>Intl.Locale : new (tag?: string, options?: Intl.LocaleOptions) => Intl.Locale
13+
>Intl.Locale : new (tag: string | Intl.Locale, options?: Intl.LocaleOptions) => Intl.Locale
1414
>Intl : typeof Intl
15-
>Locale : new (tag?: string, options?: Intl.LocaleOptions) => Intl.Locale
15+
>Locale : new (tag: string | Intl.Locale, options?: Intl.LocaleOptions) => Intl.Locale
1616
>"de-DE" : "de-DE"
1717

1818
const jaJP = new Intl.Locale("ja-JP");
1919
>jaJP : Intl.Locale
2020
>new Intl.Locale("ja-JP") : Intl.Locale
21-
>Intl.Locale : new (tag?: string, options?: Intl.LocaleOptions) => Intl.Locale
21+
>Intl.Locale : new (tag: string | Intl.Locale, options?: Intl.LocaleOptions) => Intl.Locale
2222
>Intl : typeof Intl
23-
>Locale : new (tag?: string, options?: Intl.LocaleOptions) => Intl.Locale
23+
>Locale : new (tag: string | Intl.Locale, options?: Intl.LocaleOptions) => Intl.Locale
2424
>"ja-JP" : "ja-JP"
2525

2626
const now = new Date();

tests/cases/conformance/es2020/es2020IntlAPIs.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,7 @@ console.log(regionNamesInTraditionalChinese.of('US'));
4242

4343
const locales1 = ['ban', 'id-u-co-pinyin', 'de-ID'];
4444
const options1 = { localeMatcher: 'lookup' } as const;
45-
console.log(Intl.DisplayNames.supportedLocalesOf(locales1, options1).join(', '));
45+
console.log(Intl.DisplayNames.supportedLocalesOf(locales1, options1).join(', '));
46+
47+
new Intl.Locale(); // should error
48+
new Intl.Locale(new Intl.Locale('en-US'));

0 commit comments

Comments
 (0)