I'm working with Angular 5 .
I am passing an object to a component, with the decorator @Input ():
@Input() x: any;
This object can be of class Xxx, Yyy or Zzz.
Can I know within the component if the object received is of one kind or another?
I'm working with Angular 5 .
I am passing an object to a component, with the decorator @Input ():
@Input() x: any;
This object can be of class Xxx, Yyy or Zzz.
Can I know within the component if the object received is of one kind or another?
If it really is a class, you can use instanceof
:
let a= [];
console.log('¿Es un array?', a instanceof Array);
class Ejemplo {
}
a= new Ejemplo();
console.log('¿Es un Ejemplo?', a instanceof Ejemplo);
console.log('¿Es un array?', a instanceof Array);
This will not work if the declared type is interface
or type
, because they only exist at compile time (they are concepts from Typescript, it is not Javascript).