Skip to content

Commit 1abc47b

Browse files
authored
Add Intl.Locale parameter type to toLocale[X]String signatures. (#47811)
* add Intl.Locale param type to locales argument in BigInt, Number, and Date methods * update baselines * add test for locales object arguments * fix indentation
1 parent 03c11c8 commit 1abc47b

20 files changed

+348
-17
lines changed

src/compiler/commandLineParser.ts

+2
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,13 @@ namespace ts {
6868
["es2019.string", "lib.es2019.string.d.ts"],
6969
["es2019.symbol", "lib.es2019.symbol.d.ts"],
7070
["es2020.bigint", "lib.es2020.bigint.d.ts"],
71+
["es2020.date", "lib.es2020.date.d.ts"],
7172
["es2020.promise", "lib.es2020.promise.d.ts"],
7273
["es2020.sharedmemory", "lib.es2020.sharedmemory.d.ts"],
7374
["es2020.string", "lib.es2020.string.d.ts"],
7475
["es2020.symbol.wellknown", "lib.es2020.symbol.wellknown.d.ts"],
7576
["es2020.intl", "lib.es2020.intl.d.ts"],
77+
["es2020.number", "lib.es2020.number.d.ts"],
7678
["es2021.promise", "lib.es2021.promise.d.ts"],
7779
["es2021.string", "lib.es2021.string.d.ts"],
7880
["es2021.weakref", "lib.es2021.weakref.d.ts"],

src/lib/es2020.bigint.d.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/// <reference lib="es2020.intl" />
2+
13
interface BigIntToLocaleStringOptions {
24
/**
35
* The locale matching algorithm to use.The default is "best fit". For information about this option, see the {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl#Locale_negotiation Intl page}.
@@ -92,7 +94,7 @@ interface BigInt {
9294
toString(radix?: number): string;
9395

9496
/** Returns a string representation appropriate to the host environment's current locale. */
95-
toLocaleString(locales?: string, options?: BigIntToLocaleStringOptions): string;
97+
toLocaleString(locales?: Intl.LocalesArgument, options?: BigIntToLocaleStringOptions): string;
9698

9799
/** Returns the primitive value of the specified object. */
98100
valueOf(): bigint;

src/lib/es2020.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
/// <reference lib="es2019" />
22
/// <reference lib="es2020.bigint" />
3+
/// <reference lib="es2020.date" />
4+
/// <reference lib="es2020.number" />
35
/// <reference lib="es2020.promise" />
46
/// <reference lib="es2020.sharedmemory" />
57
/// <reference lib="es2020.string" />

src/lib/es2020.date.d.ts

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/// <reference lib="es2020.intl" />
2+
3+
interface Date {
4+
/**
5+
* Converts a date and time to a string by using the current or specified locale.
6+
* @param locales A locale string, array of locale strings, Intl.Locale object, or array of Intl.Locale objects that contain one or more language or locale tags. If you include more than one locale string, list them in descending order of priority so that the first entry is the preferred locale. If you omit this parameter, the default locale of the JavaScript runtime is used.
7+
* @param options An object that contains one or more properties that specify comparison options.
8+
*/
9+
toLocaleString(locales?: Intl.LocalesArgument, options?: Intl.DateTimeFormatOptions): string;
10+
11+
/**
12+
* Converts a date to a string by using the current or specified locale.
13+
* @param locales A locale string, array of locale strings, Intl.Locale object, or array of Intl.Locale objects that contain one or more language or locale tags. If you include more than one locale string, list them in descending order of priority so that the first entry is the preferred locale. If you omit this parameter, the default locale of the JavaScript runtime is used.
14+
* @param options An object that contains one or more properties that specify comparison options.
15+
*/
16+
toLocaleDateString(locales?: Intl.LocalesArgument, options?: Intl.DateTimeFormatOptions): string;
17+
18+
/**
19+
* Converts a time to a string by using the current or specified locale.
20+
* @param locales A locale string, array of locale strings, Intl.Locale object, or array of Intl.Locale objects that contain one or more language or locale tags. If you include more than one locale string, list them in descending order of priority so that the first entry is the preferred locale. If you omit this parameter, the default locale of the JavaScript runtime is used.
21+
* @param options An object that contains one or more properties that specify comparison options.
22+
*/
23+
toLocaleTimeString(locales?: Intl.LocalesArgument, options?: Intl.DateTimeFormatOptions): string;
24+
}

src/lib/es2020.intl.d.ts

+7
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@ declare namespace Intl {
5858
*/
5959
type BCP47LanguageTag = string;
6060

61+
/**
62+
* The locale(s) to use
63+
*
64+
* [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl#locales_argument).
65+
*/
66+
type LocalesArgument = UnicodeBCP47LocaleIdentifier | UnicodeBCP47LocaleIdentifier[] | Locale | Locale[] | undefined;
67+
6168
/**
6269
* An object with some or all of properties of `options` parameter
6370
* of `Intl.RelativeTimeFormat` constructor.

src/lib/es2020.number.d.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/// <reference lib="es2020.intl" />
2+
3+
interface Number {
4+
/**
5+
* Converts a number to a string by using the current or specified locale.
6+
* @param locales A locale string, array of locale strings, Intl.Locale object, or array of Intl.Locale objects that contain one or more language or locale tags. If you include more than one locale string, list them in descending order of priority so that the first entry is the preferred locale. If you omit this parameter, the default locale of the JavaScript runtime is used.
7+
* @param options An object that contains one or more properties that specify comparison options.
8+
*/
9+
toLocaleString(locales?: Intl.LocalesArgument, options?: Intl.NumberFormatOptions): string;
10+
}

src/lib/libs.json

+2
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,13 @@
4444
"es2019.string",
4545
"es2019.symbol",
4646
"es2020.bigint",
47+
"es2020.date",
4748
"es2020.promise",
4849
"es2020.sharedmemory",
4950
"es2020.string",
5051
"es2020.symbol.wellknown",
5152
"es2020.intl",
53+
"es2020.number",
5254
"es2021.string",
5355
"es2021.promise",
5456
"es2021.weakref",

tests/baselines/reference/bigintWithLib.types

+8-8
Original file line numberDiff line numberDiff line change
@@ -66,26 +66,26 @@ stringVal = bigintVal.toLocaleString();
6666
>stringVal = bigintVal.toLocaleString() : string
6767
>stringVal : string
6868
>bigintVal.toLocaleString() : string
69-
>bigintVal.toLocaleString : (locales?: string, options?: BigIntToLocaleStringOptions) => string
69+
>bigintVal.toLocaleString : (locales?: Intl.LocalesArgument, options?: BigIntToLocaleStringOptions) => string
7070
>bigintVal : bigint
71-
>toLocaleString : (locales?: string, options?: BigIntToLocaleStringOptions) => string
71+
>toLocaleString : (locales?: Intl.LocalesArgument, options?: BigIntToLocaleStringOptions) => string
7272

7373
stringVal = bigintVal.toLocaleString('de-DE');
7474
>stringVal = bigintVal.toLocaleString('de-DE') : string
7575
>stringVal : string
7676
>bigintVal.toLocaleString('de-DE') : string
77-
>bigintVal.toLocaleString : (locales?: string, options?: BigIntToLocaleStringOptions) => string
77+
>bigintVal.toLocaleString : (locales?: Intl.LocalesArgument, options?: BigIntToLocaleStringOptions) => string
7878
>bigintVal : bigint
79-
>toLocaleString : (locales?: string, options?: BigIntToLocaleStringOptions) => string
79+
>toLocaleString : (locales?: Intl.LocalesArgument, options?: BigIntToLocaleStringOptions) => string
8080
>'de-DE' : "de-DE"
8181

8282
stringVal = bigintVal.toLocaleString('de-DE', { style: 'currency' });
8383
>stringVal = bigintVal.toLocaleString('de-DE', { style: 'currency' }) : string
8484
>stringVal : string
8585
>bigintVal.toLocaleString('de-DE', { style: 'currency' }) : string
86-
>bigintVal.toLocaleString : (locales?: string, options?: BigIntToLocaleStringOptions) => string
86+
>bigintVal.toLocaleString : (locales?: Intl.LocalesArgument, options?: BigIntToLocaleStringOptions) => string
8787
>bigintVal : bigint
88-
>toLocaleString : (locales?: string, options?: BigIntToLocaleStringOptions) => string
88+
>toLocaleString : (locales?: Intl.LocalesArgument, options?: BigIntToLocaleStringOptions) => string
8989
>'de-DE' : "de-DE"
9090
>{ style: 'currency' } : { style: string; }
9191
>style : string
@@ -95,9 +95,9 @@ stringVal = bigintVal.toLocaleString('de-DE', { style: 'currency', currency: 'EU
9595
>stringVal = bigintVal.toLocaleString('de-DE', { style: 'currency', currency: 'EUR' }) : string
9696
>stringVal : string
9797
>bigintVal.toLocaleString('de-DE', { style: 'currency', currency: 'EUR' }) : string
98-
>bigintVal.toLocaleString : (locales?: string, options?: BigIntToLocaleStringOptions) => string
98+
>bigintVal.toLocaleString : (locales?: Intl.LocalesArgument, options?: BigIntToLocaleStringOptions) => string
9999
>bigintVal : bigint
100-
>toLocaleString : (locales?: string, options?: BigIntToLocaleStringOptions) => string
100+
>toLocaleString : (locales?: Intl.LocalesArgument, options?: BigIntToLocaleStringOptions) => string
101101
>'de-DE' : "de-DE"
102102
>{ style: 'currency', currency: 'EUR' } : { style: string; currency: string; }
103103
>style : string

tests/baselines/reference/es2020IntlAPIs.symbols

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const count = 26254.39;
55

66
const date = new Date("2012-05-24");
77
>date : Symbol(date, Decl(es2020IntlAPIs.ts, 2, 5))
8-
>Date : Symbol(Date, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.scripthost.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --))
8+
>Date : Symbol(Date, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.scripthost.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --) ... and 1 more)
99

1010
function log(locale: string) {
1111
>log : Symbol(log, Decl(es2020IntlAPIs.ts, 2, 36))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//// [localesObjectArgument.ts]
2+
const enUS = new Intl.Locale("en-US");
3+
const deDE = new Intl.Locale("de-DE");
4+
const jaJP = new Intl.Locale("ja-JP");
5+
6+
const now = new Date();
7+
const num = 1000;
8+
const bigint = 123456789123456789n;
9+
10+
now.toLocaleString(enUS);
11+
now.toLocaleDateString(enUS);
12+
now.toLocaleTimeString(enUS);
13+
now.toLocaleString([deDE, jaJP]);
14+
now.toLocaleDateString([deDE, jaJP]);
15+
now.toLocaleTimeString([deDE, jaJP]);
16+
17+
num.toLocaleString(enUS);
18+
num.toLocaleString([deDE, jaJP]);
19+
20+
bigint.toLocaleString(enUS);
21+
bigint.toLocaleString([deDE, jaJP]);
22+
23+
24+
//// [localesObjectArgument.js]
25+
const enUS = new Intl.Locale("en-US");
26+
const deDE = new Intl.Locale("de-DE");
27+
const jaJP = new Intl.Locale("ja-JP");
28+
const now = new Date();
29+
const num = 1000;
30+
const bigint = 123456789123456789n;
31+
now.toLocaleString(enUS);
32+
now.toLocaleDateString(enUS);
33+
now.toLocaleTimeString(enUS);
34+
now.toLocaleString([deDE, jaJP]);
35+
now.toLocaleDateString([deDE, jaJP]);
36+
now.toLocaleTimeString([deDE, jaJP]);
37+
num.toLocaleString(enUS);
38+
num.toLocaleString([deDE, jaJP]);
39+
bigint.toLocaleString(enUS);
40+
bigint.toLocaleString([deDE, jaJP]);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
=== tests/cases/conformance/es2020/localesObjectArgument.ts ===
2+
const enUS = new Intl.Locale("en-US");
3+
>enUS : Symbol(enUS, Decl(localesObjectArgument.ts, 0, 5))
4+
>Intl.Locale : Symbol(Intl.Locale, Decl(lib.es2020.intl.d.ts, --, --), Decl(lib.es2020.intl.d.ts, --, --))
5+
>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, --, --))
6+
>Locale : Symbol(Intl.Locale, Decl(lib.es2020.intl.d.ts, --, --), Decl(lib.es2020.intl.d.ts, --, --))
7+
8+
const deDE = new Intl.Locale("de-DE");
9+
>deDE : Symbol(deDE, Decl(localesObjectArgument.ts, 1, 5))
10+
>Intl.Locale : Symbol(Intl.Locale, Decl(lib.es2020.intl.d.ts, --, --), Decl(lib.es2020.intl.d.ts, --, --))
11+
>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, --, --))
12+
>Locale : Symbol(Intl.Locale, Decl(lib.es2020.intl.d.ts, --, --), Decl(lib.es2020.intl.d.ts, --, --))
13+
14+
const jaJP = new Intl.Locale("ja-JP");
15+
>jaJP : Symbol(jaJP, Decl(localesObjectArgument.ts, 2, 5))
16+
>Intl.Locale : Symbol(Intl.Locale, Decl(lib.es2020.intl.d.ts, --, --), Decl(lib.es2020.intl.d.ts, --, --))
17+
>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, --, --))
18+
>Locale : Symbol(Intl.Locale, Decl(lib.es2020.intl.d.ts, --, --), Decl(lib.es2020.intl.d.ts, --, --))
19+
20+
const now = new Date();
21+
>now : Symbol(now, Decl(localesObjectArgument.ts, 4, 5))
22+
>Date : Symbol(Date, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.scripthost.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --) ... and 1 more)
23+
24+
const num = 1000;
25+
>num : Symbol(num, Decl(localesObjectArgument.ts, 5, 5))
26+
27+
const bigint = 123456789123456789n;
28+
>bigint : Symbol(bigint, Decl(localesObjectArgument.ts, 6, 5))
29+
30+
now.toLocaleString(enUS);
31+
>now.toLocaleString : Symbol(Date.toLocaleString, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2020.date.d.ts, --, --))
32+
>now : Symbol(now, Decl(localesObjectArgument.ts, 4, 5))
33+
>toLocaleString : Symbol(Date.toLocaleString, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2020.date.d.ts, --, --))
34+
>enUS : Symbol(enUS, Decl(localesObjectArgument.ts, 0, 5))
35+
36+
now.toLocaleDateString(enUS);
37+
>now.toLocaleDateString : Symbol(Date.toLocaleDateString, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2020.date.d.ts, --, --))
38+
>now : Symbol(now, Decl(localesObjectArgument.ts, 4, 5))
39+
>toLocaleDateString : Symbol(Date.toLocaleDateString, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2020.date.d.ts, --, --))
40+
>enUS : Symbol(enUS, Decl(localesObjectArgument.ts, 0, 5))
41+
42+
now.toLocaleTimeString(enUS);
43+
>now.toLocaleTimeString : Symbol(Date.toLocaleTimeString, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2020.date.d.ts, --, --))
44+
>now : Symbol(now, Decl(localesObjectArgument.ts, 4, 5))
45+
>toLocaleTimeString : Symbol(Date.toLocaleTimeString, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2020.date.d.ts, --, --))
46+
>enUS : Symbol(enUS, Decl(localesObjectArgument.ts, 0, 5))
47+
48+
now.toLocaleString([deDE, jaJP]);
49+
>now.toLocaleString : Symbol(Date.toLocaleString, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2020.date.d.ts, --, --))
50+
>now : Symbol(now, Decl(localesObjectArgument.ts, 4, 5))
51+
>toLocaleString : Symbol(Date.toLocaleString, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2020.date.d.ts, --, --))
52+
>deDE : Symbol(deDE, Decl(localesObjectArgument.ts, 1, 5))
53+
>jaJP : Symbol(jaJP, Decl(localesObjectArgument.ts, 2, 5))
54+
55+
now.toLocaleDateString([deDE, jaJP]);
56+
>now.toLocaleDateString : Symbol(Date.toLocaleDateString, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2020.date.d.ts, --, --))
57+
>now : Symbol(now, Decl(localesObjectArgument.ts, 4, 5))
58+
>toLocaleDateString : Symbol(Date.toLocaleDateString, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2020.date.d.ts, --, --))
59+
>deDE : Symbol(deDE, Decl(localesObjectArgument.ts, 1, 5))
60+
>jaJP : Symbol(jaJP, Decl(localesObjectArgument.ts, 2, 5))
61+
62+
now.toLocaleTimeString([deDE, jaJP]);
63+
>now.toLocaleTimeString : Symbol(Date.toLocaleTimeString, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2020.date.d.ts, --, --))
64+
>now : Symbol(now, Decl(localesObjectArgument.ts, 4, 5))
65+
>toLocaleTimeString : Symbol(Date.toLocaleTimeString, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2020.date.d.ts, --, --))
66+
>deDE : Symbol(deDE, Decl(localesObjectArgument.ts, 1, 5))
67+
>jaJP : Symbol(jaJP, Decl(localesObjectArgument.ts, 2, 5))
68+
69+
num.toLocaleString(enUS);
70+
>num.toLocaleString : Symbol(Number.toLocaleString, Decl(lib.es5.d.ts, --, --), Decl(lib.es2020.number.d.ts, --, --))
71+
>num : Symbol(num, Decl(localesObjectArgument.ts, 5, 5))
72+
>toLocaleString : Symbol(Number.toLocaleString, Decl(lib.es5.d.ts, --, --), Decl(lib.es2020.number.d.ts, --, --))
73+
>enUS : Symbol(enUS, Decl(localesObjectArgument.ts, 0, 5))
74+
75+
num.toLocaleString([deDE, jaJP]);
76+
>num.toLocaleString : Symbol(Number.toLocaleString, Decl(lib.es5.d.ts, --, --), Decl(lib.es2020.number.d.ts, --, --))
77+
>num : Symbol(num, Decl(localesObjectArgument.ts, 5, 5))
78+
>toLocaleString : Symbol(Number.toLocaleString, Decl(lib.es5.d.ts, --, --), Decl(lib.es2020.number.d.ts, --, --))
79+
>deDE : Symbol(deDE, Decl(localesObjectArgument.ts, 1, 5))
80+
>jaJP : Symbol(jaJP, Decl(localesObjectArgument.ts, 2, 5))
81+
82+
bigint.toLocaleString(enUS);
83+
>bigint.toLocaleString : Symbol(BigInt.toLocaleString, Decl(lib.es2020.bigint.d.ts, --, --))
84+
>bigint : Symbol(bigint, Decl(localesObjectArgument.ts, 6, 5))
85+
>toLocaleString : Symbol(BigInt.toLocaleString, Decl(lib.es2020.bigint.d.ts, --, --))
86+
>enUS : Symbol(enUS, Decl(localesObjectArgument.ts, 0, 5))
87+
88+
bigint.toLocaleString([deDE, jaJP]);
89+
>bigint.toLocaleString : Symbol(BigInt.toLocaleString, Decl(lib.es2020.bigint.d.ts, --, --))
90+
>bigint : Symbol(bigint, Decl(localesObjectArgument.ts, 6, 5))
91+
>toLocaleString : Symbol(BigInt.toLocaleString, Decl(lib.es2020.bigint.d.ts, --, --))
92+
>deDE : Symbol(deDE, Decl(localesObjectArgument.ts, 1, 5))
93+
>jaJP : Symbol(jaJP, Decl(localesObjectArgument.ts, 2, 5))
94+

0 commit comments

Comments
 (0)