-
Notifications
You must be signed in to change notification settings - Fork 9
Potential footgun with 'cause' property / not accepting Error #38
Comments
Perhaps if the passed in object is an |
That seems like it'd be very confusing. I don't think it will be a common mistake; the TypeScript type for Error's second argument is |
I can definitely see that magic could be an even worse cure than the disease. As to the commonness of the mistake - I can find multiple examples in this repo already. On Typescript: However, I actually mocked up the likely type definitions, and since Typescript 4.4, the the type of the error in a catch clause is actually In my defence this was only changed about a month ago! So I'm not sure what my conclusion is. Possibly a lint and/or a strong recommendation to ensure that |
Those examples (except for the last) predate the change to an options bag, and the last is continuing a discussion which began before that change. |
As the question has already been answered, I'm closing this. Please feel free to re-open if it is not the case. |
I'm not sure it's been answered. I'm fairly convinced this will be a common error - perhaps this could be converted to a discussion for potential mitigations? |
@fowl2 As stated by bakkot above, the examples you pointed out in the repo are outdated and the option bag was introduced afterward. I still don't get your point about how the option bag in the error constructor could be different from all other APIs that using the option bag pattern. The pattern is used widely and I don't believe people who learn a new API in a commonly used pattern would have problems understanding that. |
Consider the following:
Am I correct in saying that that in code would produce an
Error
like thisinstead of the intended
?
Due to both the
options
parameter ANDError
having acause
property.For those familiar with similar features in other languages (eg. C#) this will be an extremely common mistake, and it won't always be immediately obvious as it's just one intermediate "cause" being missing. It'd also be difficult for TypeScript to pick up on a problem like this without explicit.
The text was updated successfully, but these errors were encountered: