Skip to content

Map constructor should accept readonly tuples #29721

Closed

Description

@ark120202

TypeScript Version: 3.4.0-dev.20190202

Search Terms:

Readonly tuple, map, as const, const context

Code

const pairs = [['1', 1], ['2', 2]] as const
new Map(pairs);

Expected behavior:

No error

Actual behavior:

TS2345: Argument of type 'readonly [readonly ["1", 1], readonly ["2", 2]]' is not assignable to parameter of type 'readonly [{}, {}][]'.
  Type 'readonly ["1", 1] | readonly ["2", 2]' is not assignable to type '[{}, {}]'.
    Type 'readonly ["1", 1]' is missing the following properties from type '[{}, {}]': pop, push, reverse, shift, and 6 more.

Playground Link:

Related Issues:


The same issue applies to some libraries on DefinitelyTyped. Maybe readonly tuples should be added to Common mistakes, like arrays?

Activity

  1. added a commit that references this issue on Mar 13, 2019
    027d65a
  2. ark120202 commented on Mar 13, 2019

    @ark120202
    Author

    Sheetal Nandi (@sheetalkamat) the same issue also applies to WeakMap, could it be fixed as well or should I open a new issue?

  3. chharvey commented on Jan 9, 2021

    @chharvey

    ark120202 Sheetal Nandi (@sheetalkamat) — Can we follow up on this with WeakMap? I’m having the same issue as OP where I’m trying to pass a readonly array of readonly tuples to the WeakMap constructor and getting a TypeError.

    interface WeakMapConstructor {
    -    new <K extends object = object, V = any>(entries?: readonly [K, V][] | null): WeakMap<K, V>;
    +    new <K extends object = object, V = any>(entries?: readonly (readonly [K, V])[] | null): WeakMap<K, V>;
        readonly prototype: WeakMap<object, any>;
    }
    declare var WeakMap: WeakMapConstructor;
  4. locked as resolved and limited conversation to collaborators on Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

BugA bug in TypeScriptDomain: lib.d.tsThe issue relates to the different libraries shipped with TypeScriptFix AvailableA PR has been opened for this issueFixedA PR has been merged for this issue

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions