Skip to content

Commit 3426167

Browse files
authored
Merge pull request #30381 from Microsoft/mapConstructor
Fix map constructor to accept readonly tuple
2 parents 5d08b68 + 027d65a commit 3426167

6 files changed

+38
-2
lines changed

src/lib/es2015.collection.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ interface Map<K, V> {
1010

1111
interface MapConstructor {
1212
new(): Map<any, any>;
13-
new<K, V>(entries?: ReadonlyArray<[K, V]> | null): Map<K, V>;
13+
new<K, V>(entries?: ReadonlyArray<readonly [K, V]> | null): Map<K, V>;
1414
readonly prototype: Map<any, any>;
1515
}
1616
declare var Map: MapConstructor;

src/lib/es2015.iterable.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ interface ReadonlyMap<K, V> {
129129
}
130130

131131
interface MapConstructor {
132-
new <K, V>(iterable: Iterable<[K, V]>): Map<K, V>;
132+
new <K, V>(iterable: Iterable<readonly [K, V]>): Map<K, V>;
133133
}
134134

135135
interface WeakMap<K extends object, V> { }
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//// [mapConstructorOnReadonlyTuple.ts]
2+
const pairs = [['1', 1], ['2', 2]] as const
3+
new Map(pairs);
4+
5+
//// [mapConstructorOnReadonlyTuple.js]
6+
const pairs = [['1', 1], ['2', 2]];
7+
new Map(pairs);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
=== tests/cases/compiler/mapConstructorOnReadonlyTuple.ts ===
2+
const pairs = [['1', 1], ['2', 2]] as const
3+
>pairs : Symbol(pairs, Decl(mapConstructorOnReadonlyTuple.ts, 0, 5))
4+
5+
new Map(pairs);
6+
>Map : Symbol(Map, Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --))
7+
>pairs : Symbol(pairs, Decl(mapConstructorOnReadonlyTuple.ts, 0, 5))
8+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
=== tests/cases/compiler/mapConstructorOnReadonlyTuple.ts ===
2+
const pairs = [['1', 1], ['2', 2]] as const
3+
>pairs : readonly [readonly ["1", 1], readonly ["2", 2]]
4+
>[['1', 1], ['2', 2]] as const : readonly [readonly ["1", 1], readonly ["2", 2]]
5+
>[['1', 1], ['2', 2]] : readonly [readonly ["1", 1], readonly ["2", 2]]
6+
>['1', 1] : readonly ["1", 1]
7+
>'1' : "1"
8+
>1 : 1
9+
>['2', 2] : readonly ["2", 2]
10+
>'2' : "2"
11+
>2 : 2
12+
13+
new Map(pairs);
14+
>new Map(pairs) : Map<"1" | "2", 1 | 2>
15+
>Map : MapConstructor
16+
>pairs : readonly [readonly ["1", 1], readonly ["2", 2]]
17+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// @target: es2015
2+
3+
const pairs = [['1', 1], ['2', 2]] as const
4+
new Map(pairs);

0 commit comments

Comments
 (0)