简体   繁体   中英

Rxjs observable, Argument of type 'unknown' is not assignable to parameter of type 'String'

I'm toying around with Rxjs and was trying to see how subscriptions worked. This is my code:

export class AppComponent implements OnInit {
  title = 'hello_world_project';
  ngOnInit() {
    const rickRoll = new Observable((observable) => {
      observable.next('Never gonna give you up');
      observable.next('Never gonna let you down');
    });

    rickRoll.subscribe(this.uselessWrapperFunc);

    console.log('Rxjs is fun');
  }

  uselessWrapperFunc(message: String): void {
    console.log(message);
  }
}

And it gave me the error saying "Argument of type 'unknown' is not assignable to parameter of type 'String'"

However, if I try doing rickRoll.subscribe(console.log) , it worked as expected. So in this case, why has console.log seemingly been called with the value from the observable, while the uselessWrapperFunc seems to have been passed in the entire observable itself?

Thanks! Any insight will be appreciated

Update: Would specifying the type of the observable help?

Solved it, turns out all I had to do was doing const rickRoll: Observable<String> = new Observable() instead of just const rickRoll = new Observable()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM