Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular - specify observable return type?

I have this part in a function :

this.myservice.getComments()
  .subscribe(data => {
    this.post.comments = data.body;
    this.post.comments_count = Number(data.headers.get('x-wp-total'));
    this.post.comments_pages = Number(data.headers.get('x-wp-totalpages'));
    this.content_ready = true;
  });

The getComments function :

export class myservice extends DataService {
  protected url = 'my_url';

  constructor( protected http: HttpClient ) {
    super(http);
  }

  getComments(){
    return this.get(
      this.subUrl, true );
  }
}

Relevant part of dataService :

export class DataService {
  protected url: string;

  constructor(protected http: HttpClient) {}

  get(subUrl:string = '', needObservable = false) {
    let options = {};
    if(needObservable) {
      options = {observe: 'response'};
    }
    return this.http.get(this.url + subUrl, options);
  }
}

All of this works well. The thing is, my IDE (phpstorm) is complaining about the data.headers and data.body, arguing that those properties does not exists on type 'object'.

How to let it know that everything is fine ? I thought about typing the return but not succeeding in doing so.

like image 474
Jeremy Belolo Avatar asked Jul 22 '18 09:07

Jeremy Belolo


People also ask

How to use observables in angular?

In Angular, Observable instances are used to specify subscriber functions. Any time a consumer wants to execute the function, the subscribe () method is called. The subscribe () method provides a way to retrieve messages and values to be published. Let’s create new angular project to play around with using Observables in Angular.

What is the difference between promise and observable in angular?

An observable is a technique to handle sharing data. It’s considered the better version of a promise and is used extensively throughout Angular. Where a promise can only return a single value, an observable can return a stream of values. A promise cannot be cancelled, but an observable can be.

What is the return type of observable in JavaScript?

The return type is Observable<SearchItem[]>, it’s going to return an observable where each item in the observable is SearchItem[], each item in the observable is going to be an array of SearchItems.

What is the return type of observable<searchitem[]>?

The return type is Observable<SearchItem []>, it’s going to return an observable where each item in the observable is SearchItem [], each item in the observable is going to be an array of SearchItems.


1 Answers

This is not related to the IDE. Just Typescript's compiler is arguing that type object does not has those properties and it is right.

Just set the type of data to any - data: any.

.subscribe((data: any) => {
    this.post.comments = data.body;
    this.post.comments_count = Number(data.headers.get('x-wp-total'));
    this.post.comments_pages = Number(data.headers.get('x-wp-totalpages'));
    this.content_ready = true;
});
like image 145
Suren Srapyan Avatar answered Oct 08 '22 12:10

Suren Srapyan