1

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 !!!

3
  • 1
    Please provide code that shows what you're doing. Commented Dec 16, 2018 at 7:37
  • Hi edited my question with code Commented Dec 16, 2018 at 7:43
  • Do you understand the purpose of generics in general? Commented Dec 16, 2018 at 8:08

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.