两个,
(逗号)和 ;
(分号)在声明 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
}
}
]
接受三个值(或两个用于单行):注意 - 这不是单行的选项,因为单行上的成员之间没有分隔符是 TS 中的语法错误。
要求最后
确定接口(interface)/类型中的最后一个成员是否应该有分隔符:
引用链接:click here
关于javascript - 如何在 Typescript 界面中强制使用分号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65388160/