简体   繁体   English

如何在 Typescript 接口中强制分号

[英]How to enforce semicolon in Typescript interface

Both , (comma) and ; , (逗号) 和; (semicolon) are valid syntax while declaring a typescript interface eg the below are both valid (分号)在声明 typescript 接口时是有效的语法,例如以下都是有效的

export interface IUser {
  name: string;
  email: string;
  id: number;
}
export interface IUser {
  name: string,
  email: string,
  id: number
}

Below is my concern as it is also valid.以下是我的担忧,因为它也是有效的。 Mixing of , and ;混合,; also works也有效

export interface IUser {
  name: string;
  email: string,
  id: number;
}

Now for consistency I would like to enforce use of ;现在为了保持一致性,我想强制使用; at all occasions.在所有场合。 My linting still passes even with semicolon rule enforced for typescript .Any ideas how this can be implemented?即使对typescript强制执行分号规则,我的 linting 仍然通过。任何想法如何实现?

.eslintrc.json .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 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"
    ]
  }
}

In the.eslintrc file, add below rule as mentioned,在 .eslintrc 文件中,如上所述添加以下规则,

 "@typescript-eslint/member-delimiter-style": [
            "warn",
            {
                "multiline": {
                    "delimiter": "semi",
                    "requireLast": true
                },
                "singleline": {
                    "delimiter": "semi",
                    "requireLast": false
                }
            }
        ]

Accepts three values (or two for singleline):接受三个值(或两个用于单行):

  • comma - each member should be delimited with a comma (,).逗号 - 每个成员都应该用逗号 (,) 分隔。

  • semi - each member should be delimited with a semicolon (;). semi - 每个成员都应该用分号 (;) 分隔。

  • none - each member should be delimited with nothing. none - 每个成员都应该用空来分隔。

NOTE - this is not an option for singleline because having no delimiter between members on a single line is a syntax error in TS.注意 - 这不是单行的选项,因为单行上的成员之间没有分隔符是 TS 中的语法错误。 requireLast Determines whether or not the last member in the interface/type should have a delimiter: requireLast 确定接口/类型中的最后一个成员是否应该有分隔符:

  • true - the last member must have a delimiter. true - 最后一个成员必须有分隔符。
  • false - the last member must not have a delimiter. false - 最后一个成员不能有分隔符。

Ref link: click here参考链接: 点击这里

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

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