简体   繁体   English

tslint:“object-literal-shorthand”的好处

[英]tslint: benefit of "object-literal-shorthand"

an example for this rule:此规则的示例:

    this.http.post(this.apiUrl + "screenshot", {
        url: url,
        ratio: ratio
    }).subscribe(res => {
        console.log(res);
    });

tslint will complain about this code. tslint 会抱怨这段代码。 the way tslint wants me to do this would be tslint 希望我这样做的方式是

    this.http.post(this.apiUrl + "screenshot", {
        url,
        ratio
    }).subscribe(res => {
        console.log(res);
    });

for me the first snippet is way more well-arranged.对我来说,第一个片段安排得更好。 I disabled the corresponding rule but now I wonder if there is any benefit of using the "object-literal-shorthand" in terms of performance or something else.我禁用了相应的规则,但现在我想知道在性能或其他方面使用“object-literal-shorthand”是否有任何好处。

I think this gets pretty messy, especially if the literal is bigger and mixed with variables that can't use the shorthand.我认为这会变得非常混乱,尤其是当文字更大并且与不能使用速记的变量混合时。 So whats the reason for this rule, I don't get it.那么这条规则的原因是什么,我不明白。

This is one of the fixable rules in tslint.这是 tslint 中可修复的规则之一。

https://palantir.github.io/tslint/usage/cli/ https://palantir.github.io/tslint/usage/cli/

When you run:当你运行时:

tslint --fix

It will rewrite the code to use literal syntax.它将重写代码以使用文字语法。

benefit of using the "object-literal-shorthand" in terms of performance在性能方面使用“object-literal-shorthand”的好处

The rule is part of a collection of shorter code fragment rules that help produce fewer lines of code.该规则是较短代码片段规则集合的一部分,有助于生成更少的代码行。

So whats the reason for this rule, I don't get it.那么这条规则的原因是什么,我不明白。

Fixable rules in tslint are intended to assist with code sharing. tslint 中的可修复规则旨在帮助代码共享。 You can add a Git hook or a build stage that will run tslint --fix automatically for you.你可以添加一个 Git 钩子或一个构建阶段,它会自动为你运行tslint --fix

People who use modern IDEs that display tslint warnings don't really benefit from the fixable feature.使用显示 tslint 警告的现代 IDE 的人并没有真正从可修复功能中受益。 The IDE bugs you to fix it when you write it. IDE 会在您编写时提示您修复它。

Just turn off if you don't want it on.如果您不想打开它,请关闭它。

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

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