简体   繁体   English

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

[英]TSLint - Preventing error: The key is not sorted alphabetically

I'm doing a test app with Ionic2 / Cordova / Typescript / Angular. 我正在用Ionic2 / Cordova / Typescript / Angular做一个测试应用程序。 I'm using tslint 5.6.0 . 我正在使用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上配置什么选项以防止出现此错误?

The rule failing here seems to be object-literal-sort-keys . 这里失败的规则似乎是object-literal-sort-keys

You should be able to disable it in the rules section of your config file by adding: 您应该可以通过添加以下命令在配置文件的规则部分中禁用它:

"object-literal-sort-keys": false

You can find all the tslint rules here . 您可以在此处找到所有tslint规则。

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, ie to get rid of this error, when you having console statements defined inside javascript (not typescript files). 对于那些从javascript迁移到TypeScript的人,或者只是简单地拥有javascript + typescriptm的混合代码库的人,你也可以在'jsRules'中定义这个规则,即当你有控制台时摆脱这个错误在javascript(而不是typescript文件)中定义的语句。

//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
  }
}

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

相关问题 表格可以按字母顺序排序,但不能按字母顺序倒序 - Table can be sorted alphabetically but not reverse alphabetically 防止 JSON 对象在 ReactJS 中按字母顺序排序 - Preventing JSON objects from ordering alphabetically in ReactJS 尝试使用 React + TypeScript 项目修复 tslint 错误 - Trying to fix tslint error with React + TypeScript project ReactJs中的Antd Carousel上的TSLint错误“属性'carousel'不存在” - TSLint Error "Property 'carousel' does not exist" on Antd Carousel in ReactJs tslint错误:找不到模块'redux / todo / actions'.ts(2307) - tslint error: Cannot find module 'redux/todo/actions'.ts(2307) TSLINT 错误:属性“Component”在类型“typeof React”上不存在 - TSLINT Error: Property 'Component' does not exist on type 'typeof React' Tslint:没有子元素的 JSX 元素必须自关闭 [错误] - Tslint: JSX elements with no children must be self closing [Error] geocodeAync 错误阻止 function 拍摄 - Error with geocodeAync preventing function to shoot NextJS Typescript - 设置 tslint 配置以在有未使用的导入/声明时强制出错 - NextJS Typescript - Setup tslint configuration to force error when have unused import/declarations 防止 reactjs 中 axios 错误的默认事件 - preventing default event on axios error in reactjs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM