简体   繁体   English

如何配置@typescript-eslint 规则

[英]How to configure @typescript-eslint rules

I'm trying to convert to @typescript-eslint but the documentation seems sorely lacking.我正在尝试转换为@typescript-eslint但文档似乎非常缺乏。 For example, I'm getting errors like this:例如,我收到这样的错误:

Line 58:  Expected a semicolon             @typescript-eslint/member-delimiter-style

I want to enforce no semicolons or commas.我想强制没有分号或逗号。 I found the documentation for that rule.我找到了该规则的文档。 https://github.com/bradzacher/eslint-plugin-typescript/blob/master/docs/rules/member-delimiter-style.md https://github.com/bradzacher/eslint-plugin-typescript/blob/master/docs/rules/member-delimiter-style.md

But it doesn't seem to give any examples of how to configure it in a real eslint file!但是它似乎没有给出如何在真正的 eslint 文件中配置它的任何示例! Anyone know how?有谁知道怎么做?

Using a .eslintrc.js configuration file you have to add this to the "rules" section:使用 .eslintrc.js 配置文件,您必须将其添加到“规则”部分:

"rules": {
    "@typescript-eslint/member-delimiter-style": ["error", {
      multiline: {
        delimiter: 'none',    // 'none' or 'semi' or 'comma'
        requireLast: true,
      },
      singleline: {
        delimiter: 'semi',    // 'semi' or 'comma'
        requireLast: false,
      },
    }]
}

Worked for me for the "@typescript-eslint/explicit-function-return-type" parameter.为“@typescript-eslint/explicit-function-return-type”参数工作。 Options are from the rules project site on github选项来自github 上规则项目站点

Thanks to maxkoryukov for improving my original answer.感谢 maxkoryukov 改进了我的原始答案。

Here are some example rules from a .eslintrc.js file from a VueJS Typescript project.以下是来自 VueJS Typescript 项目的 .eslintrc.js 文件的一些示例规则。

The two different rules relating to semi colons are for javascript/.vue files, and typescript/.ts files.与分号相关的两个不同规则分别用于 javascript/.vue 文件和 typescript/.ts 文件。

rules: {
    'no-console': process.env.NODE_ENV === 'production' ? 'warn' : 'off',
    'no-debugger': process.env.NODE_ENV === 'production' ? 'warn' : 'off',
    quotes: [
      'error',
      'single'
    ],
    semi: [
      'error',
      'never'
    ],
    'vue/html-self-closing': [
      'error',
      {
        html: {
          void: 'any',
          normal: 'any',
          component: 'any'
        }
      }
    ],
    '@typescript-eslint/member-delimiter-style': [
      'error',
      {
        multiline: {
          delimiter: 'none',
          requireLast: true
        },
        singleline: {
          delimiter: 'semi',
          requireLast: false
        }
      }
    ]
  }

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

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