javascript - 如何在 Typescript 界面中强制使用分号

标签 javascript typescript

两个, (逗号)和 ; (分号)在声明 typescript 接口(interface)时是有效的语法,例如以下都是有效的

export interface IUser {
  name: string;
  email: string;
  id: number;
}
export interface IUser {
  name: string,
  email: string,
  id: number
}
以下是我的担忧,因为它也是有效的。混合,;也有效
export interface IUser {
  name: string;
  email: string,
  id: number;
}
现在为了保持一致性,我想强制使用 ;在所有场合。即使对 typescript 强制执行分号规则,我的 linting 仍然通过.任何想法如何实现?
.eslintrc.json
{
  "root": true,
  "ignorePatterns": [
    "projects/**/*"
  ],
  "overrides": [
    {
      "files": [
        "*.ts"
      ],
      "parserOptions": {
        "project": [
          "tsconfig.json",
          "e2e/tsconfig.json"
        ],
        "createDefaultProgram": true
      },
      "extends": [
        "plugin:@angular-eslint/ng-cli-compat",
        "plugin:@angular-eslint/ng-cli-compat--formatting-add-on",
        "plugin:@angular-eslint/template/process-inline-templates"
      ],
      "rules": {
        "@angular-eslint/component-selector": [
          "error",
          {
            "type": "element",
            "prefix": "app",
            "style": "kebab-case"
          }
        ],
        "@angular-eslint/directive-selector": [
          "error",
          {
            "type": "attribute",
            "prefix": "app",
            "style": "camelCase"
          }
        ]
      }
    },
    {
      "files": [
        "*.html"
      ],
      "extends": [
        "plugin:@angular-eslint/template/recommended"
      ],
      "rules": {}
    }
  ]
}

tsconfig.json
/* To learn more about this file see: https://angular.io/config/tsconfig. */
{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "downlevelIteration": true,
    "experimentalDecorators": true,
    "moduleResolution": "node",
    "importHelpers": true,
    "target": "es2015",
    "module": "es2020",
    "lib": [
      "es2018",
      "dom"
    ]
  }
}

最佳答案

在 .eslintrc 文件中,如上所述添加以下规则,

 "@typescript-eslint/member-delimiter-style": [
            "warn",
            {
                "multiline": {
                    "delimiter": "semi",
                    "requireLast": true
                },
                "singleline": {
                    "delimiter": "semi",
                    "requireLast": false
                }
            }
        ]
接受三个值(或两个用于单行):
  • 逗号 - 每个成员都应该用逗号 (,) 分隔。
  • semi - 每个成员都应该用分号 (;) 分隔。
  • none - 每个成员都应该用空来分隔。

  • 注意 - 这不是单行的选项,因为单行上的成员之间没有分隔符是 TS 中的语法错误。
    要求最后
    确定接口(interface)/类型中的最后一个成员是否应该有分隔符:
  • true - 最后一个成员必须有分隔符。
  • false - 最后一个成员不能有分隔符。

  • 引用链接:click here

    关于javascript - 如何在 Typescript 界面中强制使用分号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65388160/

    相关文章:

    node.js - 返回类中的中间件

    mysql - 如何设置查询不接受不存在的字段

    javascript - TextHighlighter 库的解除绑定(bind)/销毁事件

    javascript - 为什么其中一个计算返回 NaN 而其他计算按预期工作?

    javascript - 如何使用 Javascript 将字符串方程式转换为数值

    typescript - 我可以将 TypeScript 类型与 io-ts 一起使用吗?

    javascript - Angular 路由器。如何使用不同的参数导航到当前 URL?

    javascript - 在应用程序外部调用路由更改(react-router)

    javascript - 如何测试 AngularJS $asyncValidators 包括 $http 调用

    javascript - 编写需要 Promise 但如果不满足条件则什么也不能返回的 typescript 方法的正确方法是什么?