typescript - Type 'void' is not assignable to object -


export class logininfo {     username: string;     password: string; }   public getlogininfo(id: number): promise<logininfo> {     return this.http.get(this.url + id + '/' + '/logininfo')         .topromise()         .then(response => response.json() logininfo)         .catch((error: response) => {             this.handleerror(error);         }); } 

got code retrieving data api controller. when compiling in ts, getting error:

type 'promise<void | logininfo>' not assignable type 'promise<logininfo>' type 'void' not assignable type 'logininfo' 

here package versions:

"typescript": "2.5.2", "@angular/compiler": "4.3.6" "@angular/compiler-cli": "4.3.6" 

you need return on error handling case or throw new error. method promises return logininfo if error occurs return nothing, typescript protect accidentally returning nothing, if want should return null explicitly:

public getlogininfo(id: number): promise<logininfo> {     return this.http.get(this.url + id + '/' + '/logininfo')         .topromise()         .then(response => response.json() logininfo)         .catch((error: response) => {             this.handleerror(error);             // return null;             throw new error();         }); } 

as side note, async/await version may more readable:

public async getlogininfo(id: number): promise<logininfo> {     try{         let response = await this.http.get(this.url + id + '/' + '/logininfo').topromise();         return response.json() logininfo;     } catch (error: response) {         this.handleerror(error);         return null;     } } 

Comments

Popular posts from this blog

neo4j - finding mutual friends in a cypher statement starting with three or more persons -

minify - Minimizing css files -

php - How to remove letter in front of the word laravel -