ts-reset icon indicating copy to clipboard operation
ts-reset copied to clipboard

Feature idea: Change Map from any --> unknown

Open Zamiell opened this issue 2 years ago • 7 comments

Both of the default generic parameters of Map are any. You should change them to unknown for the same reasons that you e.g. change JSON.parse.

For a more in-depth discussion on this, see: https://github.com/microsoft/TypeScript/issues/52552

Zamiell avatar Feb 21 '23 18:02 Zamiell

Maps already have a generic option: Map<K, T>() where as JSON.parse does not, I think the goal is to improve existing features and not add generics everywhere, that said, if you do not set the K & T it will be any.

Up for debate, it is safer, but you can also just add a starting generic.

m10rten avatar Feb 23 '23 20:02 m10rten

I'm aware that maps have generic parameters. The point of this issue is that sometimes programmers forget to specify the generic parameters, so the default generic parameters are used, which means that any slips into the codebase, which should be solved at the type-level.

Zamiell avatar Feb 23 '23 20:02 Zamiell

Agree, this issue deserves to stay open! My response is on the PR

mattpocock avatar Feb 23 '23 21:02 mattpocock

FYI, the title should be "any --> unknown" :slightly_smiling_face:

nmay231 avatar Mar 18 '23 21:03 nmay231

Ultimately this is just about the default value, so Map<K = unknown, V = unknown> right?

Crisfole avatar Jan 11 '24 18:01 Crisfole

yes

Zamiell avatar Jan 11 '24 19:01 Zamiell

So, what is the status of the PR?

Seems like the test is accurate - I had the same defined myself in a big repo at work, and it has not caused any issues - i suggest this ends up being merged.

arnorhs avatar May 10 '24 12:05 arnorhs