Why does the following error occur?
Error:(27, 16) TS2322: Type 'Observable<void>' is not assignable to type 'Observable<User>' Type 'void' is not assignable to type 'User'. Actually, this line causes an error:
return this.http.post(this._user_session_url, body, options) .map(this.extractData) .catch(this.handleError) Here is the code:
user.ts
export class User { constructor( public id:number, public name:string, public username:string, public password:string ) { } } login.service.ts
import {Injectable} from 'angular2/core'; import {Headers, RequestOptions} from 'angular2/http'; import {Observable} from "rxjs/Observable"; import {User} from "./../user"; import {Http, Response} from 'angular2/http'; @Injectable() export class LoginService { constructor (private http: Http) {} private _user_session_url = 'auth/sign_in'; login (username: string, password: string) : Observable<User> { let body = JSON.stringify({ username: username, password: password }); let headers = new Headers({ 'Content-Type': 'application/json', 'accept': 'json' }); let options = new RequestOptions({ headers: headers}); return this.http.post(this._user_session_url, body, options).map(this.extractData).catch(this.handleError) } private extractData(res: Response) { if (res.status < 200 || res.status >= 300) { throw new Error('Bad response status: ' + res.status); } let data = res.json(); localStorage.setItem('access-token', this.token); localStorage.setItem('client', this.client); localStorage.setItem('expiry', this.expiry); localStorage.setItem('uid', data.uid); } private handleError (error: any) { let errMsg = error.json().error || 'Server error'; console.error(errMsg); // log to console instead return Observable.throw(errMsg); } } TypeScript Compiler 1.8.7