Vyas Reddy
Vyas Reddy

Reputation: 1272

What is the use implementing interface in angular HttpClient?

I implemented interface for http get, post,put requests but I am not finding any difference with/without interface.

code

public get_api<T>(path: string) {
   return this.http.get<T>(API_ENDPOINT + path);
 }

In Other component

this.http_global.get_api<Interface>('user').subscribe(.....=>);

Any one helps ?

Thanks In Advance !!!

Upvotes: 1

Views: 1456

Answers (1)

Joshua Terrill
Joshua Terrill

Reputation: 2017

Interfaces are used for type-checking responses. In the example that you gave, I don't believe it would actually give you anything that you don't have already. However, if you were returning a specific object from the response of a GET request, you could create an interface with those properties, and cast the response of the GET request to be that interface. This will give you access to those properties down the road when you have the response and want to reference the properties inside. You can find more information about this here: https://angular.io/guide/http#type-checking-the-response

Upvotes: 2

Related Questions