Skip to content

Trailing commas for type parameters in TypeScript #3662

Closed

Description

@JoshuaKGoldberg

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?

Activity

  1. azz commented on Jan 6, 2018

    @azz
    Member

    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?

  2. added
    lang:typescript
    on Jan 6, 2018
  3. JoshuaKGoldberg commented on Jan 6, 2018

    @JoshuaKGoldberg
    Author

    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 [email protected]? Would require changing all those options.parser === "typescript" lines to some form of parserIsLanguage(options.parser, "typescript").

  4. azz commented on Jan 6, 2018

    @azz
    Member

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

  5. j-f1 commented on Jan 6, 2018

    @j-f1
    Member

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

  6. duailibe commented on Jan 6, 2018

    @duailibe
    Collaborator
  7. sharno commented on Sep 11, 2019

    @sharno

    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

  8. lydell commented on Sep 11, 2019

    @lydell
    Member

    @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;
    
  9. added
    locked-due-to-inactivity
    on Apr 29, 2021
  10. locked as resolved and limited conversation to collaborators on Apr 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    lang:typescriptlocked-due-to-inactivity

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions