Skip to content

Map constructor should accept readonly tuples #29721

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ark120202 opened this issue Feb 4, 2019 · 2 comments · Fixed by #50449
Closed

Map constructor should accept readonly tuples #29721

ark120202 opened this issue Feb 4, 2019 · 2 comments · Fixed by #50449
Assignees
Labels
Bug A bug in TypeScript Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript Fix Available A PR has been opened for this issue Fixed A PR has been merged for this issue

Comments

@ark120202
Copy link

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?

@RyanCavanaugh RyanCavanaugh added Bug A bug in TypeScript Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript labels Feb 5, 2019
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 3.4.0 milestone Feb 5, 2019
@sheetalkamat sheetalkamat added the Fixed A PR has been merged for this issue label Mar 13, 2019
@ark120202
Copy link
Author

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

@chharvey
Copy link

chharvey commented Jan 9, 2021

@ark120202 @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;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript Fix Available A PR has been opened for this issue Fixed A PR has been merged for this issue
Projects
None yet
5 participants