Skip to content

Commit f7c457d

Browse files
authored
Update Intl.DisplayNames types for better spec compliance (#48442)
* update display names types * update baselines * add tests and baselines * add another test
1 parent 9881c99 commit f7c457d

File tree

6 files changed

+273
-25
lines changed

6 files changed

+273
-25
lines changed

src/lib/es2020.intl.d.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ declare namespace Intl {
6363
*
6464
* [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl#locales_argument).
6565
*/
66-
type LocalesArgument = UnicodeBCP47LocaleIdentifier | UnicodeBCP47LocaleIdentifier[] | Locale | Locale[] | undefined;
66+
type LocalesArgument = UnicodeBCP47LocaleIdentifier | Locale | (UnicodeBCP47LocaleIdentifier | Locale)[] | undefined;
6767

6868
/**
6969
* An object with some or all of properties of `options` parameter
@@ -295,30 +295,32 @@ declare namespace Intl {
295295
| "code"
296296
| "none";
297297

298-
type ResolvedDisplayNamesType =
298+
type DisplayNamesType =
299299
| "language"
300300
| "region"
301301
| "script"
302+
| "calendar"
303+
| "dateTimeField"
302304
| "currency";
303305

304-
type DisplayNamesType =
305-
| ResolvedDisplayNamesType
306-
| "calendar"
307-
| "datetimeField";
306+
type DisplayNamesLanguageDisplay =
307+
| "dialect"
308+
| "standard";
308309

309310
interface DisplayNamesOptions {
310-
localeMatcher: RelativeTimeFormatLocaleMatcher;
311-
style: RelativeTimeFormatStyle;
311+
localeMatcher?: RelativeTimeFormatLocaleMatcher;
312+
style?: RelativeTimeFormatStyle;
312313
type: DisplayNamesType;
313-
languageDisplay: "dialect" | "standard";
314-
fallback: DisplayNamesFallback;
314+
languageDisplay?: DisplayNamesLanguageDisplay;
315+
fallback?: DisplayNamesFallback;
315316
}
316317

317318
interface ResolvedDisplayNamesOptions {
318319
locale: UnicodeBCP47LocaleIdentifier;
319320
style: RelativeTimeFormatStyle;
320-
type: ResolvedDisplayNamesType;
321+
type: DisplayNamesType;
321322
fallback: DisplayNamesFallback;
323+
languageDisplay?: DisplayNamesLanguageDisplay;
322324
}
323325

324326
interface DisplayNames {
@@ -365,7 +367,7 @@ declare namespace Intl {
365367
*
366368
* [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DisplayNames/DisplayNames).
367369
*/
368-
new(locales?: BCP47LanguageTag | BCP47LanguageTag[], options?: Partial<DisplayNamesOptions>): DisplayNames;
370+
new(locales: LocalesArgument, options: DisplayNamesOptions): DisplayNames;
369371

370372
/**
371373
* Returns an array containing those of the provided locales that are supported in display names without having to fall back to the runtime's default locale.
@@ -380,7 +382,7 @@ declare namespace Intl {
380382
*
381383
* [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DisplayNames/supportedLocalesOf).
382384
*/
383-
supportedLocalesOf(locales: BCP47LanguageTag | BCP47LanguageTag[], options?: {localeMatcher: RelativeTimeFormatLocaleMatcher}): BCP47LanguageTag[];
385+
supportedLocalesOf(locales?: LocalesArgument, options?: { localeMatcher?: RelativeTimeFormatLocaleMatcher }): BCP47LanguageTag[];
384386
};
385387

386388
}

tests/baselines/reference/es2020IntlAPIs.errors.txt

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
tests/cases/conformance/es2020/es2020IntlAPIs.ts(45,1): error TS2554: Expected 1-2 arguments, but got 0.
2+
tests/cases/conformance/es2020/es2020IntlAPIs.ts(48,1): error TS2554: Expected 2 arguments, but got 0.
3+
tests/cases/conformance/es2020/es2020IntlAPIs.ts(49,1): error TS2554: Expected 2 arguments, but got 1.
4+
tests/cases/conformance/es2020/es2020IntlAPIs.ts(50,29): error TS2345: Argument of type '{}' is not assignable to parameter of type 'DisplayNamesOptions'.
5+
Property 'type' is missing in type '{}' but required in type 'DisplayNamesOptions'.
26

37

4-
==== tests/cases/conformance/es2020/es2020IntlAPIs.ts (1 errors) ====
8+
==== tests/cases/conformance/es2020/es2020IntlAPIs.ts (4 errors) ====
59
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl#Locale_identification_and_negotiation
610
const count = 26254.39;
711
const date = new Date("2012-05-24");
@@ -50,4 +54,26 @@ tests/cases/conformance/es2020/es2020IntlAPIs.ts(45,1): error TS2554: Expected 1
5054
~~~~~~~~~~~~~~~~~
5155
!!! error TS2554: Expected 1-2 arguments, but got 0.
5256
!!! 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'));
57+
new Intl.Locale(new Intl.Locale('en-US'));
58+
59+
new Intl.DisplayNames(); // TypeError: invalid_argument
60+
~~~~~~~~~~~~~~~~~~~~~~~
61+
!!! error TS2554: Expected 2 arguments, but got 0.
62+
!!! related TS6210 /.ts/lib.es2020.intl.d.ts:390:13: An argument for 'locales' was not provided.
63+
new Intl.DisplayNames('en'); // TypeError: invalid_argument
64+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
65+
!!! error TS2554: Expected 2 arguments, but got 1.
66+
!!! related TS6210 /.ts/lib.es2020.intl.d.ts:390:39: An argument for 'options' was not provided.
67+
new Intl.DisplayNames('en', {}); // TypeError: invalid_argument
68+
~~
69+
!!! error TS2345: Argument of type '{}' is not assignable to parameter of type 'DisplayNamesOptions'.
70+
!!! error TS2345: Property 'type' is missing in type '{}' but required in type 'DisplayNamesOptions'.
71+
!!! related TS2728 /.ts/lib.es2020.intl.d.ts:333:9: 'type' is declared here.
72+
console.log((new Intl.DisplayNames(undefined, {type: 'language'})).of('en-GB')); // "British English"
73+
74+
const localesArg = ["es-ES", new Intl.Locale("en-US")];
75+
console.log((new Intl.DisplayNames(localesArg, {type: 'language'})).resolvedOptions().locale); // "es-ES"
76+
console.log(Intl.DisplayNames.supportedLocalesOf(localesArg)); // ["es-ES", "en-US"]
77+
console.log(Intl.DisplayNames.supportedLocalesOf()); // []
78+
console.log(Intl.DisplayNames.supportedLocalesOf(localesArg, {})); // ["es-ES", "en-US"]
79+

tests/baselines/reference/es2020IntlAPIs.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,19 @@ const options1 = { localeMatcher: 'lookup' } as const;
4444
console.log(Intl.DisplayNames.supportedLocalesOf(locales1, options1).join(', '));
4545

4646
new Intl.Locale(); // should error
47-
new Intl.Locale(new Intl.Locale('en-US'));
47+
new Intl.Locale(new Intl.Locale('en-US'));
48+
49+
new Intl.DisplayNames(); // TypeError: invalid_argument
50+
new Intl.DisplayNames('en'); // TypeError: invalid_argument
51+
new Intl.DisplayNames('en', {}); // TypeError: invalid_argument
52+
console.log((new Intl.DisplayNames(undefined, {type: 'language'})).of('en-GB')); // "British English"
53+
54+
const localesArg = ["es-ES", new Intl.Locale("en-US")];
55+
console.log((new Intl.DisplayNames(localesArg, {type: 'language'})).resolvedOptions().locale); // "es-ES"
56+
console.log(Intl.DisplayNames.supportedLocalesOf(localesArg)); // ["es-ES", "en-US"]
57+
console.log(Intl.DisplayNames.supportedLocalesOf()); // []
58+
console.log(Intl.DisplayNames.supportedLocalesOf(localesArg, {})); // ["es-ES", "en-US"]
59+
4860

4961
//// [es2020IntlAPIs.js]
5062
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl#Locale_identification_and_negotiation
@@ -78,3 +90,12 @@ const options1 = { localeMatcher: 'lookup' };
7890
console.log(Intl.DisplayNames.supportedLocalesOf(locales1, options1).join(', '));
7991
new Intl.Locale(); // should error
8092
new Intl.Locale(new Intl.Locale('en-US'));
93+
new Intl.DisplayNames(); // TypeError: invalid_argument
94+
new Intl.DisplayNames('en'); // TypeError: invalid_argument
95+
new Intl.DisplayNames('en', {}); // TypeError: invalid_argument
96+
console.log((new Intl.DisplayNames(undefined, { type: 'language' })).of('en-GB')); // "British English"
97+
const localesArg = ["es-ES", new Intl.Locale("en-US")];
98+
console.log((new Intl.DisplayNames(localesArg, { type: 'language' })).resolvedOptions().locale); // "es-ES"
99+
console.log(Intl.DisplayNames.supportedLocalesOf(localesArg)); // ["es-ES", "en-US"]
100+
console.log(Intl.DisplayNames.supportedLocalesOf()); // []
101+
console.log(Intl.DisplayNames.supportedLocalesOf(localesArg, {})); // ["es-ES", "en-US"]

tests/baselines/reference/es2020IntlAPIs.symbols

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,82 @@ new Intl.Locale(new Intl.Locale('en-US'));
160160
>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, --, --))
161161
>Locale : Symbol(Intl.Locale, Decl(lib.es2020.intl.d.ts, --, --), Decl(lib.es2020.intl.d.ts, --, --))
162162

