Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[explicit-function-return-type] request option for curried arrow functions #193

Closed
skylize opened this issue Feb 3, 2019 · 4 comments · Fixed by #538
Closed

[explicit-function-return-type] request option for curried arrow functions #193

skylize opened this issue Feb 3, 2019 · 4 comments · Fixed by #538
Labels
enhancement: plugin rule option New rule option for an existing eslint-plugin rule has pr there is a PR raised to close this package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@skylize
Copy link

skylize commented Feb 3, 2019

Proposal

Additional option for explicit-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.

const foo = ( bar: string ) => ( baz: string ) => ( buz: string ) => ( biz: string ) : string =>
    `${ bar } ${ baz } ${ buz } ${ biz }`

const fiz = foo( 'bar' )( 'baz' )( 'buz' )( 'biz' )

Bad: Applied all parameters, but still failed to express a return type.

const badFoo = ( bar: string ) => ( baz: string ) => ( buz: string ) => ( biz: string ) =>
    `${ bar } ${ baz } ${ buz } ${ biz }`

Note that in the example, there are technically 4 different functions in the foo 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 of foo( '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, once all the parameters are given, it still makes sense to enforce an explicit return type for the final returned value.

@skylize skylize added package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for maintainers to take a look labels Feb 3, 2019
@j-f1 j-f1 added enhancement: plugin rule option New rule option for an existing eslint-plugin rule and removed triage Waiting for maintainers to take a look labels Feb 3, 2019
@bradzacher
Copy link
Member

good idea and pretty easy to implement!

@lbssousa
Copy link

Any news about this issue?

@bradzacher
Copy link
Member

bradzacher commented Apr 25, 2019

Accepting PRs from the community (we love new contributors!!)
Otherwise will get done eventually - we're only a few volunteers (fixes take priority over features when I'm picking new work).

@Svish
Copy link
Contributor

Svish commented May 16, 2019

I think I actually managed to implement it! 😳 #538 🎉

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement: plugin rule option New rule option for an existing eslint-plugin rule has pr there is a PR raised to close this package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants