Extracting data in ngOnInit hook:
ngOnInit(): void {
var routeParameters = zip(
this._route.parent.paramMap,
this._route.paramMap
).pipe(
map(([parentMap, currentMap]) => ({ customerID: parentMap.get('id'), siteID: currentMap.get('siteID') })),
filter(e => !!e.customerID && !!e.siteID)
);
const [creating, editing] = routeParameters.pipe(partition(e => e.siteID === 'new'));
creating.subscribe(e => this._ss.getEditedSite(null, Number(this.customerID)));
editing.subscribe(e => this._ss.getEditedSite(Number(e.siteID), Number(this.customerID)));
}
Although it functions in the development environment, an error is displayed:
Error: Argument of type 'UnaryFunction<Observable<{ customerID: string; siteID: string; }>, [Observable<{ customerID: string; siteID: string; }>, Observable<{ customerID: string; siteID: string; }>]>' is not assignable to parameter of type 'OperatorFunction<{ customerID: string; siteID: string; }, unknown>'.
Type '[Observable<{ customerID: string; siteID: string; }>, Observable<{ customerID: string; siteID: string; }>]' is not assignable to type 'Observable<unknown>'
Additionally, after the partition
operator, type inference ceases to work, resulting in creating
and editing
being of type Observable<unknown>
. This issue prevents project build for production due to the same reason.
Is there an appropriate way to utilize the partition
operator in Angular projects?
UPDATE
The partition declaration from 'rxjs/operators':
export declare function partition<T>(predicate: (value: T, index: number) => boolean, thisArg?: any): UnaryFunction<Observable<T>, [Observable<T>, Observable<T>]>;
Hence, the correct usage is:
const [creating, editing] = partition(e => e.siteID === 'new')(routeParameters);
Yet, type inference remains ineffective, leading to a compilable source string:
const [creating, editing] = partition<{ customerID: string, siteID: string }>(e => e.siteID === 'new')(routeParameters);
However, this approach is inconvenient. As an alternative, incorporating an if-else
statement within the subscribe
callback is considered, despite it not being the "best practice" reactive approach. Any suggestions?