question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

2.4: Readonly<Map<k,v>> vs. ReadonlyMap<k,v>

See original GitHub issue

TypeScript Version: 2.4.1

Code

function GenMap() : ReadonlyMap<string, number> {
    const theMap = new Map<string, number>();
    theMap.set("one", 1);
    theMap.set("two", 2);
    return Object.freeze(theMap);
}

Expected behavior: Compiles without error in tsc@<2.4.0

Actual behavior:

TryX.ts(5,5): error TS2322: Type 'Readonly<Map<string, number>>' is not assignable to type 'ReadonlyMap<s
tring, number>'.
  Property '[Symbol.iterator]' is missing in type 'Readonly<Map<string, number>>'.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
NaridaLcommented, Sep 18, 2017

@mhegazy , as @sylvanaar 's comment/link shows, calling Object.freeze on a map doesn’t actually stop new keys being added with .set(). Do the suggested overloads still make sense in this case?

1reaction
ThomasdenHcommented, Jan 31, 2019

Readonly<Map<_, _>> is not the same as ReadonlyMap<_, _>. I think the latter is a subset of the former, so the best solution would be to make Readonly<Map<_, _>> assignable to ReadonlyMap<_, _>.

Read more comments on GitHub >

github_iconTop Results From Across the Web

javascript - Using ReadonlyMap<K, V> type
Short answer: you're doing it right. ReadonlyMap<K, V> is essentially a supertype of Map<K, V> since the methods and properties it does have ......
Read more >
Documentation - Mapped Types
Generating types by re-using an existing type.
Read more >
Solved: Can a Key Value Map be set up as read only?
I want to use KVM so that I can set a value per environment. I would want to ensure that other proxies could...
Read more >
Map - scala-library 2.7.6 javadoc - javadoc.io
This trait represents mutable maps. Concrete map implementations just have to provide functionality for the abstract methods in scala.collection.
Read more >
Map (Groovy JDK enhancements)
Iterates through this Map transforming each map entry into a new value using the transform closure returning the collector with all transformed values...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found