TSLint - 防止错误:密钥不按字母顺序排序

时间:2017-08-21 08:39:22

标签: reactjs typescript typescript2.0 lint tslint

我正在使用Ionic2 / Cordova / Typescript / Angular进行测试。 我正在使用 tslint 5.6.0

我正在使用以下模块: https://www.npmjs.com/package/tslint

只关注一个文件...

当linting以下文件时:

import { NgModule, ErrorHandler } from "@angular/core";
import { BrowserModule } from "@angular/platform-browser";
import { IonicApp, IonicModule, IonicErrorHandler } from "ionic-angular";
import { MyApp } from "./app.component";

import { AboutPage } from "../pages/about/about";
import { ContactPage } from "../pages/contact/contact";
import { HomePage } from "../pages/home/home";
import { TabsPage } from "../pages/tabs/tabs";

import { StatusBar } from "@ionic-native/status-bar";
import { SplashScreen } from "@ionic-native/splash-screen";

@NgModule( {
    declarations: [
        MyApp,
        AboutPage,
        ContactPage,
        HomePage,
        TabsPage,
    ],
    imports: [
        BrowserModule,
        IonicModule.forRoot( MyApp ),
    ],
    bootstrap: [ IonicApp ],
    entryComponents: [
        MyApp,
        AboutPage,
        ContactPage,
        HomePage,
        TabsPage,
    ],
    providers: [
        StatusBar,
        SplashScreen,
        { provide: ErrorHandler, useClass: IonicErrorHandler },
    ],
})
export class AppModule { }

我明白了:

The key 'bootstrap' is not sorted alphabetically
RuleFailurePosition { position: 790, lineAndCharacter: { line: 25, character: 4 } }
RuleFailurePosition { position: 799, lineAndCharacter: { line: 25, character: 13 } }

我正在使用以下选项:

{
    "extends": "tslint:recommended",
    "rules": {
        "no-duplicate-variable": true,
        "max-line-length": {
            "options": [120]
        },
        "ordered-imports": false,
        "new-parens": true,
        "no-arg": true,
        "no-bitwise": true,
        "no-conditional-assignment": true,
        "no-consecutive-blank-lines": false,
        "no-console": {
            "options": [
                "debug",
                "info",
                "log",
                "time",
                "timeEnd",
                "trace"
            ]
        }
    },
    "jsRules": {
        "max-line-length": {
            "options": [120]
        }
    }
}

我需要在TSLint上配置什么选项以防止出现此错误?

2 个答案:

答案 0 :(得分:19)

此处失败的规则似乎是object-literal-sort-keys

您应该可以在配置文件的规则部分中添加以下内容来禁用它:

"object-literal-sort-keys": false

您可以找到所有 tslint 规则here

答案 1 :(得分:14)

对于来这里从JavaScript到TypeScript进行迁移的任何人,或者只是具有javascript + typescriptm的混合代码库的人,您也可以在'jsRules'中定义此规则,即在出现以下情况时摆脱此错误:您在javascript(不是打字稿文件)中定义了控制台语句。

//tslint.json

{
  "extends": ["tslint:recommended", "tslint-react", "tslint-config-prettier"],  
  "rules": {
    "object-literal-sort-keys": false //Disable for typescript
  },
  "jsRules": {
    "object-literal-sort-keys": false //Disable for javascript
  }
}