4 ms·
From all of the quirks JS has, I think ["10", "10", "10"].map(parseInt) is the sanest. Taken in isolation, the parameters of both Array.prototype.map and pars
by patates 4y ago
From all of the quirks JS has, I think
["10", "10", "10"].map(parseInt)
is the sanest. Taken in isolation, the parameters of both Array.prototype.map and parseInt makes sense, and a typed language wouldn't help there too much. All hope is on ESLint rules to not let you do shortcuts which could burn you.
- danbruc 4y ago[...] and a typed language wouldn't help there too much. That would be impossible in a typed language. The argument of map() is supposed to be a function with signature (T, Integer, T[]) -> R and parseInt() has signature (String, Integer) -> Integer.
- patates 4y agoYeah the third parameter would by chance save the situation here but it's not a rule that you'd always be protected. Also it would be perfectly fine to cast parseInt to (String, Integer, _) in a sufficiently smart type system.
- danbruc 4y agoThe third parameter, I would argue, is a bit wired to begin with. If you need the entire collection, then you are probably doing more than map(), and if you really need it, then you could probably just close over it. But with that somewhat wired third parameter removed, you are right, that is something you could easily run into in a typed language.