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() as LoginInfo)
.catch((error: Response) => {
this.handleError(error);
});
}
Got this code for retrieving data from API controller. When compiling it in ts, I am always getting this error:
Type 'Promise<void | LoginInfo>' is not assignable to type 'Promise<LoginInfo>'
Type 'void' is not assignable to type 'LoginInfo'
Here are my package versions:
"typescript": "2.5.2",
"@angular/compiler": "4.3.6"
"@angular/compiler-cli": "4.3.6"
catch
catches an error, it's not re-thrown and nothing is returned, so the promise will then resolve tovoid
- hence thePromise<void | LoginInfo>
in the error. You need to decide what you want to do in thecatch
handler. At the moment, the typing of your function does not agree with what you are doing and an error is effected. – cartant