简体   繁体   English

如何覆盖此ESLint TypeScript规则?

[英]How do I override this ESLint TypeScript rule?

I want to override the rule @typescript/type-annotation-spacing which determines how many spaces to put before and after : in type annotations. 我想覆盖规则@typescript/type-annotation-spacing ,该规则确定在类型注释中:之前和之后放置多少空格。

I would like to override it to have a space before and after : , but the default it to just have one after. 我想重写它:和之前和之后都有一个空格,但是默认情况下,它后面只有一个空格。

The documentation describes several options for this rule, but it does not give any example of the full syntax for overriding this in the eslintrc.* file. 文档描述了此规则的几个选项,但在eslintrc.*文件中未提供用于覆盖此规则的完整语法的任何示例。

https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/type-annotation-spacing.md 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 = () => {}).

I tried adding this to my eslintrc file 我尝试将其添加到我的eslintrc文件中

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

but then I get this error: 但是然后我得到这个错误:

   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" }').

I tried several other variations, but they all result in a similar error. 我尝试了其他几种变体,但是它们都导致类似的错误。

What is the correct syntax for this? 正确的语法是什么?

I checked out the ESLint documentation, and it shows putting multiple options for a rule in an array. 我检查了ESLint文档,它显示了将规则的多个选项放入数组中的情况。 This works: 这有效:

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

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

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