Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Property 'error' does not exist on type '"" | Promise<any>'

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?

like image 773
tofu Avatar asked Apr 17 '17 14:04

tofu


People also ask

How do you fix property does not exist on type?

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!

Does not exist on type () => void?

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.

Does not exist on type '{ name string }'?

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.


1 Answers

Same thing just happened to me. This happens when you fail to import the Response object.

import { Response } from '@angular/http';
like image 93
Marcelo Mason Avatar answered Sep 23 '22 23:09

Marcelo Mason