tslint - Missing trailing comma (trailing-comma) on the last line
Asked Answered
A

3

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"
  }
}]
Advocate answered 7/9, 2018 at 8:16 Comment(0)
B
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).

Bennett answered 7/9, 2018 at 8:20 Comment(5)
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?Advocate
@MaxTravis - The rule is for the last property, not each line. You have no choice about the commas between properties.Bennett
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 tslintAdvocate
@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.Bennett
Ouch, I understood you now. Sorry for that, I just still a little bid confused after migration from .js to .ts ! Thanks!Advocate
B
1

I solved this in my tslint.json as following:

"rules": { "trailing-comma": false }
Broncobuster answered 14/8, 2019 at 15:43 Comment(0)
L
1

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

Lamar answered 18/12, 2020 at 15:54 Comment(1)
I also like it cause when you are copying and pasting and changing lines order the comma there helps a lot!Photoplay

© 2022 - 2024 — McMap. All rights reserved.