3 ms·
I don't understand why this is a "wat"? This is just a) not understanding how `map` works, and b) not understanding the args to `parseInt`. The callback funct
by try_the_bass 4y ago
I don't understand why this is a "wat"? This is just a) not understanding how `map` works, and b) not understanding the args to `parseInt`. The callback function for `map` is passed three args: the current item, the index of that item, and the whole array. `parseInt` takes two args[1]: `number` and `radix`. So you're calling (unrolled):
parseInt("10", 0) => 10
parseInt("10", 1) => NaN
parseInt("10", 2) => 2
This doesn't seem particularly surprising to me?
[1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
- jjnoakes 4y agoMap passes three arguments to parseInt but parseInt only takes two. I think a language with a type system ought to have a way to get a warning for that kind of thing. Especially when it does so in other contexts. I understand why it is this way, but I think it is very surprising and unintuitive behavior.
- jjnoakes 4y agoFor example (continuing my sibling comment), if you unroll it properly, you do get TypeScript complaints. parseInt("10", 0, ["10", "10", "10"]) parseInt("10", 1, ["10", "10", "10"]) parseInt("10", 2, ["10", "10", "10"]) https://www.typescriptlang.org/play?#code/ATAOEMCcGcFMEkB2AXAFAIgIwAZ0BphsCBtLXAs-YSgXQEoAoECGBFDHKzEyizv3PSZgocJGl7AATD37U5tOkA https://www.typescriptlang.org/play?#code/ATAOEMCcGcFMEkB2AX... So TypeScript intentionally suppresses this kind of complaint when matching up function types even though it issues them when calling functions directly. The reason for that has a justification, but in contexts like this, I think it is a WAT-worthy interaction. https://github.com/microsoft/TypeScript/wiki/FAQ#why-are-functions-with-fewer-parameters-assignable-to-functions-that-take-more-parameters https://github.com/microsoft/TypeScript/wiki/FAQ#why-are-fun...