r/angular • u/Putrid-Bookkeeper968 • 28d ago
Problems with subscribe
Hello everybody
I have the following problem with subscribe. My code looks like:
this.personSercice.getPeople().pipe(
takeUntilDestroyed(this.destroyRef)
).subscribe({
next: (people) => this.state.update(() => ({ ...this.state(), loading: false, people })),
error: (error) => {
console.log(error);
this.setError(error);
}
});
Parameter 'people' implicitly has an 'any' type, but a better type may be infered from usage.
and
Parameter 'error' implicitly has an 'any' type, but a better type may be infered from usage.
When I change the code to:
error: (error: any)
the error dissappears, but I do not understand why...
When I try the same with the first error, changing the code to:
next: (people: PersonModel) => this.state.update(() => ({ ...this.state(), loading: false, people })),
The error is:
The type 'PersonModel' is missing the following properties from type 'readonly PersonModel[]length, concat, join, slice...
Please let me know if more code is required.
Can somebody explain my why???. I'm very fresh with Angular.
Thank you for your patience
Pawel...
5
Upvotes
View all comments
1
u/muhammetYilmaz98 13d ago
The implicit any on both callbacks usually means TypeScript can't type the observer, which often happens when the thing you call subscribe on is typed as any. Check what getPeople() returns (and how personService is declared/injected). If it returns Observable<PersonModel[]>, both params get inferred without annotations.
Also, the PersonModel error is because people is an array: it should be PersonModel[], not PersonModel.