Skip to content

Expression expected error on logical operator after checking if instanceof GenericClass<T> #13856

@lucasrabiec

Description

@lucasrabiec

Prettier 2.7.1
Playground link

--parser typescript

Input:

export class Foo<T> {
}

function sample(error: unknown) {
  if (!(error instanceof Foo<'some-type'> || error instanceof Error) || !error.message) {
    return 'something';
  }
}

Output:

SyntaxError: Expression expected. (5:43)
  3 |
  4 | function sample(error: unknown) {
> 5 |   if (!(error instanceof Foo<'some-type'> || error instanceof Error) || !error.message) {
    |                                           ^
  6 |     return 'something';
  7 |   }
  8 | }

Expected behavior:

  3 |
  4 | function sample(error: unknown) {
  5 |   if (
  6 |     !(error instanceof Foo<'some-type'> || error instanceof Error) ||
  7 |     !error.message
  8 |   ) {
  6 |     return 'something'
  7 |   }
  8 | }

It works if I wrap Foo<'some-type'> with parentheses and then prettier formats it like expected behavior (also removes ( )) but I think it should work without wrapping. edit: problem occured again on reformat (obviously)

It works if I split that logical OR into separate statements (I know this is not logically the same as above, just for an example)

!(error instanceof Foo<'some-type'>) ||
!(error instanceof Error) ||
!error.message

Metadata

Metadata

Assignees

No one assigned

    Labels

    locked-due-to-inactivityPlease open a new issue and fill out the template instead of commenting.type:testsIssues about tests that are not correct, should be added, or similar

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions