Skip to content

Commit e872a6e

Browse files
ExE-Bossjakebailey
andauthored
Un‑consolidate and fix WeakMap constructor overloads (#56713)
Co-authored-by: Jake Bailey <5341706+jakebailey@users.noreply.github.com>
1 parent 5e4281f commit e872a6e

File tree

10 files changed

+28
-12
lines changed

10 files changed

+28
-12
lines changed

src/lib/es2015.iterable.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ interface MapConstructor {
168168
interface WeakMap<K extends WeakKey, V> {}
169169

170170
interface WeakMapConstructor {
171-
new <K extends WeakKey, V>(iterable: Iterable<readonly [K, V]>): WeakMap<K, V>;
171+
new <K extends WeakKey = WeakKey, V = any>(iterable?: Iterable<readonly [K, V]> | null): WeakMap<K, V>;
172172
}
173173

174174
interface SetIterator<T> extends IteratorObject<T, BuiltinIteratorReturn, unknown> {

tests/baselines/reference/dissallowSymbolAsWeakType.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ dissallowSymbolAsWeakType.ts(4,8): error TS2345: Argument of type 'symbol' is no
1212
dissallowSymbolAsWeakType.ts(5,8): error TS2345: Argument of type 'symbol' is not assignable to parameter of type 'object'.
1313
dissallowSymbolAsWeakType.ts(6,11): error TS2345: Argument of type 'symbol' is not assignable to parameter of type 'object'.
1414
dissallowSymbolAsWeakType.ts(8,16): error TS2769: No overload matches this call.
15-
Overload 1 of 2, '(iterable: Iterable<readonly [object, boolean]>): WeakMap<object, boolean>', gave the following error.
15+
Overload 1 of 2, '(iterable?: Iterable<readonly [object, boolean]> | null | undefined): WeakMap<object, boolean>', gave the following error.
1616
Argument of type '[symbol, false][]' is not assignable to parameter of type 'Iterable<readonly [object, boolean]>'.
1717
The types returned by '[Symbol.iterator]().next(...)' are incompatible between these types.
1818
Type 'IteratorResult<[symbol, false], undefined>' is not assignable to type 'IteratorResult<readonly [object, boolean], any>'.
@@ -60,7 +60,7 @@ dissallowSymbolAsWeakType.ts(19,14): error TS2345: Argument of type 'symbol' is
6060
const wm = new WeakMap([[s, false]]);
6161
~~~~~~~
6262
!!! error TS2769: No overload matches this call.
63-
!!! error TS2769: Overload 1 of 2, '(iterable: Iterable<readonly [object, boolean]>): WeakMap<object, boolean>', gave the following error.
63+
!!! error TS2769: Overload 1 of 2, '(iterable?: Iterable<readonly [object, boolean]> | null | undefined): WeakMap<object, boolean>', gave the following error.
6464
!!! error TS2769: Argument of type '[symbol, false][]' is not assignable to parameter of type 'Iterable<readonly [object, boolean]>'.
6565
!!! error TS2769: The types returned by '[Symbol.iterator]().next(...)' are incompatible between these types.
6666
!!! error TS2769: Type 'IteratorResult<[symbol, false], undefined>' is not assignable to type 'IteratorResult<readonly [object, boolean], any>'.

tests/baselines/reference/extendingSetWithCheckJs.symbols renamed to tests/baselines/reference/extendingCollectionsWithCheckJs.symbols

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
//// [tests/cases/compiler/extendingSetWithCheckJs.ts] ////
1+
//// [tests/cases/compiler/extendingCollectionsWithCheckJs.ts] ////
22

3-
=== extendingSetWithCheckJs.ts ===
3+
=== extendingCollectionsWithCheckJs.js ===
44
class MySet extends Set {
5-
>MySet : Symbol(MySet, Decl(extendingSetWithCheckJs.ts, 0, 0))
5+
>MySet : Symbol(MySet, Decl(extendingCollectionsWithCheckJs.js, 0, 0))
66
>Set : Symbol(Set, 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, --, --))
77

88
constructor() {
@@ -12,7 +12,7 @@ class MySet extends Set {
1212
}
1313

1414
class MyWeakSet extends WeakSet {
15-
>MyWeakSet : Symbol(MyWeakSet, Decl(extendingSetWithCheckJs.ts, 4, 1))
15+
>MyWeakSet : Symbol(MyWeakSet, Decl(extendingCollectionsWithCheckJs.js, 4, 1))
1616
>WeakSet : Symbol(WeakSet, 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, --, --))
1717

1818
constructor() {
@@ -22,7 +22,7 @@ class MyWeakSet extends WeakSet {
2222
}
2323

2424
class MyMap extends Map {
25-
>MyMap : Symbol(MyMap, Decl(extendingSetWithCheckJs.ts, 10, 1))
25+
>MyMap : Symbol(MyMap, Decl(extendingCollectionsWithCheckJs.js, 10, 1))
2626
>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, --, --))
2727

2828
constructor() {
@@ -32,11 +32,12 @@ class MyMap extends Map {
3232
}
3333

3434
class MyWeakMap extends WeakMap {
35-
>MyWeakMap : Symbol(MyWeakMap, Decl(extendingSetWithCheckJs.ts, 16, 1))
35+
>MyWeakMap : Symbol(MyWeakMap, Decl(extendingCollectionsWithCheckJs.js, 16, 1))
3636
>WeakMap : Symbol(WeakMap, 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, --, --))
3737

3838
constructor() {
3939
super();
4040
>super : Symbol(WeakMapConstructor, Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --))
4141
}
4242
}
43+

tests/baselines/reference/extendingSetWithCheckJs.types renamed to tests/baselines/reference/extendingCollectionsWithCheckJs.types

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
//// [tests/cases/compiler/extendingSetWithCheckJs.ts] ////
1+
//// [tests/cases/compiler/extendingCollectionsWithCheckJs.ts] ////
22

3-
=== extendingSetWithCheckJs.ts ===
3+
=== extendingCollectionsWithCheckJs.js ===
44
class MySet extends Set {
55
>MySet : MySet
66
> : ^^^^^
@@ -60,3 +60,4 @@ class MyWeakMap extends WeakMap {
6060
> : ^^^^^^^^^^^^^^^^^^
6161
}
6262
}
63+

tests/baselines/reference/newMap.errors.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ newMap.ts(1,9): error TS2743: No overload expects 1 type arguments, but overload
55
new Map<string>();
66
~~~~~~
77
!!! error TS2743: No overload expects 1 type arguments, but overloads do exist that expect either 0 or 2 type arguments.
8+
new WeakMap<object>();
89

tests/baselines/reference/newMap.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
//// [newMap.ts]
44
new Map<string>();
5+
new WeakMap<object>();
56

67

78
//// [newMap.js]
89
"use strict";
910
new Map();
11+
new WeakMap();

tests/baselines/reference/newMap.symbols

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@
44
new Map<string>();
55
>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, --, --))
66

7+
new WeakMap<object>();
8+
>WeakMap : Symbol(WeakMap, 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, --, --))
9+

tests/baselines/reference/newMap.types

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,9 @@ new Map<string>();
77
>Map : MapConstructor
88
> : ^^^^^^^^^^^^^^
99

10+
new WeakMap<object>();
11+
>new WeakMap<object>() : WeakMap<object, any>
12+
> : ^^^^^^^^^^^^^^^^^^^^
13+
>WeakMap : WeakMapConstructor
14+
> : ^^^^^^^^^^^^^^^^^^
15+

tests/cases/compiler/extendingSetWithCheckJs.ts renamed to tests/cases/compiler/extendingCollectionsWithCheckJs.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// @noEmit: true
55
// @lib: es2017
66

7+
// @filename: extendingCollectionsWithCheckJs.js
78
class MySet extends Set {
89
constructor() {
910
super();
@@ -26,4 +27,4 @@ class MyWeakMap extends WeakMap {
2627
constructor() {
2728
super();
2829
}
29-
}
30+
}

tests/cases/compiler/newMap.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
// @target: es2015
22
// @lib: es6
33
new Map<string>();
4+
new WeakMap<object>();

0 commit comments

Comments
 (0)