Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable @typescript-eslint/explicit-function-return-type for some(), filter(), forEach()?

Tags:

How to disable @typescript-eslint/explicit-function-return-type for some(), filter(), forEach()?

It's very annoying to declare a boolean return type for some() and filter() and void for forEach() every time.

Invalid

[2, 5, 8, 1, 4].some(elem => elem > 10)

Valid

[2, 5, 8, 1, 4].some((elem):boolean => elem > 10)

I want to be able to use the first pattern (marked "invalid") without getting errors from this rule.

like image 207
Grzegorzg Avatar asked Jul 05 '19 17:07

Grzegorzg


People also ask

How do you bypass ESLint?

How do I turn off rule ESLint? If you want to disable an ESLint rule in a file or on a specific line, you can add a comment. On a single line: const message = 'foo'; console. log(message); // eslint-disable-line no-console // eslint-disable-next-line no-console console.

How do you find the return type of a function in TypeScript?

Use the ReturnType utility type to get the return type of a function in TypeScript, e.g. type T = ReturnType<typeof myFunction> . The ReturnType utility type constructs a type that consists of the return type of the provided function type.

What is return type in TypeScript?

To define the return type for the function, we have to use the ':' symbol just after the parameter of the function and before the body of the function in TypeScript. The function body's return value should match with the function return type; otherwise, we will have a compile-time error in our code.


1 Answers

In your .eslintrc file you can add the following under rules:

{
  ...
  "plugins": ["@typescript-eslint"],
  "rules": {
    ...
    "@typescript-eslint/explicit-function-return-type": {
      "allowExpressions": true
    }
  }
}

According to the documentation on allowExpressions, this would allow you to provide inline callbacks to any function without declaring explicit return types.

like image 138
Patrick Roberts Avatar answered Sep 27 '22 23:09

Patrick Roberts