Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.8k
Description
Proposal
Additional option forexplicit-function-return-type
ignores all but the final returned function when multiple fat-arrows appear in a function expression before a shared body.
{"rules": {"@typescript-eslint/explicit-function-return-type": ["error", {"allowCurrying":true } ] }}
Purpose
In functional programming, currying is a common pattern. With this pattern a function taking multiple parameters is instead modeled as a unary (1-parameter) function that returns a unary function that returns a unary function, .etc. With arrow functions, you can express this quite elegantly, but the intermediate return types are (usually) not of meaningful importance.
Example
Good: Explicit return given for last function returned by expression.
constfoo=(bar:string)=>(baz:string)=>(buz:string)=>(biz:string) :string=>`${bar}${baz}${buz}${biz}`constfiz=foo('bar')('baz')('buz')('biz')
Bad: Applied all parameters, but still failed to express a return type.
constbadFoo=(bar:string)=>(baz:string)=>(buz:string)=>(biz:string)=>`${bar}${baz}${buz}${biz}`
Note that in the example, there are technically 4 different functions in thefoo
expression. But from a conceptual level, you can think of it as 1 function that must be called 4 times to apply all the parameters.
The type offoo( 'bar' )
is "foo
with the first parameter fixed as'bar'
". Trying to express that explicitly through the type system is a painful exercise in meaningless verbosity.
However, onceall the parameters are given, it still makes sense to enforce an explicit return type for the final returned value.