In ActionScript, it is possible to check the type at run-time using the is operator:
var mySprite:Sprite = new Sprite(); trace(mySprite is Sprite); // true trace(mySprite is DisplayObject);// true trace(mySprite is IEventDispatcher); // true
Is it possible to detect if a variable (extends or) is a certain class or interface with TypeScript?
I couldn’t find anything about it in the language specs. It should be there when working with classes/interfaces.
instanceofoperator requires the left operand to be of type Any, an object type, or a type parameter type, and the right operand to be of type Any or a subtype of the ‘Function’ interface type. The result is always of the Boolean primitive type.
So you could use
mySprite instanceof Sprite;
Note that this operator is also in ActionScript but it shouldn’t be used there anymore:
The is operator, which is new for ActionScript 3.0, allows you to test whether a variable or expression is a member of a given data type. In previous versions of ActionScript, the instanceof operator provided this functionality, but in ActionScript 3.0 the instanceof operator should not be used to test for data type membership. The is operator should be used instead of the instanceof operator for manual type checking, because the expression x instanceof y merely checks the prototype chain of x for the existence of y (and in ActionScript 3.0, the prototype chain does not provide a complete picture of the inheritance hierarchy).
instanceof shares the same problems. As it is a language which is still in its development I recommend you to state a proposal of such facility.
- MDN: instanceof
Answered By – Zeta
Answer Checked By – Gilberto Lyons (BugsFixing Admin)