Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class type check in TypeScript

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.

like image 478
Mark Knol Avatar asked Oct 08 '12 20:10

Mark Knol


People also ask

How TypeScript check types?

Using TypeScript type guards Checking a specific value's type at runtime is the primary function of type guards. This helps the TypeScript compiler, which then uses the information to become more predictive about the types. The above code snippet showcases the example of the type guard instanceof .

Are classes types in TypeScript?

TypeScript offers full support for the class keyword introduced in ES2015. As with other JavaScript language features, TypeScript adds type annotations and other syntax to allow you to express relationships between classes and other types.

How do you check if an object is an instance of a class TypeScript?

Use the instanceof operator to check if an object is an instance of a class, e.g. if (myObj instanceof MyClass) {} . The instanceof operator checks if the prototype property of the constructor appears in the prototype chain of the object and returns true if it does.

What is typeof in TypeScript?

TypeScript adds a typeof operator you can use in a type context to refer to the type of a variable or property: let s = "hello"; let n : typeof s ; let n: string. This isn't very useful for basic types, but combined with other type operators, you can use typeof to conveniently express many patterns.


2 Answers

4.19.4 The instanceof operator

The instanceof operator 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).

TypeScript's 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.

See also:

  • MDN: instanceof
like image 128
Zeta Avatar answered Sep 18 '22 06:09

Zeta


TypeScript have a way of validating the type of a variable in runtime. You can add a validating function that returns a type predicate. So you can call this function inside an if statement, and be sure that all the code inside that block is safe to use as the type you think it is.

Example from the TypeScript docs:

function isFish(pet: Fish | Bird): pet is Fish {    return (<Fish>pet).swim !== undefined; }  // Both calls to 'swim' and 'fly' are now okay. if (isFish(pet)) {   pet.swim(); } else {   pet.fly(); } 

See more at: https://www.typescriptlang.org/docs/handbook/advanced-types.html

like image 35
Gilad S Avatar answered Sep 21 '22 06:09

Gilad S