I'm trying to add some error handling to a service, following the Angular guide.
Relevant snippet:
private handleError (error: Response | any) {
// In a real world app, you might use a remote logging infrastructure
let errMsg: string;
if (error instanceof Response) {
const body = error.json() || '';
const err = body.error || JSON.stringify(body);
errMsg = `${error.status} - ${error.statusText || ''} ${err}`;
} else {
errMsg = error.message ? error.message : error.toString();
}
console.error(errMsg);
return Observable.throw(errMsg);
}
However, I'm getting a TypeScript error:
error TS2339: Property 'error' does not exist on type '"" | Promise<any>'.
Property 'error' does not exist on type '""'.
I can understand why it's happening-- error.json()
returns a Promise<any>
and then the subsequent line with body.error
wouldn't work because there is no error
property. However, it seems like it should expect a JSON object to be returned from .json()
. Why is that, and what am I missing that the Angular guide isn't?
The "Property does not exist on type '{}'" error occurs when we try to access or set a property that is not contained in the object's type. To solve the error, type the object properties explicitly or use a type with variable key names. Copied!
The "Property does not exist on type void" error occurs when we try to access a property on the return value of a function that doesn't return anything. To solve the error, make sure to return the correct value from all of the function's code paths.
The "Property does not exist on type String" error occurs when we try to access a property that does not exist on the string type. To solve the error, use an object instead of a string, or make sure you're accessing a valid built-in method on the string.
Same thing just happened to me. This happens when you fail to import the Response object.
import { Response } from '@angular/http';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With