Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Options to force trailing comma on interfaces declaration #960

Closed
Strate opened this issue Feb 6, 2016 · 13 comments
Closed

Options to force trailing comma on interfaces declaration #960

Strate opened this issue Feb 6, 2016 · 13 comments

Comments

@Strate
Copy link

Strate commented Feb 6, 2016

I want to force trailing comma on interface declaration:

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

Is there any option to do this?

@jkillian
Copy link
Contributor

jkillian commented Feb 9, 2016

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.

@adidahiya
Copy link
Contributor

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

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

@adidahiya adidahiya added P3 and removed P2 labels Feb 10, 2016
@jkillian
Copy link
Contributor

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

@jkillian
Copy link
Contributor

jkillian commented Jun 8, 2016

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.

@jkillian jkillian closed this as completed Jun 8, 2016
@schickling
Copy link

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.

@schickling
Copy link

Maybe @jkillian knows something about this?

@adidahiya
Copy link
Contributor

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

@schickling
Copy link

This is the way how we write interfaces:

interface Post {
  title: string
  age: number
}

@Zjaaspoer
Copy link

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?

@adidahiya
Copy link
Contributor

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

@Zjaaspoer
Copy link

great thanks!

@theodorejb
Copy link

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

@muuvmuuv
Copy link

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

7 participants