英文:
Typescript or ESLint rule to disallow default returns (in lambdas)?
问题
I came across this error in my code and I felt it should have been picked up by Lint or Typescript itself. Is there is a plugin / additional setting I can set to make "default return" a compile warning?
What I wrote:
activeRegion = regions.find( region => { region.countryCode === activeCountryCode } )
What I should have written:
activeRegion = regions.find( region => region.countryCode === activeCountryCode )
The second does what you want, and returns the region where the condition is true.
The first, because you have actually written a function, expects a return. BUT because JS has a concept of default returns, what it actually becomes is this
activeRegion = regions.find( region => {
region.countryCode === activeCountryCode
return undefined;
} )
which of course is nonsense in this case (nothing will be found), so is there a lint rule that would stop these sneaky default returns from getting added in?
英文:
I came across this error in my code and I felt it should have been picked up by Lint or Typescript itself. Is there is a plugin / additional setting I can set to make "default return" a compile warning?
What I wrote:
activeRegion = regions.find( region => { region.countryCode === activeCountryCode } )
What I should have written:
activeRegion = regions.find( region => region.countryCode === activeCountryCode )
The second does what you want, and returns the region where the condition is true.
The first, because you have actually written a function, expects a return. BUT because JS has a concept of default returns, what it actually becomes is this
activeRegion = regions.find( region => {
region.countryCode === activeCountryCode
return undefined;
} )
which of course is nonsense in this case (nothing will be found), so is there a lint rule that would stop these sneaky default returns from getting added in?
答案1
得分: 3
以下是翻译好的内容:
The rules array-callback-return
and no-unused-expressions
will both be triggered by this code.
To manually add a rule, here's how to do it, from the documentation:
In .eslintrc.js
:
{
...
"rules": {
"array-callback-return": "error",
"no-unused-expressions": "error",
}
}
或者在 yaml 中:
rules:
array-callback-return: error
no-unused-expressions: error
An easier way to work with eslint is by using plugins, or extending a base configuration.
This adds a set of rules, without having to add them one by one.
For example in .eslintrc.js
:
...
extends: ["airbnb-typescript", ... ],
或者
...
plugins: ["@typescript-eslint", ...],
Two commonly used are the one from Airbnb, and the Typescript plugin but there are more.
Note that you need to install the package before adding it to the eslint configuration.
More information in the eslint documentation.
英文:
The rules array-callback-return
and no-unused-expressions
will both be triggered by this code.
To manually add a rule, here's how to do it, from the documentation:
In .eslintrc.js
:
{
...
"rules": {
"array-callback-return": "error",
"no-unused-expressions": "error",
}
}
Or in yaml:
rules:
array-callback-return: error
no-unused-expressions: error
An easier way to to work with eslint is by using plugins, or extending a base configuration.
This adds a set of rules, without having to add them one by one.
For example in .eslintrc.js
:
...
extends: ["airbnb-typescript", ... ],
or
...
plugins: ["@typescript-eslint", ...],
Two commonly used are the one from Airbnb, and the Typescript plugin but there are more.
Note that you need to install the package before adding it to the eslint configuration.
More information in the eslint documentation.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论