163+
new Intl.DisplayNames(); // TypeError: invalid_argument
164+
>Intl.DisplayNames : Symbol(Intl.DisplayNames, Decl(lib.es2020.intl.d.ts, --, --), Decl(lib.es2020.intl.d.ts, --, --))
165+
>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, --, --))
166+
>DisplayNames : Symbol(Intl.DisplayNames, Decl(lib.es2020.intl.d.ts, --, --), Decl(lib.es2020.intl.d.ts, --, --))
167+
168+
new Intl.DisplayNames('en'); // TypeError: invalid_argument
169+
>Intl.DisplayNames : Symbol(Intl.DisplayNames, Decl(lib.es2020.intl.d.ts, --, --), Decl(lib.es2020.intl.d.ts, --, --))
170+
>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, --, --))
171+
>DisplayNames : Symbol(Intl.DisplayNames, Decl(lib.es2020.intl.d.ts, --, --), Decl(lib.es2020.intl.d.ts, --, --))
172+
173+
new Intl.DisplayNames('en', {}); // TypeError: invalid_argument
174+
>Intl.DisplayNames : Symbol(Intl.DisplayNames, Decl(lib.es2020.intl.d.ts, --, --), Decl(lib.es2020.intl.d.ts, --, --))
175+
>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, --, --))
176+
>DisplayNames : Symbol(Intl.DisplayNames, Decl(lib.es2020.intl.d.ts, --, --), Decl(lib.es2020.intl.d.ts, --, --))
177+
178+
console.log((new Intl.DisplayNames(undefined, {type: 'language'})).of('en-GB')); // "British English"
179+
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
180+
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
181+
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
182+
>(new Intl.DisplayNames(undefined, {type: 'language'})).of : Symbol(Intl.DisplayNames.of, Decl(lib.es2020.intl.d.ts, --, --))
183+
>Intl.DisplayNames : Symbol(Intl.DisplayNames, Decl(lib.es2020.intl.d.ts, --, --), Decl(lib.es2020.intl.d.ts, --, --))
184+
>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, --, --))
185+
>DisplayNames : Symbol(Intl.DisplayNames, Decl(lib.es2020.intl.d.ts, --, --), Decl(lib.es2020.intl.d.ts, --, --))
186+
>undefined : Symbol(undefined)
187+
>type : Symbol(type, Decl(es2020IntlAPIs.ts, 50, 47))
188+
>of : Symbol(Intl.DisplayNames.of, Decl(lib.es2020.intl.d.ts, --, --))
189+
190+
const localesArg = ["es-ES", new Intl.Locale("en-US")];
191+
>localesArg : Symbol(localesArg, Decl(es2020IntlAPIs.ts, 52, 5))
192+
>Intl.Locale : Symbol(Intl.Locale, Decl(lib.es2020.intl.d.ts, --, --), Decl(lib.es2020.intl.d.ts, --, --))
193+
>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, --, --))
194+
>Locale : Symbol(Intl.Locale, Decl(lib.es2020.intl.d.ts, --, --), Decl(lib.es2020.intl.d.ts, --, --))
195+
196+
console.log((new Intl.DisplayNames(localesArg, {type: 'language'})).resolvedOptions().locale); // "es-ES"
197+
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
198+
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
199+
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
200+
>(new Intl.DisplayNames(localesArg, {type: 'language'})).resolvedOptions().locale : Symbol(Intl.ResolvedDisplayNamesOptions.locale, Decl(lib.es2020.intl.d.ts, --, --))
201+
>(new Intl.DisplayNames(localesArg, {type: 'language'})).resolvedOptions : Symbol(Intl.DisplayNames.resolvedOptions, Decl(lib.es2020.intl.d.ts, --, --))
202+
>Intl.DisplayNames : Symbol(Intl.DisplayNames, Decl(lib.es2020.intl.d.ts, --, --), Decl(lib.es2020.intl.d.ts, --, --))
203+
>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, --, --))
204+
>DisplayNames : Symbol(Intl.DisplayNames, Decl(lib.es2020.intl.d.ts, --, --), Decl(lib.es2020.intl.d.ts, --, --))
205+
>localesArg : Symbol(localesArg, Decl(es2020IntlAPIs.ts, 52, 5))
206+
>type : Symbol(type, Decl(es2020IntlAPIs.ts, 53, 48))
207+
>resolvedOptions : Symbol(Intl.DisplayNames.resolvedOptions, Decl(lib.es2020.intl.d.ts, --, --))
208+
>locale : Symbol(Intl.ResolvedDisplayNamesOptions.locale, Decl(lib.es2020.intl.d.ts, --, --))
209+
210+
console.log(Intl.DisplayNames.supportedLocalesOf(localesArg)); // ["es-ES", "en-US"]
211+
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
212+
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
213+
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
214+
>Intl.DisplayNames.supportedLocalesOf : Symbol(supportedLocalesOf, Decl(lib.es2020.intl.d.ts, --, --))
215+
>Intl.DisplayNames : Symbol(Intl.DisplayNames, Decl(lib.es2020.intl.d.ts, --, --), Decl(lib.es2020.intl.d.ts, --, --))
216+
>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, --, --))
217+
>DisplayNames : Symbol(Intl.DisplayNames, Decl(lib.es2020.intl.d.ts, --, --), Decl(lib.es2020.intl.d.ts, --, --))
218+
>supportedLocalesOf : Symbol(supportedLocalesOf, Decl(lib.es2020.intl.d.ts, --, --))
219+
>localesArg : Symbol(localesArg, Decl(es2020IntlAPIs.ts, 52, 5))
220+
221+
console.log(Intl.DisplayNames.supportedLocalesOf()); // []
222+
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
223+
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
224+
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
225+
>Intl.DisplayNames.supportedLocalesOf : Symbol(supportedLocalesOf, Decl(lib.es2020.intl.d.ts, --, --))
226+
>Intl.DisplayNames : Symbol(Intl.DisplayNames, Decl(lib.es2020.intl.d.ts, --, --), Decl(lib.es2020.intl.d.ts, --, --))
227+
>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, --, --))
228+
>DisplayNames : Symbol(Intl.DisplayNames, Decl(lib.es2020.intl.d.ts, --, --), Decl(lib.es2020.intl.d.ts, --, --))
229+
>supportedLocalesOf : Symbol(supportedLocalesOf, Decl(lib.es2020.intl.d.ts, --, --))
230+
231+
console.log(Intl.DisplayNames.supportedLocalesOf(localesArg, {})); // ["es-ES", "en-US"]
232+
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
233+
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
234+
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
235+
>Intl.DisplayNames.supportedLocalesOf : Symbol(supportedLocalesOf, Decl(lib.es2020.intl.d.ts, --, --))
236+
>Intl.DisplayNames : Symbol(Intl.DisplayNames, Decl(lib.es2020.intl.d.ts, --, --), Decl(lib.es2020.intl.d.ts, --, --))
237+
>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, --, --))
238+
>DisplayNames : Symbol(Intl.DisplayNames, Decl(lib.es2020.intl.d.ts, --, --), Decl(lib.es2020.intl.d.ts, --, --))
239+
>supportedLocalesOf : Symbol(supportedLocalesOf, Decl(lib.es2020.intl.d.ts, --, --))
240+
>localesArg : Symbol(localesArg, Decl(es2020IntlAPIs.ts, 52, 5))
241+

0 commit comments

Comments
 (0)