简体   繁体   English

tslint-组中的导入源必须按字母顺序排列

[英]tslint - Import sources within a group must be alphabetized

I am using a create-react-app setup with custom-react-scripts-ts . 我正在使用带有custom-react-scripts-tscreate-react-app设置。

This is my component: 这是我的组件:

import * as React from "react";
import "./App.css"; // reset.css

import ErrorsContainer from "./components/Error/Container";
import Header from "./components/Header";
import { initStores } from "./stores";
import { Provider } from "mobx-react";
import typography from "./utils/typography";

// font styles
typography.injectStyles();

class App extends React.Component {
  public onErrorDismiss = () => {
    return null;
  };

  public render() {
    return (
      <Provider {...initStores()}>
        <div className="App">
          <Header foo="string" />
          <ErrorsContainer />
        </div>
      </Provider>
    );
  }
}

export default App;

This is the failed to compile error I am getting: 这是我收到的无法编译的错误:

(7,1): Import sources within a group must be alphabetized. (7,1):组中的导入源必须按字母顺序排列。

This is the tslint.json file I am using: 这是我正在使用的tslint.json文件:

{
  "extends": ["tslint:recommended", "tslint-react", "tslint-config-prettier"],
  "linterOptions": {
    "exclude": [
      "config/**/*.js",
      "node_modules/**/*.ts",
      "coverage/lcov-report/*.js"
    ]
  },
  "rules": {
    "interface-name": [true, "never-prefix"]
  }
}

As far as I can tell the order of the imports is OK. 据我所知,导入的顺序还可以。 Why is this failing to compile? 为什么无法编译?

See https://stackoverflow.com/a/41841149/7977208 for a more complete answer 请参阅https://stackoverflow.com/a/41841149/7977208了解更完整的答案

You may try to override the rule by adding this to your tslint rules object. 您可以尝试通过将其添加到tslint规则对象中来覆盖该规则。

"ordered-imports": [true, {
   "import-sources-order": "any",
   "named-imports-order": "case-insensitive"
}]

or alternatively, move the import on line 7 或者,将导入移动到第7行

import { Provider } from "mobx-react";

to the bottom of the import group, which should appease the rule 到导入组的最底端,应该使规则更安心

import ErrorsContainer from "./components/Error/Container";
import Header from "./components/Header";
import { initStores } from "./stores";
import typography from "./utils/typography";
import { Provider } from "mobx-react";

Alphabetically, in this context, '.' 就此而言,按字母顺序,“。” comes before 'm'. 在“ m”之前。

Depending on your editor, there's probably an extension available to automatically sort your import statements for you. 根据您的编辑器的不同,可能会有一个扩展名可以为您自动对导入语句进行排序。

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

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