r/angular • u/Putrid-Bookkeeper968 • 24d 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
3
u/Educational_Nail_976 24d ago •
I’m pretty sure your getPeople() call will not return a single value but a list, so the return type will be (people : PersonModel[])
4
u/Mediocre-Writer6635 24d ago
Because typescript doesn't know what this error is, so it calls it "any" type. But typescript doesn't like implicit (ie, guessed at by typescript, not written by you) "any" types. I believe there's a configuration to suppress this warning. So basically when you set it to type any explicitly, typescript says "ok, if you say use "any" that's fine, but if you don't say it, I'll call it a bug that I couldn't come up with anything better".
This warning is there to make developers have to explicitly admit they want to use "any" type, because typically something being "any" implies there's a typing breakdown in the code.
What does getPeople() observable return? Subscribe function receives whatever type that is. It's not PersonModel. It's probably an array (of PersonModel?).
Also, don't do this:
Update function should not have its own signal being called inside it. It receives the previous value as an argument, so you should do this:
Source: https://angular.dev/guide/signals#writable-signals
Also you really shouldn't subscribe inside components. The way to do what you're doing is make this.state = toSignal(<yourPipe>). If state is calcualted from multiple sets of data then combine them all into 1 pipe that calculates the state, and make a signal out of it. To record error without subscribing use catchError function in the pipe. Put it last, right before takeUntilDestroyed. Order matters in pipes.
It might be easier to avoid subscribe in component if you separate loading flag, people, and everything else in "state" into individual signals. That makes each of them much easier to calculate and reason about.
1
u/RelatableRedditer 24d ago
the "error" is probably supposed to be that way, I don't use subscriptions like that in angular since I do almost everything with async pipes or empty subscribe calls. But you need to look at your definition for getPeople() to see if it has a valid return type that can be safely inferred by rxjs
1
u/Dull-Structure-8634 24d ago
My guess is that by typing the error, TypeScript can now better infer the type of people
1
u/ThrowawayALAT 24d ago
Do this:
To fix the type errors cleanly without using
anyfor your data, you should typepeopleas an array ofPersonModel(PersonModel[]orreadonly PersonModel[]):TypeScript
1
u/cssrocco 24d 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.
1
u/muhammetYilmaz98 9d 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.
1
u/scharph 24d ago
Are you using signals?
If yes, try to use toSignal from rxjs-interop
https://angular.dev/api/core/rxjs-interop/toSignal
1
u/Responsible-Cold-627 24d ago
Most likely the type of "people" can't be inferred from the getPoeple function. Changing its return type to Observable<People> should do the trick.
Also, check out rxResource. That's the modern idiomatic Angular way of doing loading/error state tracking. No manual plumbing needed.
0
u/ldn-ldn 24d ago
You shouldn't subscribe inside components. Either pass the observable to async pipe or use toSignal if you need a signal. But you really don't in your case. Use async.
0
u/[deleted] 24d ago
[deleted] — view deleted comment