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] Is type inference not allowed? #434

Closed
ghost opened this issue Apr 16, 2019 · 4 comments
Closed

[explicit-function-return-type] Is type inference not allowed? #434

ghost opened this issue Apr 16, 2019 · 4 comments
Labels
package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin working as intended Issues that are closed as they are working as intended

Comments

@ghost
Copy link

ghost commented Apr 16, 2019

Repro

{
  "rules": {
    "@typescript-eslint/explicit-function-return-type": "error"
  }
}
const fn1: (i: number) => string = function(i) { return i.toString(); }; // Error
const fn2: (i: number) => string = i => i.toString(); //Error

Expected Result
Expect no error because these functions are inferred to return string.

Actual Result
Error. (Missing return type on function.)

Additional Info

Versions

package version
@typescript-eslint/eslint-plugin 1.6.0
@typescript-eslint/parser 1.6.0
TypeScript 3.4.3
ESLint 5.16.0
node 10.15.3
npm 6.4.1
@ghost ghost added package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for maintainers to take a look labels Apr 16, 2019
@bradzacher bradzacher added working as intended Issues that are closed as they are working as intended awaiting response Issues waiting for a reply from the OP or another party and removed triage Waiting for maintainers to take a look awaiting response Issues waiting for a reply from the OP or another party labels Apr 16, 2019
@bradzacher
Copy link
Member

Check out the options for the rule: https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/explicit-function-return-type.md#options

I believe you'll want allowTypedFunctionExpressions

@hanneswidrig
Copy link

This might be a dumb question but I am trying to understand how I use these rules for [explicit-function-return-type] inside my React Typescript application on Create-React-App 3.0.

{
  "plugins": ["react", "react-hooks", "@typescript-eslint"],
  "extends": [
    "plugin:@typescript-eslint/recommended",
    "prettier",
    "prettier/@typescript-eslint"
  ],
  "rules": {
    "react-hooks/rules-of-hooks": "error",
    "react-hooks/exhaustive-deps": "warn",
    "allowExpressions ": true,
    "allowTypedFunctionExpressions": true
  },
  "parser": "@typescript-eslint/parser",
  "globals": {
    "Atomics": "readonly",
    "SharedArrayBuffer": "readonly"
  },
  "env": {
    "browser": true,
    "es6": true
  },
  "parserOptions": {
    "project": "./tsconfig.json",
    "ecmaFeatures": {
      "jsx": true
    },
    "ecmaVersion": 2018,
    "sourceType": "module"
  }
}

This is my .eslintrc.json file. I am wondering how I add those rules like AllowExpressions.

"@typescript-eslint/explicit-function-return-type": 0, Adding this to the rules made it go away but I am not sure if this is completely turning off all rules. I still want to require function return type when it's not inferred.

image

@bradzacher
Copy link
Member

@hanneswidrig

Check out the eslint docs for configuring rules.
https://eslint.org/docs/user-guide/configuring#using-configuration-files

The available options for this rule are listed here
https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/explicit-function-return-type.md#options

Let me know if you need more help.

@hanneswidrig
Copy link

@bradzacher I just figured it out, thanks for the resources though.

For anyone working with React and wondering about this, see the example below.
image

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin working as intended Issues that are closed as they are working as intended
Projects
None yet
Development

No branches or pull requests

2 participants