eslint object-shorthand 传入变量错误
eslint object-shorthand error with variable passed in
我有以下功能,它正在设置一个 select2 插件,如果它们是多个,则需要选择保持打开状态,如果不是,则关闭:
function setUpSelects($selects, closeOnSelect) {
$selects.each((i, item) => {
const $item = $(item);
$item.select2({
closeOnSelect: closeOnSelect, // <-- error on this line
minimumResultsForSearch: Infinity,
placeholder: $item.data('placeholder') || $item.attr('placeholder'),
});
});
}
setUpSelects($('select:not([multiple])'), false);
setUpSelects($('select[multiple]'), true);
但是,当我尝试 运行 这段代码时,eslint 检查器给我一个错误(在上面显示的行中):
error Expected property shorthand object-shorthand
我已经搜索并阅读了文档,但它没有显示您打算如何使用变量,this question 上不被接受的答案似乎认为它可能是 eslint 中的一个错误(尽管我没有找到支持这一点的证据)
有没有办法让它工作,或者我应该只禁用该行的规则?
来自 eslint 的关于此问题的 excerpt:
Require Object Literal Shorthand Syntax (object-shorthand) - Rule Details
This rule enforces the use of the shorthand syntax. This applies to
all methods (including generators) defined in object literals and any
properties defined where the key name matches name of the assigned
variable.
改变
closeOnSelect: closeOnSelect
只是
closeOnSelect
此rule checks that object literal shorthand syntax is used, e.g {a, b}
instead of {a: a, b: b}
. The rule is configurable, see options了解更多详情。
尽管 shorthand 语法很方便,但在某些情况下您可能不想强制使用它。您可以在配置中禁用检查:
// .eslintrc.json
{
"rules": {
// Disables the rule. You can just remove it,
// if it is not enabled by a parent config.
"object-shorthand": 0
}
}
在 TSLint 的情况下有一个不同的 option:
// tslint.json
{
"rules": {
// Disables the rule. You can just remove it,
// if it is not enabled by a parent config.
"object-literal-shorthand": false
}
}
想用键定义对象,但不能使用任何键。试试这个。
界面图{
[键:字符串]:字符串 |不明确的
}
const HUMAN_MAP: Map = {
draft: "Draft",
}
export const human = (str: string) => HUMAN_MAP[str] || str
我有以下功能,它正在设置一个 select2 插件,如果它们是多个,则需要选择保持打开状态,如果不是,则关闭:
function setUpSelects($selects, closeOnSelect) {
$selects.each((i, item) => {
const $item = $(item);
$item.select2({
closeOnSelect: closeOnSelect, // <-- error on this line
minimumResultsForSearch: Infinity,
placeholder: $item.data('placeholder') || $item.attr('placeholder'),
});
});
}
setUpSelects($('select:not([multiple])'), false);
setUpSelects($('select[multiple]'), true);
但是,当我尝试 运行 这段代码时,eslint 检查器给我一个错误(在上面显示的行中):
error Expected property shorthand object-shorthand
我已经搜索并阅读了文档,但它没有显示您打算如何使用变量,this question 上不被接受的答案似乎认为它可能是 eslint 中的一个错误(尽管我没有找到支持这一点的证据)
有没有办法让它工作,或者我应该只禁用该行的规则?
来自 eslint 的关于此问题的 excerpt:
Require Object Literal Shorthand Syntax (object-shorthand) - Rule Details
This rule enforces the use of the shorthand syntax. This applies to all methods (including generators) defined in object literals and any properties defined where the key name matches name of the assigned variable.
改变
closeOnSelect: closeOnSelect
只是
closeOnSelect
此rule checks that object literal shorthand syntax is used, e.g {a, b}
instead of {a: a, b: b}
. The rule is configurable, see options了解更多详情。
尽管 shorthand 语法很方便,但在某些情况下您可能不想强制使用它。您可以在配置中禁用检查:
// .eslintrc.json
{
"rules": {
// Disables the rule. You can just remove it,
// if it is not enabled by a parent config.
"object-shorthand": 0
}
}
在 TSLint 的情况下有一个不同的 option:
// tslint.json
{
"rules": {
// Disables the rule. You can just remove it,
// if it is not enabled by a parent config.
"object-literal-shorthand": false
}
}
想用键定义对象,但不能使用任何键。试试这个。
界面图{ [键:字符串]:字符串 |不明确的 }
const HUMAN_MAP: Map = {
draft: "Draft",
}
export const human = (str: string) => HUMAN_MAP[str] || str