Tslint: Options to force trailing comma on interfaces declaration

Created on 6 Feb 2016  路  13Comments  路  Source: palantir/tslint

I want to force trailing comma on interface declaration:

interface A {
  prop1: string,
  prop2: string,
  prop3: string,
}

Is there any option to do this?

P3 Declined Feature Request

Most helpful comment

We're writing our interfaces the exact same way as @schickling (basically interfaces without semicolons). Right now Tslint is adding trailing commas to the last row like so:

interface Post {
  title: string
  age: number,
}

which is a bit undesirable... :) I'm not sure what the best strategy is to resolve this, but probably the trailing comma's should only be added if the other rows in the interface already have a comma?

All 13 comments

The trailing-comma rule only checks array literals, object literals, named import lists, and destructuring constructs right now, so unfortunately there's no way to have object literal _types_ checked, whether in an interface declaration like this or as a declared type somewhere else.

Seems like a reasonable feature request though - I've tagged it as such.

aren't fields in an interface supposed to end with a trailing semicolon?

http://www.typescriptlang.org/Handbook#interfaces

I had thought so also, but the Interface grammar allows for semicolons or commas.

See #1233 for discussion on this issue. The "ignore-interfaces" option for the semicolon rule will let users make a custom rule for this request if desired.

Since version 4.0 this seems to be always enforced when using multiline: "always". Is there a way to allow to omit trailing commas for interfaces? I imagine the following:

  "rules": {
    "trailing-comma": [
      true,
      {
        "multiline": "always",
        "singleline": "never"
      },
      "ignore-interfaces"
    ]
  }

Unfortunately this change seemed to have broke all of our build once we upgraded to v4.

Maybe @JKillian knows something about this?

@schickling the "ignore-interfaces" is supported in the semicolon rule, not the trailing-comma rule (see the linked PR #1233). What exact format are you looking to write interfaces with? I'm a little hesitant to add more options to this rule, especially since TSLint v4 will automatically fix all violations of the trailing-comma rule for you.

This is the way how we write interfaces:

interface Post {
  title: string
  age: number
}

We're writing our interfaces the exact same way as @schickling (basically interfaces without semicolons). Right now Tslint is adding trailing commas to the last row like so:

interface Post {
  title: string
  age: number,
}

which is a bit undesirable... :) I'm not sure what the best strategy is to resolve this, but probably the trailing comma's should only be added if the other rows in the interface already have a comma?

Yeah that makes sense @Zjaaspoer; filed #1810 for you and @schickling

great thanks!

We use commas to separate interface properties, and I'd really like an option to enforce this.

This affects this case as well:

fs.writeFile(
    ContentsJSONFile,
    JSON.stringify(ContentsJSON, null, 2),
    (err) => {
      if (err) {
        throw new Error(err.message)
      }

      console.log(chalk.green(`Wrote \`Contents.json\` file for ${set.name}!`))
    } // <!-- Missing trailing comma (trailing-comma) tslint(1)
  )
Was this page helpful?
0 / 5 - 0 ratings