Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect whether object implement interface in TypeScript dynamically

Tags:

Is there any way, how can I detect whether some object implements some interface?

if(myObj implements IMyInterface) {     //... do something } 
like image 449
the.ufon Avatar asked Apr 15 '13 11:04

the.ufon


People also ask

Is interface an object TypeScript?

Using this information, TypeScript creates an implicit interface type for student . An interface is just like an object but it only contains the information about object properties and their types.

Which keyword is used to check if a class satisfies a particular interface in TypeScript?

Here's another option: the module ts-interface-builder provides a build-time tool that converts a TypeScript interface into a runtime descriptor, and ts-interface-checker can check if an object satisfies it.

How do you check TypeScript type?

Use the typeof operator to check the type of a variable in TypeScript, e.g. if (typeof myVar === 'string') {} . The typeof operator returns a string that indicates the type of the value and can be used as a type guard in TypeScript.


1 Answers

No.

Currently, types are used only during development and compile time. The type information is not translated in any way to the compiled JavaScript code.

With some code generation by the compiler this would be possible. For now the TypeScript team is trying to add as little code as possible to the final JavaScript, the exception being the 'extends' keyword which adds a new method to the compiled output.

like image 191
Boris Yankov Avatar answered Sep 17 '22 11:09

Boris Yankov