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.
[2, 5, 8, 1, 4].some(elem => elem > 10)
[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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With