Is there anything that compares the type of the object?

Asked

Viewed 78 times

2

I want to test the type of the object not its value, more or less this:

component1: IndexComponent

if(component1: IndexComponent) {
   dosomething();
}
  • Do you know that voting on everything on the site, besides accepting an answer in your questions? They are different things, you can do both.

1 answer

6


This is probably what you want:

if(component1 instanceof IndexComponent) {
   dosomething();
}

I put in the Github for future reference.

Then the variable needs to have an object that is of the specified type for the condition to be true. it is possible to check any type that the object has, even looking at its hierarchy.

Operator documentation instanceof.

In Javascript you can use typeof but I don’t think that’s what you want.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.