typescript TSLint - 防止错误:键未按字母顺序排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45792683/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TSLint - Preventing error: The key is not sorted alphabetically
提问by davidesp
I'm doing a test app with Ionic2 / Cordova / Typescript / Angular. I'm using tslint 5.6.0.
我正在用 Ionic2/Cordova/Typescript/Angular 做一个测试应用程序。我正在使用tslint 5.6.0。
I'm using the following module: https://www.npmjs.com/package/tslint
我正在使用以下模块:https: //www.npmjs.com/package/tslint
Focusing on just one file...
只关注一个文件...
when linting the following file:
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 { }
I get:
我得到:
The key 'bootstrap' is not sorted alphabetically
RuleFailurePosition { position: 790, lineAndCharacter: { line: 25, character: 4 } }
RuleFailurePosition { position: 799, lineAndCharacter: { line: 25, character: 13 } }
I'm using the following options:
我正在使用以下选项:
{
"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]
}
}
}
What option do I need to configure on TSLint to prevent showing up this error?
我需要在 TSLint 上配置什么选项来防止出现此错误?
回答by Lee Brindley
For anyone coming here who is doing a migration to TypeScript from javascript, or who simply has a mixed codebase of javascript + typescriptm you may to define this rule inside 'jsRules' as well, i.e. to get rid of this error, when you having console statements defined inside javascript (not typescript files).
对于任何来这里从 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
}
}
回答by toskv
The rule failing here seems to be object-literal-sort-keys.
这里失败的规则似乎是object-literal-sort-keys。
You should be able to disable it in the rulessection of your config file by adding:
您应该能够通过添加以下内容在配置文件的规则部分禁用它:
"object-literal-sort-keys": false
You can find all the tslintrules here.
您可以在此处找到所有tslint规则。