Ved Prakash
Ved Prakash

Reputation: 39

Typescript Error Property 'comments' does not exist on type '{}'

This is my service which returns Typescript Error Property 'comments' does not exist on type '{}'. when app initilize and not after first comment is commented

getMessages() {
    let observable = new Observable(observer => {
      this.socket = io(this.url);
      this.socket.on('add comment', (data) => {

        observer.next(data);    

      });
      return () => {
        this.socket.disconnect();
      };  
    })   

    return observable;
  } 

and this is my function

ngOnInit() {

       this.connection = this.chatService.getMessages().subscribe((comment) => {
      console.log("comment",comment);
          comment.comments[0].createdBy.profilePicture = comment.comments[0].createdBy.profilePicture;
          this.issue.comments.push(comment.comments[0]);
      })
  } 

this is my service which returns Typescript Error Property 'comments' does not exist on type '{}'. when app initialize and not after first comment is commented

Upvotes: 1

Views: 486

Answers (1)

Poul Kruijt
Poul Kruijt

Reputation: 71931

You should add a type definition to your new Observable creation. Otherwise it defaults to an empty object.

This is where the error comes from. Perhaps you want to make a Comment object, but without knowing your intentions you can change it to this:

let observable: Observable<any> = new Observable<any>(observer => {

Upvotes: 1

Related Questions