Utilizing a helper service to simplify httpClient calls, I am eager to enforce strong typing on the Observable being returned.
In my service where I utilize the api Service and attempt to obtain a strongly typed observable that emits:
export class ApiUser(){
constructor(private api: Api){}
getUsers(url? : string): Observable<Array<User>>{
return this.api.get<Array<User>>('users'); //here I encounter type errors.
}
}
Within my api helper service, the type in question should be passed:
import { HttpClient, HttpParams } from '@angular/common/http';
import { Injectable } from '@angular/core';
import {Observable} from "rxjs/Observable";
import {User} from "../../interfaces/interfaces";
/**
* Api is a generic REST Api handler.
*/
@Injectable()
export class ApiService {
constructor(public http: HttpClient) {}
get(endpoint: string, params?: any, reqOpts?: any){
return this.http.get('https://sub.domain.com/api/v1/' +
endpoint,reqOpts);
}
}
The current challenge lies in the fact that this may not be the most appropriate method for enforcing strong typing. While it works if I specify types within this.http.get
in the api helper service, it contradicts the purpose of having such a helper service.