简体   繁体   English

为什么 tslint 会删除对象键?

[英]Why does tslint remove object-keys?

I started using tslint on my TypeScript project.我开始在我的 TypeScript 项目上使用 tslint。 When i reviewed the linted source, i noticed that sometimes keys were removed from objects.当我查看 linted 源时,我注意到有时键会从对象中删除。

I made a production build (locally) and no errors were shown.我进行了生产构建(本地),没有显示错误。 How is this possible?这怎么可能? I could not find something about this behaviour on the net.我在网上找不到有关此行为的信息。

Before:前:

this.streamsDataModelDistance = new SimpleStreamsDataModel({
            polylineValueModels: polylineValueModels,
            fields: this.settingsModel.fields,
            commuters: distanceCommuters
        });

After:后:

this.streamsDataModelDistance = new SimpleStreamsDataModel({
            polylineValueModels,
            fields: this.settingsModel.fields,
            commuters: distanceCommuters
        });

Notice the missing key "polylineValueModels".请注意缺少的键“polylineValueModels”。

How does this even compile to JavaScript without errors?这甚至如何编译为 JavaScript 而没有错误? I'm hesitant to check this in into the trunk.我很犹豫要不要把这个放进后备箱。

You just enabled the object-literal-shorthand rule, you can set it in the tslint.json file.您刚刚启用了object-literal-shorthand规则,您可以在tslint.json文件中设置它。

{
 "rules": {
    "object-literal-shorthand": false
  }
}

https://palantir.github.io/tslint/rules/object-literal-shorthand/ https://palantir.github.io/tslint/rules/object-literal-shorthand/

This is not an error, it is the standard syntax of ECMAScript 2015.这不是错误,它是 ECMAScript 2015 的标准语法。

In ECMAScript 2015, a shorthand notation is available, so that the keyword "function" is no longer necessary.在 ECMAScript 2015 中,可以使用简写符号,因此不再需要关键字“function”。

// Shorthand property names (ES2015)
var a = 'foo', b = 42, c = {};
var o = {a, b, c};

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer

This is fine and a handy shorthand.这很好,也是一个方便的速记。

TypeScript allows for easy assignment of object properties if the property has the same name as the variable.如果属性与变量具有相同的名称,TypeScript 允许轻松分配 object 属性。

const foo = "bar";
const object =  {
   foo
}

Will create an object containing of:将创建一个 object 包含:

{
    foo: "bar"
}

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

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