You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently, new Map() has type Map<any, any> and new Set() has type Set<any>. I suggest to add an option so that instead new Map() has type Map<never, never> and new Set() has type Set<never>. This should work because Set<never> is assignable to any other set type, and same for maps.
Note that this needs to be an option, otherwise it could break existing code.
📃 Motivating Example
I ran into this issue with code similar to the following:
declarefunctionreturnsNullableMap(): Map<number,number>|null;constmyMap=returnsNullableMap()??newMap();myMap.set("","");//This compiles but shouldn't
The last line is obviously a mistake since the map contains numbers, not strings. But since new Map() has type Map<any, any>, so does myMap, and the last line compiles.
💻 Use Cases
What do you want to use this for?
I would like to completely avoid the any type in my code, and the fact that the code above actually does contain any is not immediately obvious. I use existing options such as noImplicitAny and useUnknownInCatchVariables to avoid any sneaking in at other places, but apparently any can still sneak in in this case. By the way, I would have expected noImplicitAny to cover this case as well, but apparently it doesn't.
What workarounds are you using in the meantime?
In the specific code above I can use const myMap = returnsNullableMap() ?? new Map<never, never>(); and it works fine, myMap has the correct type (Map<number, number>). Note that in my actual code my types are much more complicated than just number, so using never saves a lot of typing compared to using the actual type. In this simplified example however new Map<number, number>() would have worked just as well.
What shortcomings exist with current approaches?
The main issue is that if I forget the <never, never> (or a similar workaround), my code will contain the any type without making it immediately obvious, allowing mistakes like in the example above. Another minor annoyance is that new Map<never, never>() is more verbose than just new Map(), but I can live with that, what I'm more concerned about is any types sneaking into my code where I don't want them.
The text was updated successfully, but these errors were encountered:
You're right, it already works as it should for arrays. Your workaround for sets and maps is better than mine, but it would still be nice to have a tsconfig.json option for that.
🔍 Search Terms
implicit any containers
✅ Viability Checklist
⭐ Suggestion
Currently,
new Map()
has typeMap<any, any>
andnew Set()
has typeSet<any>
. I suggest to add an option so that insteadnew Map()
has typeMap<never, never>
andnew Set()
has typeSet<never>
. This should work becauseSet<never>
is assignable to any other set type, and same for maps.Note that this needs to be an option, otherwise it could break existing code.
📃 Motivating Example
I ran into this issue with code similar to the following:
The last line is obviously a mistake since the map contains numbers, not strings. But since
new Map()
has typeMap<any, any>
, so doesmyMap
, and the last line compiles.💻 Use Cases
I would like to completely avoid the
any
type in my code, and the fact that the code above actually does containany
is not immediately obvious. I use existing options such asnoImplicitAny
anduseUnknownInCatchVariables
to avoidany
sneaking in at other places, but apparentlyany
can still sneak in in this case. By the way, I would have expectednoImplicitAny
to cover this case as well, but apparently it doesn't.In the specific code above I can use
const myMap = returnsNullableMap() ?? new Map<never, never>();
and it works fine,myMap
has the correct type (Map<number, number>
). Note that in my actual code my types are much more complicated than justnumber
, so usingnever
saves a lot of typing compared to using the actual type. In this simplified example howevernew Map<number, number>()
would have worked just as well.The main issue is that if I forget the
<never, never>
(or a similar workaround), my code will contain theany
type without making it immediately obvious, allowing mistakes like in the example above. Another minor annoyance is thatnew Map<never, never>()
is more verbose than justnew Map()
, but I can live with that, what I'm more concerned about isany
types sneaking into my code where I don't want them.The text was updated successfully, but these errors were encountered: