Skip to content

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

Closed
@skylize

Description

@skylize

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement: plugin rule optionNew rule option for an existing eslint-plugin rulehas prthere is a PR raised to close thispackage: eslint-pluginIssues related to @typescript-eslint/eslint-plugin

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions