Skip to content

[explicit-function-return-type] Only apply to generic functions #730

Closed
@AnyhowStep

Description

@AnyhowStep

The template seems to be for bug reports but I have a feature request.

I would like a way to configure explicit-function-return-type so that it only forces generic functions to have an explicit return type.

//Error, generic functions must have explicit return type
function foo<T extends SomeType> (t : T) {
    return /*snip some code using `t`*/;
}

//OK! Not generic, so no need for explicit return type
function blah (t : SomeType) {
    return /*snip some code using `t`*/;
}

Rationale

In general, tsc's type inference is very fast.

However, when it comes to complicated generic types, type inference can fail and/or infer... unfavorable types. This is especially true of generic function return types.

I personally just had a check time performance issue that was fixed by adding an explicit return type annotation to one generic function.

It cut down check time by five minutes


This missing one line of code caused me weeks of suffering =(

I do not wish for others and future me to trip over this again =(

In general, I tend to always add explicit retun types to all my generic functions. The one time I forgot, it messed me up =(


Letting tsc infer return type

image

Explicitly setting return type

image

The difference is staggering

Metadata

Metadata

Labels

accepting prsGo ahead, send a pull request that resolves this issueenhancement: plugin rule optionNew rule option for an existing eslint-plugin rulepackage: 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