Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type 'void' is not assignable to type 'ObservableInput<{}>'

This error started to pop up after I migrated to TS 2.2.2, so I'm assuming that's the problem... The code did not stop working, but now I receive that error and I tried a few things like returning an empty observable, catching the re-thrown exception and returning an object, nothing seemed to work. Why is this happening now? Shouldn't it understand I'm re-throwing the exception and not expect a return? Am I misreading the error?

This is the complete error description:

enter image description here

Here's the complete code:

return request     .map((res: Response) => res.json())     .catch((error: any) => {         // todo: log?          if (error.status == 500) {             this.alertService.showError(error.statusText);         } else if (error.status == 588) {             this.alertService.showAlert(error.statusText);         }          Observable.throw(error.statusText);     }); 

I tried returning the Observable, but my wrapper method expects a return of type T, which is the return of my deserialized request (map(...)). If I do return the throw this is the error I get:

[ts] Type 'Observable' is not assignable to type 'T'

I'm using:

  • Angular4
  • Typescript 2.2.2
like image 428
eestein Avatar asked Mar 30 '17 10:03

eestein


2 Answers

you have to return the Observable

 return request     .map((res: Response) => res.json())     .catch((error: any) => {         // todo: log?          if (error.status == 500) {             this.alertService.showError(error.statusText);         } else if (error.status == 588) {             this.alertService.showAlert(error.statusText);         }          return Observable.throw(error.statusText);     }); 
like image 85
El houcine bougarfaoui Avatar answered Sep 21 '22 02:09

El houcine bougarfaoui


Sometimes when you call catch without using arrow function like below

getUserList() {     return this.http.get(this.constURL + '/api/url/here', this.headerOptions)     .catch(this.handleError); }  handleError(error: Response) {     if (error.status == 500) {             this.router.navigate(['/login']);     } else {       return Observable.throw(error);     } } 

then it gives error of

ERROR TypeError: Cannot read property 'navigate' of undefined not getting this

Because in handleError function this object is not accessible..if you console this.router then you will get undefined.. so this object not working and not getting router all available methods

So you have to use arrow function here like below

getUserList() {     return this.http.get(this.constURL + '/api/url/here', this.headerOptions)     .catch(error => {        return this.handleError(error);     }); }  handleError(error: Response) {     if (error.status == 500) {             this.router.navigate(['/login']);     } else {       return Observable.throw(error);     } } 

Also if you have not mentioned return for handlerError function then it will throw error again like

Argument of type '(error: any) => void' is not assignable to parameter of type

So its necessary to type return for handlerError function.

Check here for in detail.He has explained code very well with all possible errors and solution of that..worked for me

like image 41
Prashant M Bhavsar Avatar answered Sep 22 '22 02:09

Prashant M Bhavsar