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.

Sign up to request clarification or add additional context in comments.

2 Comments

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??
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.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.