-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Function argument comparison doesn't match expectations #20541
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
See https://github.com/Microsoft/TypeScript/wiki/FAQ#why-are-functions-with-fewer-parameters-assignable-to-functions-that-take-more-parameters and the entry immediately following it |
Situations like these show why it's often a good idea to omit type annotations where possible. If you just write |
@RyanCavanaugh whoops, I should RTFM, those two sections address this exactly. I could understand the Substitutability argument for these cases, proving that nothing terrible will happen. However, in our use case, it would still be preferable that a type error is thrown, since the mismatch of arguments reveals that a developer on our team is likely mis-using the tool. It would be nice if there was a compiler flag like @Andy-MS that's very interesting less explicit types add more strictness, I'm not sure I understand why that is. I've been assuming the more type annotations I add, the safer I am... |
One issue I have with that FAQ description, it says:
But couldn't the forEach callback just use optional arguments? (value?, index?, array?) => void |
No, because marking a parameter as optional means it might not be provided. But
If you have |
See #17868 and others linked from that issue |
I've amended the FAQ since this is a common counter-question |
Ohhhhhhhh @Andy-MS I see my misunderstanding, I was missing this:
And thanks @RyanCavanaugh for the link, I had been unable to search up a relevant issue. Your comment in there helps me understand:
Also @Andy-MS that makes sense about the |
TypeScript Version: 2.6.1-insiders.20171019 (via TS Playground)
Code
Expected behavior:
Type error about arg2 mismatch, indicating that (cheese: 'wheel') => void is incompatible with () => 'A'.
Actual behavior:
No type error.
The text was updated successfully, but these errors were encountered: