0

I get the error:

TS2358: The left-hand side of an 'instanceof' expression must be of type 'any', an object type or a type parameter.

When compiling the following code:

const animal: Tiger | Monkey | Goat = fetchAnimal();
if (!animal instanceof Tiger && !animal instanceof Monkey) {
    alert('It is safe to touch the animal');
}
Kent Munthe Caspersen
  • 5,918
  • 1
  • 35
  • 34

1 Answers1

0

It turns out I needed to add parenthesis, then it worked:

if (!(animal instanceof Tiger) && !(animal instanceof Monkey)) {
    alert('It is safe to touch the animal');
}
Kent Munthe Caspersen
  • 5,918
  • 1
  • 35
  • 34
  • 1
    Yes. Without the `()`, it's `(!animal) instanceof Monkey` which ends up being `true instanceof Monkey` or `false instanceof Monkey`. :-) Unary operators are typically **very** high precedence. – T.J. Crowder Feb 27 '20 at 07:35