title: parsing object not working as expected in typescript
body: <p>Are line1 and line 2 below equal to line3?</p>
  
  <pre><code>let resultObject:UserProfileAPI  =JSON.parse(subscribed['additional-info']) as UserProfileAPI; //line 1
  this.profile = new UserProfileAPI(new User(resultObject["external-profile"].firstname,resultObject["external-profile"].lastname,resultObject["external-profile"].email))//line2 
  //this.profile = JSON.parse(subscribed['additional-info']) as UserProfileAPI; //uncommenting line3 doesn't work if swapped with line1 and line2. I suppose line 1 and line2 do the same thing together as line 3
  </code></pre>
  
  <p>I have this piece of code. I am unable to understand why the un-commented code works but swapping it with the commented like doesn't</p>
  
  <pre><code>profile:UserProfileAPI;
  ...
  (result:Result)=&gt; {
          console.log("In nav - result from user signin state ",result);
          let subscribed:UserSigninState = result.additionalInfo;
          console.log("new user signin state received:", subscribed);
          this.userLoggedIn = subscribed.isSignedIn;
          //success in login and got profile
          if(subscribed.isSignedIn ){
            if(subscribed['additional-info'] !== '') {
              let resultObject:UserProfileAPI  =JSON.parse(subscribed['additional-info']) as UserProfileAPI; //line 1 works
              this.profile = new UserProfileAPI(new User(resultObject["external-profile"].firstname,resultObject["external-profile"].lastname,resultObject["external-profile"].email))//line2 works
              //this.profile = JSON.parse(subscribed['additional-info']) as UserProfileAPI; //uncommenting line3 doesn't work if swapped with line1 and line2. I suppose line 1 and line2 do the same thing together as line 3
            }else{
              console.log('login successful but didn\'t get profile');
              this.profile = new UserProfileAPI(new User('unavailable','unavailable','unavailable'));
            }
          }
          //fail in login, additional-inifo should have reason
          if(!subscribed.isSignedIn ){
            if(subscribed['additional-info'] !== '') {
              this.navEvent.emit(new NavContext(subscribed['additional-info']));
            } else{
              console.log('login wasn\'t successful but didn\'t get the reason for error');
              this.navEvent.emit(new NavContext('login failed. Reason unknown'));
            }
          }
        }
  </code></pre>
  
  <p>I am unit-testing the above piece of code in <code>Jasmine</code>. The spec tests that on successful login, the server should send user's profile which the client should store.</p>
  
  <pre><code>fit('should set user profile for successful signin attempt',()=&gt;{
      let navComponent:NavComponentComponent = component;
      let userManagementService:MockUserManagementService = TestBed.get(UserManagementService);
      let user = new User('manu','chadha','test@test.com');//create a user
      let dummyUserProfile= new UserProfileAPI(user);//create a dummy profile using user
      console.log('dummy profile is ',dummyUserProfile);//prints 'dummy profile is ', UserProfileAPI{external-profile: User{firstname: 'manu', lastname: 'chadha', email: 'test@test.com', password: ''}} 
      console.log('dummy profile is JSON stringify',JSON.stringify(dummyUserProfile));//prints 'dummy profile is JSON stringify', '{"external-profile":{"firstname":"manu","lastname":"chadha","email":"test@test.com","password":""}}'
      let userSignInState = new UserSigninState(true,JSON.stringify(dummyUserProfile));//create part of response as string
      console.log('user signin state ',userSignInState);//prints 'user signin state ', UserSigninState{isSignedIn: true, additional-info: '{"external-profile":{"firstname":"manu","lastname":"chadha","email":"test@test.com","password":""}}'}
     let result:Result = new Result('success',userSignInState);
  console.log('result is ',result);//prints 'result is ', Result{result: 'success', additionalInfo: UserSigninState{isSignedIn: true, additional-info: '{"external-profile":{"firstname":"manu","lastname":"chadha","email":"test@test.com","password":""}}'}}
  userManagementService.signInStateSubject.next(result);//send response
      console.log('user profile is ',navComponent.profile);//prints user profile is ', Object{external-profile: Object{firstname: 'manu', lastname: 'chadha', email: 'test@test.com', password: ''}}
      expect(navComponent.profile).toBeTruthy();
      expect(navComponent.profile).toEqual(dummyUserProfile);
    });
  </code></pre>
  
  <p>The above test doesn't work for code if I comment line1 and line2 and use only line3</p>
  
  <pre><code>if(subscribed['additional-info'] !== '') {
     //                let resultObject:UserProfileAPI  =JSON.parse(subscribed['additional-info']) as UserProfileAPI; //line1
        //              this.profile = new UserProfileAPI(new User(resultObject["external-profile"].firstname,resultObject["external-profile"].lastname,resultObject["external-profile"].email)) //line2
                      this.profile = JSON.parse(subscribed['additional-info']) as UserProfileAPI; //line3
  </code></pre>
  
  <p>For above, I get error <code>Expected object to be a kind of UserProfileAPI, but was Object({ external-profile: Object({ firstname: 'manu', lastname: 'chadha', email: 'test@test.com', password: '' }) }).</code></p>