title: How do I cast to an interface an object may implement?
body: <p>I have the following classes &amp; interfaces:</p>
  
  <pre><code>export interface IBody {
      body : ListBody;
  }
  
  export class Element  {
  // ...
  }
  
  export class Paragraph extends Element implements IBody {
  // ...
  }
  
  export class Character extends Element {
  // ...
  }
  </code></pre>
  
  <p>I have code where I will get an array of Element derived objects (there are more than just Paragraph &amp; Character). In the case of those that implement IBody, I need to take action on the elements in the body.</p>
  
  <p>What is the best way to see if it implements IBody? Is it "if (element.body !== undefined)"?</p>
  
  <p>And then how do I access it? "var bodyElement = &lt;IBody> element;" gives me an error.</p>
  
  <pre><code>C:/src/jenova/Dev/Merge/AutoTagWeb/client/layout/document/elements/factory.ts(34,27): error TS2012: Cannot convert 'Element' to 'IBody':
      Type 'Element' is missing property 'body' from type 'IBody'.
      Type 'IBody' is missing property 'type' from type 'Element'.
  </code></pre>
  
  <p>thanks - dave</p>