Title: The 'this' context of type 'void' is not assignable to method's 'this' of type 'Observable<string>'
Tags: angular;angular-material
Question: I am getting this error in angular's autocomplete example. Here is the code:
ngOnInit() {
this.filteredOptions = this.myControl.valueChanges
.pipe(
startWith(''),
map(val =&gt; 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 =&gt;
option.toLowerCase().indexOf(val.toLowerCase()) === 0);
There is something with mixed controls or imports. I am not sure but when I create a new component everything works fine.
Comment: Would you please show the full code for the function the error is in?

Comment: Seems like myControl is not a control, it's a FormGroup? Because then valueChanges would emit objects instead of strings.


Here is another answer: My case was that I loaded take(1) automatically by autopopulating from rxjs/compat/take but not from rxjs/operators, and catch this issue. Not so obvious thing :)

Here is another answer: 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.
Comment for this answer: Same issue here -> import { delay } from 'rxjs/operator/delay'; instead of import { delay } from 'rxjs/operators/delay';

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 2, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2018:00:03/07/18', 'FavoriteCount': 1.0, 'PostTypeId': 1, 'Tags': '<angular><angular-material>', 'Title': "The 'this' context of type 'void' is not assignable to method's 'this' of type 'Observable<string>'", 'ViewCount': 21293, 'id': 49155047, 'user_AboutMe': '<p>“Learning never exhausts the mind”\n― Leonardo Da Vinci</p>\n', 'user_AccountId': 7477788.0, 'user_DisplayName': 'Cap Barracudas', 'user_Reputation': 2140.0, 'user_Views': 124.0, 'user_id': 5683645.0}}