title: Cast object to interface in TypeScript
body: <p>I'm trying to make a cast in my code from the body of a request in express (using body-parser middleware) to an interface, but it's not enforcing type safety.</p>
  
  <p>This is my interface:</p>
  
  <pre><code>export interface IToDoDto {
    description: string;
    status: boolean;
  };
  </code></pre>
  
  <p>This is the code where I'm trying to do the cast:</p>
  
  <pre><code>@Post()
  addToDo(@Response() res, @Request() req) {
    const toDo: IToDoDto = &lt;IToDoDto&gt; req.body; // &lt;&lt;&lt; cast here
    this.toDoService.addToDo(toDo);
    return res.status(HttpStatus.CREATED).end();
  }
  </code></pre>
  
  <p>And finally, the service method that's being called:</p>
  
  <pre><code>public addToDo(toDo: IToDoDto): void {
    toDo.id = this.idCounter;
    this.todos.push(toDo);
    this.idCounter++;
  }
  </code></pre>
  
  <p><strong>I can pass whatever arguments, <em>even ones that don't come close to matching the interface definition</em>, and this code will work fine.</strong> I would expect, if the cast from response body to interface is not possible, that an exception would be thrown at runtime like Java or C#.</p>
  
  <p>I have read that in TypeScript casting doesn't exist, only Type Assertion, so it will only tell the compiler that an object is of type <code>x</code>, so... Am I wrong? What's the right way to enforce and ensure type safety? </p>