Discussion from https://github.com/prettier/prettier/issues/1820
Consistency makes sense to me here, but do we feel a trailing comma in a type param/arg list is a smell for some typo?
I think the whole idea of allowing trailing commas is introducing more confusion then it helps. Why:
-fugly-comma
To me, the only times I've had a trailing comma is because of a typo, I think they should be allowed but under some flag like "fuly-comma": true and disabled by default.
Another place where typescript doesn't support trailing commas: https://github.com/prettier/prettier/issues/1858
Note that trailing commas for function calls and arguments is now in stage 3 at tc39 and node 8 that was just released this week supports them.
https://github.com/tc39/proposal-trailing-function-commas/blob/master/README.md
Note that trailing commas for function calls and arguments is now in stage 3 at tc39 and node 8 that was just released this week supports them.
trailing commas in function calls and parameter lists are already supported in TS since [email protected].
the question here is whether to extend that to type declarations or not.
I vote for allowing it because the trailing commas almost allowed everywhere, so if the type declarations do not, it will smell very strange for me.
Like the follow TypeScript will throw [ts] Trailing comma not allowed
error, which I would like to keep the trailing comma for it very much.
type Command = 'align' | 'embedding'
interface Args {
command: [
Command,
string,
]
}
Now that there are generic parameter defaults, there are times when using them in combination with a typical 120 line cap in linting (the TSLint default at least) makes it necessary to break across lines.
type TemplateBinding =
| AppleTemplateBinding
| BananaTemplateBinding
| CarrotTemplateNode
;
type ModelNode =
| AppleModelNode
| BananaModelNode
| CarrotModelNode
;
export const createSourceNodeFromTemplateBinding = <
TTemplateBinding extends TemplateBinding = TemplateBinding,
TSourceNode extends SourceNode = SourceNode
>(templateBinding: TTemplateBinding) => {
/* ... */
};
Strong +1 to allowing them.
I just ran into this "[ts] Trailing comma not allowed." error on a type. It was surprising, as I though Typescript supported trailing commas. I've now learned that this is only supported in parameter lists and object literals. I still find this surprising as this seems to be inconsistent. Either support trailing commas or don't. Doing it in some places and not in others is inconsistent and leads to surprises while writing Typescript.
@samal84 can you file a new issue with sample code? If there's another unexpected place, I'd be interested in trying to fix that too.
My situation was basically the same as the one you demonstrated above with multiple generic parameters to a function. The example that mhegazy demonstrated above is also something that may come up in real world codebases.
I also have a code formatting rule that if there is more than one parameter then every parameter should be on its own line. This makes for much shorter lines and makes the codebase much more consistent in general. Unfortunately I cannot enforce this rule for generic parameters in Typescript it seems.
In regards to @JoshuaKGoldberg's comment,
It is odd that leading pipes are allowed:
type TemplateBinding =
| AppleTemplateBinding
| BananaTemplateBinding
| CarrotTemplateNode
;
Whilst trailing pipes are not:
type TemplateBinding =
AppleTemplateBinding |
BananaTemplateBinding |
CarrotTemplateNode | // Error. Type expected.
;
I would of imagined that the same Error would apply for the first example. It seems that between the =
and the |
, throwing a Type expected.
Error seems reasonable.
If trailing commas are allowed, does that mean that trailing pipes should be too?
Most helpful comment
I think the whole idea of allowing trailing commas is introducing more confusion then it helps. Why:
-fugly-comma
To me, the only times I've had a trailing comma is because of a typo, I think they should be allowed but under some flag like "fuly-comma": true and disabled by default.