instanceof check on interface instanceof check on interface typescript typescript

instanceof check on interface


There is no way to runtime check an interface as type information is not translated in any way to the compiled JavaScript code.

You can check for a specific property or a method and decide what to do.

module MyModule {  export interface IMyInterface {      name: string;      age: number;  }  export interface IMyInterfaceA extends IMyInterface {      isWindowsUser: boolean;  }  export interface IMyInterfaceB extends IMyInterface {  }  export function doSomething(myValue: IMyInterface){    // check for property    if (myValue.hasOwnProperty('isWindowsUser')) {      // do something cool    }  }}


TypeScript uses duck typing for interfaces, so you should just check if object contains some specific members:

if ((<IMyInterfaceA>my).someCoolMethodFromA) {    (<IMyInterfaceA>my).someCoolMethodFromA();}