Type 'Post[]' is not assignable to type 'Observable<Post[]>': error TS2322

2490 views angular
-2

I have googled both the message and error and it seems the issue I keep finding is not what I am dealing with.

I am an angular noob and feel I am finally close to getting my service wired up.

I am getting the above error on my component

it will squawk at the this.posts below.

 getPosts() {
this.postsService.get()
  .subscribe((data: Post[]) => this.posts = data);};

and my service in case theres an issue there.

public get(): Observable<Post[]> {
return this.http
  .get<Post[]>(this.accessPointUrl);}

Someone save my morning please!

answered question

What type is this.posts? I'm guessing Observable<Post[]>?

Make sure this.posts is not Observable<Post[]> that might be your issue

1 Answer

13

just return the http.

return this.http
 .get(this.accessPointUrl);}

Declare the type of posts as post[]

posts: Post[];

posted this

Have an answer?

JD

Please login first before posting an answer.