Skip to content

Map constructor doesn't work with target ES5, lib ES6 #8407

Closed

Description

TypeScript Version:

1.7.5 / 1.8.0-beta / nightly (1.9.0-dev.20160430)

Code

const x = new Map([["a", 1]]); // Error
const y = new Map<string, number>([["a", 1]]); // Error
const z = new Map([<[string, number]>["a", 1]]); // Works
{
    "compilerOptions": {
        "target": "es5",
        "lib": ["es2016"]
    }
}

Expected behavior:
No errors

Actual behavior:
Errors are reported on the first two lines:

Argument of type '(string | number)[][]' is not assignable to parameter of type 'Iterable<[{}, {}]>'. 
Argument of type '(string | number)[][]' is not assignable to parameter of type 'Iterable<[string, number]>'.

I think the reason is that the compiler doesn't use iterators to resolve the type arguments when the target is ES5. Adding an overload to the Map constructor, which takes an array of tuples instead of an iterator, would probably fix this. I'm not sure whether this happens on more locations though, if so it might be worth to pick up iterators in the compiler even when the target is ES5.

Activity

  1. mhegazy commented on May 1, 2016

    @mhegazy
    Contributor

    Iterator support are not guaranteed to exist if target is not ES6, so i think the assumption we have today is accurate. I agree with your proposal to add an array overload for Map, WeakMap, Set and WeakSet. A PR would be appreciated.

  2. modified the milestones: , TypeScript 2.0 on May 1, 2016
  3. ivogabe commented on May 2, 2016

    @ivogabe
    ContributorAuthor

    I've opened PR #8422

  4. ivogabe commented on May 5, 2016

    @ivogabe
    ContributorAuthor

    Thanks!

  5. locked and limited conversation to collaborators on Jun 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptDomain: lib.d.tsThe issue relates to the different libraries shipped with TypeScriptFixedA PR has been merged for this issueHelp WantedYou can do this

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions