title: TypeScript - Check if Class implements an Interface
body: <p>I'm using an interface in TypeScript to define a function that is only available on some of the classes that extend the base class. This is a simplified version of the code I have so far:</p>
  
  <pre><code>class Animal {
  }
  
  interface IWalkingAnimal {
      walk(): void;
  }
  
  class Dog extends Animal implements IWalkingAnimal {
  }
  
  class Cat extends Animal implements IWalkingAnimal {
  }
  
  class Snake extends Animal {
  }
  
  private moveAnimal(animal: Animal) {
      if (animal instanceof Cat || animal instanceof Dog) {
          animal.walk();
      }
  }
  </code></pre>
  
  <p>Now, the trouble is I'll be adding more 'walking' animals so the moveAnimal functional will grow too large to be manageable. What I would like to do is something like this:</p>
  
  <pre><code>private moveAnimal(animal: Animal) {
      if (animal implements IWalkingAnimal ) {
          animal.walk();
      }
  }
  </code></pre>
  
  <p>However the 'implements' check does not work, and I cannot find an equivalent to 'instanceof' when using interfaces. In Java it seems that the use of 'instanceof' would work here, but TypeScript will not allow this.</p>
  
  <p>Does such a thing exist in TypeScript, or is there a better approach here? I am using the TypeScript 1.8.9.</p>