21

I cannot figure out why my tslint even want to see the trailing comma on the end of the last line in the objects? How can I set the ignore rule for the last line of the objects for example? Thanks.

Exemple:

  props = {
    prop1: 21, // good
    prop2: 2, // good
    prop3: false // error: [tslint] Missing trailing comma (trailing-comma)

  }

Rule for trailing-comma in my tsconfig.json:

"trailing-comma": [true, {
  "singleline": "never",
  "multiline": {
    "objects": "always",
    "arrays": "always",
    "functions": "never",
    "typeLiterals": "ignore"
  }
}]
Max Travis
  • 1,228
  • 4
  • 18
  • 41

3 Answers3

34

You clearly have the rule enabled for multi-line objects:

"trailing-comma": [true, {
  "singleline": "never",
  "multiline": {
    "objects": "always",     // <==================
    "arrays": "always",
    "functions": "never",
    "typeLiterals": "ignore"
  }
}]

So...disable it by making that "never" (if you want to disallow commas there) or "ignore" (if you want to allow commas to be there or not, either way).

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Oh, but in this case we shall to fully disable `trailing commas` for `object`, instead to make it disabled only for the last line in it. Is not? – Max Travis Sep 07 '18 at 08:23
  • 1
    @MaxTravis - The rule is for the last property, not each line. You have no choice about the commas *between* properties. – T.J. Crowder Sep 07 '18 at 08:24
  • Thank you, but to be clearly, if I set the `"objects": "always"` to `never` or `ignore` it will disable this tslint rule for whole object, that's incorrect... It will be: `props = { prop1: 21, prop2: 2 prop3: false }` - on the `prop2` must be an error in tslint – Max Travis Sep 07 '18 at 08:27
  • 3
    @MaxTravis - `prop2: 2 prop3: false` is **already** a syntax error. This rule has nothing to do with it. This rule is purely about a comma after the **last** property before the closing `}` on the object initializer. – T.J. Crowder Sep 07 '18 at 08:30
  • 1
    Ouch, I understood you now. Sorry for that, I just still a little bid confused after migration from .js to .ts ! Thanks! – Max Travis Sep 07 '18 at 08:48
1

I solved this in my tslint.json as following:

"rules": { "trailing-comma": false }
ejderuby
  • 710
  • 5
  • 21
M.Liscio
  • 577
  • 1
  • 4
  • 11
1

Include trailing comma even in the last line is a good practice of having less merge conflict, although it looks weird.

Yi Yang
  • 85
  • 2