Dasun Pubudumal
Dasun Pubudumal

Reputation: 97

WebStorm displaying an Error when the map object in Angular is accessed

In one of the services in my Angular project, I have called the map() function to get return data from GitHub API.

getUser(username: string) {
// RegEx used for String Manipulation
return this.http.get('https://api.github.com/users/'.concat(username))
 .map(res => res);}

However, when I try to call this method like this,

this.githubService.getUser('dasunpubudumal')
  .subscribe(user => {
    console.log(user.created_at); });

my WebStorm IDE displays a red line under created_at attribute. It says TS2339 Property created_at does not exist on type 'Object'. However, when I run the code, it displays the created_at field of the JSON object which is returned from that endpoint.

Is there something which I am doing wrong? Is there any way that I could get rid of this error? I'm using HttpClient module for requesting from the endpoints.

Upvotes: 1

Views: 125

Answers (1)

Dasun Pubudumal
Dasun Pubudumal

Reputation: 97

Found the answer. When implementing the method, I haven't declared the type of the object.

The function should be like this.

getUser(username: string) {
return this.http.get<User>('https://api.github.com/users/'.concat(username))
 .map(res => res);

}

Upvotes: 1

Related Questions