Remove Map<any, any> constructor overload #60051
Labels
Awaiting More Feedback
This means we'd like to hear from more people who would be helped by this feature
Suggestion
An idea for TypeScript
⚙ Compilation target
ES2017
⚙ Library
lib.es2015.iterable.d.ts
Missing / Incorrect Definition
This isn't exactly the kind of issue this form (Library issue) seems to be made for, but it looked like the closest match.
The
Map
constructor has an explicit overload to makenew Map()
produce aMap<any, any>
, rather than safer option of allowing normal type inference to occur. I think the only change that needs to happen is removing the overload. This was already brought up in #52552, but that was closed because it did not provide a clear usecase, and the focus was on inconsistency betweenMap
andSet
.For me, the usecase is about
Map
alone - removing a source of silent and infectiousany
s.noImplicitAny
is a recommended setting for good reason, but is undermined by the presence ofany
s in library types. In fact, I am creating this issue after fixing a bug in my own code that was hidden by this typing.A fair counterargument is that it may break existing code. My guess - total speculation - is that the override was added in the past when inference was not as effective, and it is no longer necessary in most cases. Where the empty map is meant to conform to a contextual type, it works fine. Toying around with it myself, I find two main cases where it breaks:
Map<any, any>
was literally the intent. I'd strongly argue these cases should be explicit.Map
is constructed without contextual types, but it is meant to conform to them later. As a result, the code in-between those places is unsafe, as in the example below. Though unsafe, this might be a common pattern in practice that the change would break. (Ofc a solution is to explicitly type theMap
construction.)Sample Code
Documentation Link
The MDN doc link is here, though it's not super relevant to this particular question: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/Map
The text was updated successfully, but these errors were encountered: