Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trailing commas for type parameters in TypeScript #3662

Closed
JoshuaKGoldberg opened this issue Jan 6, 2018 · 8 comments · Fixed by #10109
Closed

Trailing commas for type parameters in TypeScript #3662

JoshuaKGoldberg opened this issue Jan 6, 2018 · 8 comments · Fixed by #10109
Labels
lang:typescript Issues affecting TypeScript-specific constructs (not general JS issues) locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting.

Comments

@JoshuaKGoldberg
Copy link

JoshuaKGoldberg commented Jan 6, 2018

Prettier 1.9.2

I would paste a playground link but no TypeScript support ;) I'm silly: Playground link

--parser typescript

Input:

type TemplateBinding =
    | AppleTemplateBinding
    | BananaTemplateBinding
    | CarrotTemplateNode
;

type ModelNode =
    | AppleModelNode
    | BananaModelNode
    | CarrotModelNode
;

export const createSourceNodeFromTemplateBinding = <
    TTemplateBinding extends TemplateBinding = TemplateBinding,
    TSourceNode extends SourceNode = SourceNode
>(templateBinding: TTemplateBinding) => {
    /* ... */
};

Expected behavior:

There should be a trailing comma after = SourceNode.

Actual behavior:

As of fa708d1, it will not.

TypeScript 2.7 will be the first version to allow it (microsoft/TypeScript#16152). Should prettier add them back if TS is > 2.7?

@azz
Copy link
Member

azz commented Jan 6, 2018

Prettier rolls in it's own copy of the TypeScript parser, and we don't detect the version of TypeScript at all. Does trailing commas in the case even add any value? It's not like you ever have a large list of type parameters where diffing is a problem?

@azz azz added the lang:typescript Issues affecting TypeScript-specific constructs (not general JS issues) label Jan 6, 2018
@JoshuaKGoldberg
Copy link
Author

JoshuaKGoldberg commented Jan 6, 2018

It's not a problem, just a convenience. I would prefer having them for the same reason I'd like to have them in arrays and object literals (though admittedly not with the same level of convenience).

Perhaps this can be revisited if & when the minimum TypeScript version is 2.7...

Edit: Ooh or: --parser typescript@2.7? Would require changing all those options.parser === "typescript" lines to some form of parserIsLanguage(options.parser, "typescript").

@azz
Copy link
Member

azz commented Jan 6, 2018

I don't think such a small thing warrants the maintenance overhead of such a flag.

@j-f1
Copy link
Member

j-f1 commented Jan 6, 2018

We could only add it when you set --trailing-comma all, which assumes you’re using recent versions of stuff.

@duailibe
Copy link
Member

duailibe commented Jan 6, 2018

Related: #3313 (comment)

@sharno
Copy link

sharno commented Sep 11, 2019

This issue causes a failure to compile .tsx (React) files after prettier formatting for this case:

const f = <T, >() => 1

The trailing comma is needed especially for .tsx files for the compiler to recognize that this is a type and not a jsx tag

@lydell
Copy link
Member

lydell commented Sep 11, 2019

@sharno Prettier already seems to handle the trailing comma in that case:

❯ prettier --version
1.18.2

❯ cat test.tsx
const f = <T, >() => 1⏎                                                                                  

❯ prettier test.tsx
const f = <T,>() => 1;

@sharno
Copy link

sharno commented Sep 11, 2019

@lydell I see, it seems to be a problem in https://github.com/prettier/prettier-vscode

@github-actions github-actions bot added the locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. label Apr 29, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 29, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
lang:typescript Issues affecting TypeScript-specific constructs (not general JS issues) locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants