镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Trailing Commas in Function Param Lists #7279

Description

https://jeffmo.github.io/es-trailing-function-commas/

This proposal is Stage 3 according to https://github.com/tc39/ecma262, so seems like its at an appropriate stability for TypeScript team to implement it.

This would allow function definitions and calls in the following style:

function foo(
  bar: Bar, // Comment about bar
  baz: Baz, // Comment about baz
) {
  // Implementation...
}

foo(
  bar,
  baz,
);

Benefits: https://github.com/jeffmo/es-trailing-function-commas/blob/master/proposal_presentation_slides.pdf

  • Helps with VCS authorship attribution in case of added/removed parameters
  • Consistency with object and array literals
  • Lintable (if you don't like the style, can ban it in your codebase with a linter)
  • Supported by other languages

In addition to all of the above, I suspect the extra "verbosity" of TypeScript's inline types makes this feature even more attractive in TypeScript than in vanilla JS. I feel this particularly in class constructors, which can also have private annotations, and possibly a readonly annotation coming down the pipe.

class Foo {
  constructor(
    private bar: Bar = new BasicBar(),
    private baz: Baz = new BasicBaz(),
  ) {}
}

Activity

  1. RyanCavanaugh commented on Mar 8, 2016

    @RyanCavanaugh
    Member

    Approved for whatever the Stage 3 proposal says

  2. mhegazy commented on Mar 8, 2016

    @mhegazy
    Contributor

    PRs are welcomed of course. the change should be limited to the parser, so would be a rather easy change to make.

  3. DanielRosenwasser commented on Mar 8, 2016

    @DanielRosenwasser
    Member

    Actually, signature help and overload resolution could potentially be affected by this.

  4. nojvek commented on May 22, 2016

    @nojvek
    Contributor

    Is anyone taking on this? If not I can. I spent almost my entire weekend playing with the parser and scanner.

    Daniel Rosenwasser (@DanielRosenwasser), what do you mean by signature help and overload resolution?

  5. mhegazy commented on May 23, 2016

    @mhegazy
    Contributor

    Noj Vek (@nojvek) there is a PR for this already in #7747. if you want to take that over and respond to code review comments we can get it in.

  6. DanielRosenwasser commented on May 23, 2016

    @DanielRosenwasser
    Member

    Noj Vek (@nojvek) signature help is the little tooltip that shows up to show the parameters & their types when you call a function.

    image

    I mean that we specifically check if you have a trailing comma to see if there is a "better" overload in some cases.

    For instance, here's a call to localeCompare with one argument:

    image

    Here's a call with one argument and a trailing comma:

    image

  7. DanielRosenwasser commented on May 23, 2016

    @DanielRosenwasser
    Member

    So in general, my warning is that we can't do that anymore if we support trailing commas in argument lists. If we do that, we'd impair the experience of anyone who wants to use trailing commas in calls.

  8. mhegazy commented on May 23, 2016

    @mhegazy
    Contributor

    do not think this is an issue we should change the design of signature help for. you will get the pop up, you could decide to dismiss it an keep the comma.

  9. ghost closed this as completedin #8942on Jun 7, 2016
  10. ewinslow commented on Jun 8, 2016

    @ewinslow
    Author

    Woohoo!

  11. basarat commented on Jul 6, 2016

    @basarat
    Contributor

    Personal opinion to any users : If you are thinking about adding a trailing comma to a function you might want to reconsider and change the function parameters to a single object https://basarat.gitbooks.io/typescript/content/docs/tips/functionParameters.html 🌹

  12. locked and limited conversation to collaborators on Jun 19, 2018
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

    CommittedThe team has roadmapped this issueES NextNew featurers for ECMAScript (a.k.a. ESNext)FixedA PR has been merged for this issueHelp WantedYou can do thisSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions