bennouli
bennouli

Reputation: 45

missing return type in function of object returned by a factory function

I have come across a false error from ESLint, using @typescript-eslint, when trying to define the return type of a function inside an object which is returned by a function.

For example, we have a function returning an object with functions like this:

const createStuff = (): StuffReturn => {
    return {
        doStuff: () => { // here ESLint will throw a warning
            return;
        },
    };
};

Now when I define the interface StuffReturn for the return type of this function as such ...

interface StuffReturn {
    doStuff: () => void;
}

... and then run ESLint on the command line, I get the following error:

13:18 warning  Missing return type on function  @typescript-eslint/explicit-function-return-type

maybe I should create an issue on GitHub for this, but I am not sure whether it's actually a bug or I am missing some deeper meaning from this behavior.

Upvotes: 2

Views: 2679

Answers (1)

Luïs
Luïs

Reputation: 2833

You have to allow typed function expressions in your eslintrc:

"rules": {
  "@typescript-eslint/explicit-function-return-type": ["error", {
    "allowTypedFunctionExpressions": true
  }],
}

This will allow type annotations on the variable of a function expression rather than on the function directly as stated in the docs.

Note: If you are using typescript-eslint v2 or higher, this option will be true by default.

Upvotes: 1

Related Questions