I want to read a specific variable from a JSON file in Angular. I can read it fine with HttpClient but the compiler gives me an error when I access the specified variable, however the site renders fine in the browser and everything works fine despite having the error in the VSCode compiler. How can I avoid this error.
constructor(private http: HttpClient) {
this.http.get('../../assets/config/configuration.json').subscribe(res => {
console.log(res.serverIp);
});
}
The error is as follows: ERROR in src/app/services/options.service.ts(14,23): error TS2339: Property 'serverIp' does not exist on type 'Object'.
The error is clear, but how can I avoid it?
The typescript is failing you, you have to specify the type of data that you are going to receive in "res" through an interface =>
or better yet, declare the interface for your response
It is highly recommended to use the interfaces, to fully use the power of the typescript.