After enabling the strictFunctionTypes
check in my Angular 11.1.1 project, I encountered an issue with a function that uses RxJs combineLatest
. The type passed to the subscription remains an array and is not automatically destructured into individual variables, causing an error.
Disabling strictFunctionTypes
resolves the issue, but I'm looking for a way to refactor the code to adhere to the strictFunctionTypes
rule.
combineLatest([
this.user$.pipe(startWith(null as User)),
this.order$.pipe(startWith(null as Order)),
this.invoice$.pipe(startWith([] as Faktura[])),
this.document$.pipe(startWith([] as Document[]))
]).pipe(
tap(([ order, user, invoices, docs ]: [ Partner, User, Faktura[], Document[]]) => {
this.ordersCache$.next(order);
this.usersCache$.next(user);
this.invoicesCache$.next(invoices);
this.docsCache$.next(docs);
}),
catchError(() => of([
this.ordersCache$.value,
this.usersCache$.value,
this.invoicesCache$.value,
this.docsCache$.value,
])),
takeUntil(this.destroy$)
).subscribe({
// The type is (Order | User | Faktura[] | Document[])[] and does not
// automatically destructure it into the single entities.
// This make the strictFunctionTypes rule fail as it does
// not match the given parameter
next: ([ order, user, invoices, docs ]: [
Order,
User,
Faktura[],
Document[]
]) => {
// ...
},
error: (err) => {
throw new Error(err);
}
});
UPDATE
Upon trying kvetis suggestion of using tap instead of map and omitting the return, the error still persists:
https://i.sstatic.net/h3p9o.png
The ongoing issue seems to be related to the catchError
block because when removed, the exception no longer occurs.