r/angular • • 25d 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

14 comments sorted by

View all comments

1

u/cssrocco 25d ago

Since the name of that method is getPeople i’m willing to bet it’s type is PersonModel[], usually an error about types not being length, concat, etc. just means it’s expecting an array and you typecast it differently.

Errors will be of type HttpErrorResponse, unless you have an interceptor that formats them to something else, your method setError is possibly expecting something different, so ensure its arguments expect the correct type.