【问题标题】:Getting linting error - error Unexpected empty object pattern no-empty-pattern出现 linting 错误 - 错误 Unexpected empty object pattern no-empty-pattern
【发布时间】:2019-12-27 18:42:14
【问题描述】:

我收到以下代码的此 linting 错误-“错误意外的空对象模式 no-empty-pattern”。有谁知道如何解决这个问题?它是一个 tsx 文件。

const stateToProps = ({}, { data = [], filters = {}, staticFilters = 
  [{}] }) => {
  const allFilters = staticFilters ? Object.assign({}, filters, ...staticFilters) : filters;
  const newData = getFilteredRows(allFilters, data);
  return {
      data: newData,
      unfilteredData: data,
  };
};
//called like this
export const NodeList = connect<{}, {}, CustomNodeTableProps>(stateToProps)(CustomNodeTable);

【问题讨论】:

    标签: javascript reactjs typescript eslint


    【解决方案1】:

    在转换 TypeScript 后,我​​通过 ESLint demo 应用程序运行了这段代码; linter 错误来自第一行的{}

    const stateToProps = ({}, // rest of the function
    

    no-empty-pattern 旨在捕获看似使用解构但不分配任何变量的代码。在上面的代码中,stateToProps 接受第一个参数,但无论那个参数是什么,它都被解构为一个空对象 {},并且没有分配任何内容。

    如果第一个参数很重要,您可以更改代码以对其进行分解:

    const stateToProps = ({importantThing},
    

    如果不是,表明我们不在乎:

    const stateToProps = (_,
    

    或者,设置一个默认值。

    const stateToProps = (importantThing = {},
    

    这些更改中的每一个都解决了 ESLint 警告。

    【讨论】:

      【解决方案2】:

      在这种非空模式的情况下,我倾向于通过添加以下注释来禁用该特定行的 lint 规则。

      JavaScript:

        // eslint-disable-next-line no-empty-pattern
        const stateToProps = ({}, otherParams, // rest of the function
          ... 
      

      打字稿:

        // eslint-disable-next-line @typescript-eslint/no-empty-pattern
        const stateToProps = ({}, otherParams, // rest of the function
          ... 
      

      这样做的原因首先,我不想全局关闭 no-empty-pattern,其次我也不想收集大量 lint 警告,因为这会模糊重要的警告和错误。

      【讨论】:

        猜你喜欢
        • 2020-05-04
        • 2018-05-26
        • 1970-01-01
        • 2020-09-03
        • 2016-11-24
        • 2015-04-25
        • 2017-06-20
        • 2015-08-11
        相关资源
        最近更新 更多