title: How to map a JSON string into a TypeScript (JavaScript) object in AngularJS 2?
body: <p>Consider this simple snippet of an AngularJS 2 application:</p>
  
  <p><strong>TestObject</strong></p>
  
  <pre><code>export class TestObject {
       id: number;
       name: string;
  }
  </code></pre>
  
  <p><strong>TestService</strong></p>
  
  <pre><code>[...]
  
  export class TestService {
      constructor(private http: Http) {}
  
      test(): Observable&lt;TestObject&gt; {
          return this.http
             .get("http://www.example.com")
             .map(this.save)
             .catch(this.fail);
      }
  
      private save(response: Response) {
          let testObject: TestObject = &lt;TestObject&gt; response.json();
          return testObject || {};
      }
  
      private fail(error: any) {
          return Observable.throw("error!");
      }
  }
  </code></pre>
  
  <p><strong>AppComponent</strong></p>
  
  <pre><code>[...]
  
  export class AppComponent implements OnInit {
  
      testObject: TestObject;
  
      constructor(private testService: testService) {}
  
      ngOnInit() {
          this.testService.test().subscribe(
              data =&gt; { 
                  this.testObject = new TestObject();
                  console.log(this.testObject); // prints (empty) TestObject
                  this.testObject = data; 
                  console.log(this.testObject); // prints object, not TestObject?
              },
              error =&gt; { }
          );
      }
  }
  </code></pre>
  
  <p><strong>Here my questions:</strong></p>
  
  <p>1) Why does my application print out (using Chrome Inspector) <code>object</code> and not <code>TestObject</code> as type?</p>
  
  <p>2) The property <code>testObject</code> of class <code>AppComponent</code> should be of type <code>TestObject</code>. Why does my application not fail? </p>
  
  <p>3) How can I achieve that I really get <code>TestObject</code>? What would be the best way to do it? Of course I could just manually fill up my <code>TestObject</code>, but I hoped there is some way of automatically mapping the json to my object.</p>