2

Is it possible to force the following rule by eslint?

  • if the number of function arguments is more than one, use "object" as an argument.

For example,

  • good
    • const f = (x: string) => ...
    • const f = (x: {id: string}) => ...
    • const f = (x: {id: string; name: string}) => ...
  • bad
    • const f= (x: string; y: string) => ...

I checked the official document ( https://eslint.org/docs/rules/ ), but I couldn't find appropriate rules. I wonder if some kind of custom rules can realize this.

1 Answer 1

3

You can use max-params rule. https://eslint.org/docs/rules/max-params Set the max value to 1.

2
  • When I try this, I start getting errors for accessing the parameters of prototype functions like .map(item, index) etc. Is there no way to only have it target function declarations?? Sep 29, 2023 at 19:46
  • Technically, there is no function declaration in JavaScript; both are function definitions. I think you meant to disable it for callbacks. The closest thing I could find was this GitHub issue: github.com/eslint/eslint/issues/9562#issuecomment-353753269. In this issue, it is mentioned that you can extend this eslint rule and filter out the pointers to callbacks. Oct 1, 2023 at 6:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.