16

I'm trying to fetch data from API using interface. Bellow is my temp interface

export interface ITemp {
    id: number,
    name: string,
    age:  number
}

And below is my HTTP service, where there is a fn getHomedetails, which calls an API.

import {Injectable} from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { ITemp } from "../interfaces/temp";
import { Observable } from "rxjs/Observable";
import 'rxjs/Rx';

@Injectable()
export class HttpService{

    http:any;
    baseUrl: String;

    constructor(http:HttpClient){
        this.http = http;
        this.baseUrl = 'some_url';
    }

    getHomeDetails(): Observable<ITemp>  {
        return this.http.get<ITemp>(this.baseUrl); //problem is here 
        //when mouse is pointed on get<ITemp> it shows "Untyped function calls may not accept type arguments"

    }

}

An interface doesn't get defined. I don't know what I'm doing wrong. And the above syntax is an angular 4.3X syntax. The editor which I've used are the sublime and visual studio.

Trufa
  • 39,971
  • 43
  • 126
  • 190
nishil bhave
  • 582
  • 1
  • 5
  • 17

1 Answers1

30

This is because you're giving your class-level http a type of any:

Change http:any; to http: HttpClient

A good rule of thumb is to not use any unless you really really have to.

UncleDave
  • 6,872
  • 1
  • 26
  • 44
  • it's interesting how similar this guideline is to .NET policy of do not use pointers unless you really really have to and if you do the compiler will force you to label your code as "unsafe" and change your project configuration to "allow unsafe code". – fartwhif Nov 09 '21 at 18:49
  • My code actually specifies HttpClient as the type of the variable, it's actually almost exactly the same as OPs code with that exception, so I'm at a loss, as I'm not using the "any" type for anything in the erroronous .ts file – fartwhif Nov 09 '21 at 18:54
  • 1
    Create a minimal reproduction in Stackblitz and the issue should be spottable. – UncleDave Nov 09 '21 at 18:59
  • I appreciate the diligence, but it turns out I was operating in a nested function and so the variable scopes were different. Fixed it by creating a utility function like nishil's pattern – fartwhif Nov 09 '21 at 19:14