25

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

if(myObj implements IMyInterface) {
    //... do something
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
vaclav_o
  • 1,705
  • 3
  • 15
  • 24
  • Yes, but you can use class instead interface. See [this](https://stackoverflow.com/a/53814012/4604351) example. – Alexey Baranoshnikov Dec 17 '18 at 11:21
  • See also this question: https://stackoverflow.com/questions/33800497/check-if-an-object-implements-an-interface-at-runtime-with-typescript – Rich Apr 10 '23 at 18:05

3 Answers3

17

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.

Boris Yankov
  • 1,530
  • 14
  • 21
10

Yes, if you use the reflec-ts compiler instead of the standard tsc compiler. This enhanced version of the TypeScript compiler that allows you to know which interface implements each class of your application. This version of the compiler stores all types information until runtime, and links these information to actual constructors. For example, you can write something like the following:

function implementsInterface(object: Object, target: Interface) {
    const objClass: Class = object.constructor && object.constructor.getClass();
    if (objClass && objClass.implements) { 
        let found = false;
        for (let base of objClass.implements) {
            let found = interfaceExtends(base, target);
            if (found) {
                return true;
            }
        }
    }
    return false;
}

// recursive interface inheritance check
function interfaceExtends(i: Interface, target: Interface) {
    if (i === target) { 
        return true;
    }
    if (i.extends) {
        let found = false;
        for (let base of i.extends) {
            // do a recursive check on base interface...
            found = interfaceExtends(base, target);
            if (found) {
                return true;
            }
        }
    }
    return false;
}

You can find a full working example that suits your needs here

Rich
  • 15,048
  • 2
  • 66
  • 119
pcan
  • 893
  • 11
  • 24
  • Yes. If the object you have to test isn't an instance of a class, you can do this check field by field. The interface meta-object you pass to the method will tell you all the details (fields that are optional, etc) and you can check those against your object. This kind of check is slower but it will work. See [this](https://github.com/pcan/reflec-ts-examples/tree/master/simple-validator) example as starting point. – pcan Aug 31 '16 at 10:46
  • Yes, it works with empty interfaces. Reflec-ts compiler creates a synthetic const for each `interface` declaration you write in your code. – pcan Sep 16 '16 at 14:01
0

There are a few options for this:

Rich
  • 15,048
  • 2
  • 66
  • 119