Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The 'this' context of type 'void' is not assignable to method's 'this' of type 'Observable<string>'

I am getting this error in angular's autocomplete example. Here is the code:

ngOnInit() {
      this.filteredOptions = this.myControl.valueChanges
          .pipe(
            startWith(''),
            map(val => this.filter(val))
          );
}

The error is spawn on startWith. I am also getting an error in the second val. It says:

Argument of type '{}' is not assignable to parameter of type 'string'

The function is:

 filter(val: string): string[] {
    return this.options.filter(option =>
      option.toLowerCase().indexOf(val.toLowerCase()) === 0);
  }

HINT: There is something with mixed controls or imports. I am not sure but when I create a new component everything works fine.

like image 308
Cap Barracudas Avatar asked Mar 07 '18 15:03

Cap Barracudas


1 Answers

I just had the same issue and it turns out I was importing map and startWith from the wrong directory.

import {map, startWith} from "rxjs/operators";

Use this to import both map and startWith and it should work and compile.

like image 171
user3605709 Avatar answered Oct 17 '22 23:10

user3605709