question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 10 months ago
  • Comments:10 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
thorn0commented, Nov 22, 2022

Expressions like Foo<'some-type'> are called instantiation expressions, so the place for the test is tests\format\typescript\instantiation-expression

1reaction
fiskercommented, Nov 18, 2022

Maybe tests/format/typescript/logical-expression (not-exist yet) is a better place.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Release v2.8 · Issue #13792 · prettier/prettier - GitHub
Run on other projects to check for regressions v2.8.0 ... Expression expected error on logical operator after checking if instanceof ...
Read more >
instanceof - JavaScript - MDN Web Docs
The instanceof operator tests to see if the prototype property of a constructor appears anywhere in the prototype chain of an object.
Read more >
Error in using instanceOf operator in Java - Stack Overflow
If there is a field ID , I assume that it is there to identify the employee. In that case, it must be...
Read more >
instanceof
The instanceof operator tests whether the prototype property of a constructor appears anywhere in the prototype chain of an object.
Read more >
Programming With Java Generics - Angelika Langer
Here is a typical beginner's mistake for illustration. ... One of these situations are type checks (i.e., cast or instanceof expressions).
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found