Search code examples
angulartypescriptlinttslint

Why does tslint remove object-keys?


I started using tslint on my TypeScript project. When i reviewed the linted source, i noticed that sometimes keys were removed from objects.

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".

How does this even compile to JavaScript without errors? I'm hesitant to check this in into the trunk.


Solution

  • You just enabled the object-literal-shorthand rule, you can set it in the tslint.json file.

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

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

    This is not an error, it is the standard syntax of ECMAScript 2015.

    In ECMAScript 2015, a shorthand notation is available, so that the keyword "function" is no longer necessary.

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