Search code examples
angulartypescriptinterfaceget

Interface use with GET method


I want typed the answer of a getter, I realized a UserResponse interface

export interface UserResponse {
  research: any;
}

I call it like this in the getter:

@Injectable()
  export class ThreadService {
    constructor(public messageService: MessageService,
                private http: Http) {
     this.http.get<UserResponse>(this.baseUrl).subscribe((data) => {
       console.log(data.research);
     });
   }
 }

here is the error :

ERROR in src/app/thread-service/thread.service.ts (149,5): Supplied parameters do not match any signature of call target.

Do I need to implement the interface ?


Solution

  • this notation

    this.http.get<UserResponse>(this.baseUrl).subscribe((data) => {
      console.log(data.research);
    });
    

    Is the one used with HttpClient, not Http. You should keep this notation and use HttpClient, because Http will be deprecated soon.