Skip to content

Type error for new Map from undefined Iterable #37779

@JaredNeil

Description

@JaredNeil

TypeScript Version: 3.8.3

Search Terms: Map MapConstructor Iterable Iterator

Code

let undefinedArray: [string, string][] | undefined;
const map1 = new Map(undefinedArray);

let definedInterable: IterableIterator<[string, string]> = new Set<string>().entries();
const map2 = new Map(definedInterable);

let undefinedIterable: IterableIterator<[string, string]> | undefined;
const map3 = new Map(undefinedIterable); // Type error here

Expected behavior:
Passes type check because new Map(undefined) is a valid call to the Map constructor.
Actual behavior:

No overload matches this call.
  Overload 1 of 4, '(iterable: Iterable<readonly [string, string]>): Map<string, string>', gave the following error.
    Argument of type 'IterableIterator<[string, string]> | undefined' is not assignable to parameter of type 'Iterable<readonly [string, string]>'.
      Type 'undefined' is not assignable to type 'Iterable<readonly [string, string]>'.
  Overload 2 of 4, '(iterable: Iterable<readonly [string, string]>): Map<string, string>', gave the following error.
    Argument of type 'IterableIterator<[string, string]> | undefined' is not assignable to parameter of type 'Iterable<readonly [string, string]>'.
      Type 'undefined' is not assignable to type 'Iterable<readonly [string, string]>'.
  Overload 3 of 4, '(entries?: readonly (readonly [unknown, unknown])[] | null | undefined): Map<unknown, unknown>', gave the following error.
    Argument of type 'IterableIterator<[string, string]> | undefined' is not assignable to parameter of type 'readonly (readonly [unknown, unknown])[] | null | undefined'.
      Type 'IterableIterator<[string, string]>' is missing the following properties from type 'readonly (readonly [unknown, unknown])[]': length, concat, join, slice, and 14 more.(2769)

Playground Link: https://www.typescriptlang.org/play/#code/DYUwLgBArgdgJiAZgSxiOBBATlghgTwC4IBtAZzC1QHMAaCCqmagXRJYgB9p4lV0A3ACgAxgHsYFCAFtcABwCMEALwQ0AdwgBZeQApYCFGkw4CASmFDQkQ-zgBJGGBB4ARqGL3nb0F5e4wMSwAHnJKGnpGGhYAPhU1EE0AZXBgqOYY3TMAOhAnKhAyLOFxSUhZOQAmeI1tPVtjR29cdxALIStwHgb0Px8QT2bWvoCg0PS6BnDmWK5uvmMSiSkKgGYaxLq5fV4jXqHQdqA

Related Issues: None

Suggested Fix:
The MapConstructor interface in es2015.iterable.d.ts should accept a single optional argument.

diff --git a/src/lib/es2015.iterable.d.ts b/src/lib/es2015.iterable.d.ts
index 374aad9322..6f9b786c1d 100644
--- a/src/lib/es2015.iterable.d.ts
+++ b/src/lib/es2015.iterable.d.ts
@@ -137,7 +137,7 @@ interface ReadonlyMap<K, V> {
 }
 
 interface MapConstructor {
-    new <K, V>(iterable: Iterable<readonly [K, V]>): Map<K, V>;
+    new <K, V>(iterable?: Iterable<readonly [K, V]>): Map<K, V>;
 }
 
 interface WeakMap<K extends object, V> { }

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptDomain: lib.d.tsThe issue relates to the different libraries shipped with TypeScriptHelp WantedYou can do this

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions