-
Notifications
You must be signed in to change notification settings - Fork 12.8k
checkJs should be as strict as TS about matching function arity #31888
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
Here's a smaller js-only repro (not required, but makes it easier for me to test): /** @param {{
(fn: (a: number) => void): number;
(fn: (a: number, b: number) => void): string
}} id */
export function test(id) {
/** @type {string} */
var val = id((a, b) => a + b);
} Errors (with strict on):
|
Unfortunately, this is a side effect of the way that we handle functions in Javascript. If a function doesn't have all of its parameters annotated, then the compiler doesn't require all of them to be passed. Internally, this means the signature gets marked with To work around this, annotate /**
* @param {number} a
* @param {number} b
*/
(a, b) => {} To fix this, we'd need to make
2a. Require all parameters for arrow functions that are not immediately assigned. There are a lot of options here. |
This option sounds great! (independent of if the others should be done, too) As you said, the error is already given elsewhere with |
@DanielRosenwasser @weswigham for additional opinions. |
If the function has a contextual type, then I think it's fair game to treat it similarly to a function with parameter annotations. |
Tbh, I'm a fan of just not doing this and bringing the JS behavior inline with TS - we have a way in js to specify optional arguments, and so if errors are on, you can fix the error, and if errors aren't on, the required arity isn't going to be unjustly noisy or anything, since errors aren't on. |
You mean with |
I think it is. It's worth noting that we also still get arity hints from parameter initializers, in addition to jsdoc comments. |
I think this is a pretty easy change to make, so I think we should just run the user tests on a PR to see how many new errors turn up. Looking at actual code will make it easier to think about how it will affect people, I think. |
This is an experiment in fixing #31888. This is the simplest experiment, which requires all parameters to be provided in JS, same as TS. If this doesn't break the user tests that are written in JS too badly, then I won't proceed to the more complex experiments, which are to require all paremeters for contextually typed functions, or to require all parameters when `"noImplicitAny": true`.
The simplest solution -- requiring all parameters in JS -- is up at #35153 |
@weswigham More details at the PR, but the upshot is that lots of webpack breaks, which is evidence that JSDoc is too onerous a requirement for specifying optional parameters. |
I'm going to close this because I think Wesley's change in 3.9 #37173 addresses this bug adequately. |
TypeScript Version: 3.6.0-dev.20190613
Search Terms:
overload
higher order function
checkjs
Code
Expected behavior:
f is a binary function.
Actual behavior:
f is a unary function.
Playground Link:
N/A
Related Issues:
This issue makes
util.promisify
in@types/node
unusable with checkjs. SeeDefinitelyTyped/DefinitelyTyped#33340
The text was updated successfully, but these errors were encountered: