Description
I am writing a helper function that provides functionality often associated with the traditional switch statement, but in a more functional style. In doing so, I have come across a bug.
Consider the following code, which is a simplification of the helper I'm writing:
const match = <T>(value: T) => ({
on: <U>(prediction: T, outcome: U) => prediction === value
? matched(outcome)
: match(value),
result: () => undefined
});
const matched = <T>(outcome: T) => ({
on: () => matched(outcome),
result: () => outcome
});
const num = Math.random() > 0.5 ? 1 : 2;
const numString = match(num)
.on(1, 'one')
.on(2, 'two')
.result();
When hovering the first on
call, we see the expected information:
However, when hovering the second on
call, which is a call to the on
property of the result of either match
or matched
, we get:
As we can see, the types are just fine, but the argument names are replaced with 'arg0' and 'arg1'. This in itself could be intentional for some reason, and hence not a bug, but here's what leads me to consider this a bug:
When removing the last argument to the second on
call, IntelliSense gives us an error as expected, but with an error text containing one of the previously replaced argument names, 'outcome':
The same goes for 'prediction' if both arguments are removed.
I suspect this might be a result of trying to derive the parameter names from the second definition of on
(as a result of the matched
function), which would be inconsistent as the rest of the call signature is clearly derived from the first definition (as a result of the match
function). Or I am completely wrong, which is also very much possible.
Anyway, this is misleading. What I expect is for both the call signature and error text to contain 'prediction' and 'outcome' as the parameter names, or at the very least for these to be consistent.
PS: The full thing is at https://github.com/korkje/match/blob/master/src/index.ts, and exhibits the same weirdness.