2.4: Readonly<Map<k,v>> vs. ReadonlyMap<k,v>
See original GitHub issueTypeScript 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:
- Created 6 years ago
- Comments:9 (5 by maintainers)
Top 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 >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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@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?
Readonly<Map<_, _>>
is not the same asReadonlyMap<_, _>
. I think the latter is a subset of the former, so the best solution would be to makeReadonly<Map<_, _>>
assignable toReadonlyMap<_, _>
.