2

I am getting typescript error Expected 0 type arguments, but got 1 for line that returns get call. What is wrong with my get call?

 public get(params: SummaryParams): Observable<Summary[]> {
        const uri = `${this.config.URLS.LOAD_SUMMARY}`;
        const params = new HttpParams()
                      .set('startDate', params.startDate.toString())
                      .set('endDate', params.endDate.toString())
                      .set('userId', params.userId);

        return this.http.get<Summary[]>(uri, { params });
      }
5
  • http.get should get just uri, try using post method Commented Jun 3, 2018 at 10:35
  • 1
    @Arash Actually, that's not true. It takes an optional options object as well angular.io/api/common/http/HttpClient#get Commented Jun 3, 2018 at 10:38
  • you are correct, but I still agree with post request :D . when you wanna post data to back-end , you should use post method. Commented Jun 3, 2018 at 10:43
  • 1
    @Arash It's not evident that data is posted. It could be search query. Commented Jun 3, 2018 at 10:44
  • You can change the last line to return <Summary[]>this.http.get(uri, { params }); Commented Jun 3, 2018 at 11:00

1 Answer 1

7

HttpClient has generic methods that can be used to provide response type. Http doesn't.

The error means that <Summary[]> generic parameter wasn't expected, and http isn't an instance of HttpClient; likely an instance of Http.

If the application uses Angular 4.3 or higher, Http should be replaced with HttpClient. In case Http should be used, a response should be transformed, this is one of few differences between HttpClient and Http:

    return this.http.get(uri, { params })
    .map(res => <Summary[]>res.json());
Sign up to request clarification or add additional context in comments.

2 Comments

Because I had to look it up in the official angular docs: The variable name for an instance of HttpClient is still recommended to be http.
I had this error because my editor added the http import even though I already had HttpClient. So check for "import * as http from 'http';"

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.