Interface type check with Typescript

前端 未结 17 1391
灰色年华
灰色年华 2020-11-22 14:09

This question is the direct analogon to Class type check with TypeScript

I need to find out at runtime if a variable of type any implements an interface. Here\'s my

17条回答
  •  我在风中等你
    2020-11-22 14:45

    I would like to point out that TypeScript does not provide a direct mechanism for dynamically testing whether an object implements a particular interface.

    Instead, TypeScript code can use the JavaScript technique of checking whether an appropriate set of members are present on the object. For example:

    var obj : any = new Foo();
    
    if (obj.someInterfaceMethod) {
        ...
    }
    

提交回复
热议问题