Skip to content

Trailing comma causes TypeScript 2.9 compilation error #4624

Closed

Description

@srmagura

Under certain difficult-to-predict circumstances, Prettier will add a trailing comma in TypeScript "binding patterns" (e.g. const [x, y] = [1, 2]) when --trailing-comma is es5 or all.

When this happens, TypeScript 2.9 will give the error "A rest parameter or binding pattern cannot have a trailing comma". This was a breaking change from 2.8 to 2.9.

Some related but slightly different issues: #3673 #3311

Prettier 1.13.4
Playground link

--parser typescript
--trailing-comma all

Input:

async function test() {
let weekSummary
let projectDayDetails
let allAssignedActivityIds
let billingRates
let request: any

[weekSummary,projectDayDetails,allAssignedActivityIds,billingRates] = await request
}

Output:

async function test() {
  let weekSummary;
  let projectDayDetails;
  let allAssignedActivityIds;
  let billingRates;
  let request: any;

  [
    weekSummary,
    projectDayDetails,
    allAssignedActivityIds,
    billingRates,
  ] = await request;
}

Expected behavior:
No trailing comma after billingRates.

Activity

  1. ikatyang commented on Jun 3, 2018

    @ikatyang
    Member

    I think the trailing comma there should be allowed, tracked in microsoft/TypeScript#24628.

  2. nickserv commented on Jun 3, 2018

    @nickserv
    Member

    As far as I'm aware this is an intentional change in TypeScript, I doubt they'd change it back.

  3. nickserv commented on Jun 3, 2018

    @nickserv
    Member

    This is a bug in Prettier, here is the breaking change note from TypeScript:

    Trailing commas on rest parameters are not valid JavaScript, and the syntax is now an error in TypeScript too.

    https://github.com/Microsoft/TypeScript/wiki/Breaking-Changes#trailing-commas-not-allowed-on-rest-parameters

  4. hawkrives commented on Jun 3, 2018

    @hawkrives
    Contributor

    @nickmccurdy IIRC, that's for rest patterns - e.g.,

    [
      a, 
      b, 
      ...c,
    ] = data

    is invalid, but

    [
      a, 
      b, 
      c,
    ] = data

    should still compile.

  5. nickserv commented on Jun 3, 2018

    @nickserv
    Member

    I see, that issue seems to be microsoft/TypeScript#24628.

    I think I'm having a different issue with trailing commands, and I'm not sure if it's because of Prettier or TypeScript. Using Prettier 1.13 and TypeScript 2.9.1, Prettier adds a trailing comma after ButtonBaseClassKey and TypeScript errors with Trailing comma not allowed. It works if I remove the trailing comma option (defaults to none).

    export interface ButtonBaseProps<C>
      extends StandardProps<
        React.AnchorHTMLAttributes<HTMLElement> & React.ButtonHTMLAttributes<HTMLElement>,
        ButtonBaseClassKey,
      > {
      // redacted
    }
  6. duailibe commented on Jun 4, 2018

    @duailibe
    Collaborator

    @nickmccurdy Can you open a separate issue for that, please?

  7. added
    scope:dependency
    on Jun 4, 2018
  8. michaeljota commented on Jun 5, 2018

    @michaeljota

    The issue in TS seems to being closed after microsoft/TypeScript#24672 is merge. So, it was an issue with TS after all. 👍

  9. ikatyang commented on Jun 6, 2018

    @ikatyang
    Member
  10. added
    type:question
    scope:external
    and removed
    scope:dependency
    on Jun 6, 2018
  11. vaskevich commented on Jun 8, 2018

    @vaskevich

    As a workaround for anyone who stumbles into this issue, you can just append // prettier-ignore to an offending line and Prettier will not add a trailing comma there to satisfy TS.

  12. mscottx88 commented on Jul 30, 2018

    @mscottx88

    This also happens to object destructure statements with default values.

    To follow up on @vaskevich response, here is a clear picture of what to do to make both prettier and tslint happy in this situation:

          ({
            itemWorksheetRequestId: afterRequestId,
            // Typescript compiler error https://github.com/Microsoft/TypeScript/pull/24672
            // tslint:disable-next-line:trailing-comma
            skipOccurred = false // prettier-ignore
          } = await findAndProcessNewRequest({
            afterRequestId,
            client,
          }));
    
  13. ikatyang commented on Jul 30, 2018

    @ikatyang
    Member

    FYI, TS 3.0 just got released.

  14. added
    locked-due-to-inactivity
    on Oct 28, 2018
  15. locked as resolved and limited conversation to collaborators on Oct 28, 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

    locked-due-to-inactivityscope:externaltype:question

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions