简体   繁体   English

传入变量的eslint对象速记错误

[英]eslint object-shorthand error with variable passed in

I have the following function that is setting up a select2 plugin, which needs selects to stay open if they are multiple but closed if they are not:我有以下功能正在设置一个 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);

However, when I try to run this code, the eslint checker is giving me an error (on the line shown above) of:但是,当我尝试运行此代码时,eslint 检查器给了我一个错误(在上面显示的行中):

error Expected property shorthand object-shorthand错误预期属性简写对象简写

I have done a search and read the docs but it doesn't show how you are meant to use a variable and the unaccepted answer on this question seems to think it may be a bug in eslint (although I have found no evidence to support that)我已经进行了搜索并阅读了文档,但它并没有显示您打算如何使用变量,并且这个问题上未接受的答案似乎认为它可能是 eslint 中的一个错误(尽管我没有发现任何证据支持这一点) )

Is there a way to make this work or should I just disable the rule for that line?有没有办法使这项工作正常进行,或者我应该禁用该行的规则?

An excerpt from eslint regarding the issue: eslint 关于这个问题的摘录

Require Object Literal Shorthand Syntax (object-shorthand) - Rule Details需要对象文字速记语法 (object-shorthand) - 规则详细信息

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.这适用于在对象字面量中定义的所有方法(包括生成器)以及在键名称与分配变量的名称匹配的情况下定义的任何属性。

Change改变

closeOnSelect: closeOnSelect

to just只是

closeOnSelect

This rule checks that object literal shorthand syntax is used, eg {a, b} instead of {a: a, b: b} .规则检查是否使用了对象文字速记语法,例如{a, b}而不是{a: a, b: b} The rule is configurable, see options for more details.该规则是可配置的,有关更多详细信息,请参阅选项

Despite this shorthand syntax is convenient, in some cases you may not want to force it usage.尽管这种速记语法很方便,但在某些情况下您可能不想强制使用它。 You can disable the check in your config:您可以在配置中禁用检查:

// .eslintrc.json

{
  "rules": {
    // Disables the rule. You can just remove it,
    // if it is not enabled by a parent config.
    "object-shorthand": 0
  }
}

In case of TSLint there is a different option :TSLint 的情况下,有一个不同的选择

// tslint.json

{
  "rules": {
    // Disables the rule. You can just remove it,
    // if it is not enabled by a parent config.
    "object-literal-shorthand": false
  }
}

Wants to define Object with keys and can't use any.想用键定义对象,不能使用任何。 Try this.尝试这个。

interface Map { [key: string]: string | interface Map { [key: string]: string | undefined }不明确的 }

const HUMAN_MAP: Map = {
  draft: "Draft",
}

export const human = (str: string) => HUMAN_MAP[str] || str

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM