【问题标题】:Is there an ESLint rule that does the same as 'no-floating-promises' did in the now deprecated TSLint?是否存在与现在已弃用的 TSLint 中的“no-floating-promises”相同的 ESLint 规则?
【发布时间】:2021-05-07 15:45:48
【问题描述】:

我正在将几个项目从 TSLint 迁移到 ESLint,我发现 ESLint 没有阻止正确处理 Promise 的规则,或者至少我在文档中没有找到它。

TSLint 中的 no-floating-promises 规则强制程序员使用async/awaitthen/catch 所有承诺。有没有办法在 ESLint 中获得相同的功能?

我知道this answer,但我认为 OP 指的是具有不同行为的规则:catch() all promises。此外,这个问题的答案是 npm 包eslint-plugin-package,它已经两年没有发布了。

【问题讨论】:

    标签: typescript eslint


    【解决方案1】:

    typescript-eslint plugin 有一个 rule 强制正确处理所有承诺。

    安装了 ESLint:

    安装 typescript-eslint 插件:

    npm i --save-dev eslint typescript @typescript-eslint/parser @typescript-eslint/eslint-plugin
    

    将它添加到你当前的 ESLint 配置中:

    module.exports = {
      root: true,
      parser: '@typescript-eslint/parser',
      plugins: [
        '@typescript-eslint',
      ],
      extends: [
        'eslint:recommended',
        'plugin:@typescript-eslint/recommended',
      ],
    };
    

    如果你在安装 ESLint 时选择了“强制流行的编码风格”并选择了“标准”,那么你可能已经安装了这个插件。不过,如果您有疑问,请检查您的 ESLint 配置文件。

    并将规则添加到您的规则对象中:

    // .eslintrc.js
    /* the rest of the configuration */
    rules: {
        '@typescript-eslint/no-floating-promises': 'error',
        /8 the rest of the rules */
    }
    

    【讨论】:

      猜你喜欢
      • 2021-07-08
      • 2020-01-26
      • 2020-02-26
      • 2016-10-04
      • 2016-05-15
      • 2018-03-30
      • 2020-04-10
      • 2019-05-29
      • 2020-10-01
      相关资源
      最近更新 更多