简体   繁体   English

在Typescript中使用导入时,如何处理Tslint错误超过100个字符

[英]How to deal with Tslint error exceeds 100 characters when using imports in Typescript

I am importing a file: 我正在导入文件:

import { BodyTableHeaderExampleModule } from '../../components/example-table/example-table-header/example-table-header-xxxx/example-table-header-xxxxx.module';

However, tslint is complaining that the line exceeds 100 characters. 但是,tslint抱怨该行超过100个字符。 When I try to minimize line length, by doing the following: 当我尝试最小化行长时,请执行以下操作:

import { BodyTableHeaderExampleModule } from '../../components/example-table/example-table-header/' +
'example-table-header-xxxx/example-table-header-xxxxx.module';

I receive the following tslint errors: 我收到以下tslint错误:

ERROR: 11:92  semicolon             Missing semicolon
ERROR: 11:93  no-unused-expression  unused expression, expected an assignment or function call

Any suggestions on a way to remedy the Exceeds maximum line length of 100 when using long Typescript file imports, would be more than appreciated. 使用长的Typescript文件导入时,有关补救Exceeds Exceeds maximum line length of 100的方法的任何建议,将不胜感激。 Thank you. 谢谢。

There are few options here 这里很少有选择

  1. You could rather rewrite the import into 您宁愿将导入内容重写为

     import { BodyTableHeaderExampleModule } from '../../components/example-table/example-table-header/example-table-header-xxxx/example-table-header-xxxxx.module'; 

    But yes, this would still be over a 100 characters 但是是的,这仍然会超过100个字符

  2. You could simply disable that tslint rule for that one line 您只需为那一行禁用该tslint规则

  3. We actually disabled that rule all together and instead started using prettier to reformat our code. 实际上,我们完全禁用了该规则,而是开始使用更漂亮的格式来重新格式化我们的代码。 Now some lines might be too long, but most of them are formatted correctly and the hassle you described is gone 现在有些行可能太长了,但是大多数行的格式正确,而您描述的麻烦已经消失了
  4. Importing such a deep module in node (you seem to be using Angular, but you're still using node style of importing modules) is actually bad practice. 实际上,在节点中导入如此深的模块(您似乎正在使用Angular,但仍在使用节点样式的模块)。 And you might want to re-export that BodyTableHeaderExampleModule somewhere within that path. 您可能想在该路径中的某个位置重新导出该BodyTableHeaderExampleModule

Except 1. which won't still work for you, the other 3 solutions are all valid. 除了1.仍然无法为您工作之外,其他3个解决方案均有效。 For a long term solution, I'd myself probably solve it as described in 4. - and re-export the module somewhere in ../../components/example-table/example-table-header or similar. 对于长期解决方案,我本人可能会按照4中的描述进行解决../../components/example-table/example-table-header将该模块重新导出到../../components/example-table/example-table-header或类似位置中。 But choose what's best for your project and your need. 但是,选择最适合您的项目和需求的东西。

There is an option to ignore patterns from the max-line-rule. 有一个选项可以忽略max-line-rule中的模式。

https://palantir.github.io/tslint/rules/max-line-length/ https://palantir.github.io/tslint/rules/max-line-length/

Example: 例:

"max-line-length": [
  true,
  {
    "limit": 120,
    "ignore-pattern": "^import "
  }
],

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

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