The official angular HttpClient guide suggests building an HTTP request service in the following way:
getConfig() {
// now returns an Observable of Config
return this.http.get<Config>(this.configUrl);
}
But also provides the following disclaimer:
Specifying the response type is a declaration to TypeScript that it should treat your response as being of the given type. This is a build-time check and doesn't guarantee that the server will actually respond with an object of this type. It is up to the server to ensure that the type specified by the server API is returned.
While I like the main concept of having a service that returns a well-defined type, the disclaimer shows that this is not completely ready for the real world where no one can rely on the format of a response of some external software system.
What is the best practice of implementing an HTTP request service in Angular that will reliably return objects of an expected type (in the current example Config) or provide a means to deal with errors?