【问题标题】:How do I override this ESLint TypeScript rule?如何覆盖这个 ESLint TypeScript 规则?
【发布时间】:2019-09-04 21:34:57
【问题描述】:

我想覆盖规则@typescript/type-annotation-spacing,该规则确定在类型注释中: 前后放置多少空格。

我想覆盖它以在: 之前和之后有一个空格,但默认它后面只有一个。

文档描述了此规则的几个选项,但没有给出在 eslintrc.* 文件中覆盖此规则的完整语法示例。

https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/type-annotation-spacing.md

This rule has an object option:

"before": false, (default for colon) disallows spaces before the colon/arrow.
"before": true, (default for arrow) requires a space before the colon/arrow.
"after": true, (default) requires a space after the colon/arrow.
"after": false, disallows spaces after the colon/arrow.
"overrides", overrides the default options for type annotations with colon (e.g. const foo: string) and function types with arrow (e.g. type Foo = () => {}).

我尝试将此添加到我的 eslintrc 文件中

"rules": {
        "@typescript-eslint/type-annotation-spacing": {
            before: true,
            after: true,
            severity: "warn"
        }
    }

然后我得到这个错误:

   Configuration for rule "@typescript-eslint/type-annotation-spacing" is invalid:
        Severity should be one of the following: 0 = off, 1 = warn, 2 = error (you passed '{ before: true, after: true, severity: "warn" }').

我尝试了其他几种变体,但它们都导致了类似的错误。

正确的语法是什么?

【问题讨论】:

  • 我认为,语法与原版 eslint 相同。查看他们的文档以了解语法。
  • 好吧,如果你能说出你做了什么,而不是投反对票,那就太好了。然后阅读文档顺便说一句
  • 对不起作用的解决方案投反对票没有错

标签: typescript eslint


【解决方案1】:

我查看了 ESLint 文档,它显示了将规则的多个选项放在一个数组中。这有效:

 "rules": {
        "@typescript-eslint/type-annotation-spacing": ["warn", {
            before: true,
            after: true
        }]
    }

【讨论】:

    猜你喜欢
    • 2021-06-10
    • 2019-09-07
    • 2019-11-10
    • 2021-04-14
    • 1970-01-01
    • 2021-08-22
    • 2021-05-17
    • 2012-09-28
    • 1970-01-01
    相关资源
    最近更新 更多