An error occurs when implementing the RxJS partition operator in combination with Angular

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?

Answer №1

It appears that you may be importing the incorrect partition() function. The partition() operator is no longer supported and it seems like you are importing partition from rxjs and attempting to use it as an operator regardless. Please refer to https://github.com/ReactiveX/rxjs/blob/master/src/internal/operators/partition.ts#L52

To resolve this issue, make sure to import it correctly:

import { partition } from 'rxjs';

// Instead of this
import { partition } from 'rxjs/operators';

Then you can use it as you would with any other Observable creation method:

const [creating, editing] = partition(routeParameters, e => e.siteID === 'new');

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How to retrieve a parameter value within the app component in Angular 2

Within my appcomponent, I have incorporated a dropdown functionality. Whenever the user selects an option from the dropdown, it loads a new page in the router outlet. However, if I refresh the page, the router loads correctly but the dropdown selection i ...

Issues with Node.js and Socket.io: Receiving error "Cannot read property 'on' of undefined"

I've encountered an issue while trying to establish a websocket connection using socket.io and node.js. Here is the setup I'm working with: Operating System: Windows 7 Node version: 6.9.2 NPM version: 4.0.5 Packages: "express": "^4.14.0" "soc ...

Demystifying the Mechanics of RxJS Subscriptions during an HTTP Request

export class VendorHttpService { result = '0'; constructor(private http: HttpClient, private global: GlobalService) { } getProfileStatus(uid: String): string { this.http.get(this.global.getUrl()+"/vendor/profile-status/"+uid) ...

What is the best way to upload a photo taken with the Android camera using FormData in Ionic/Angular?

Currently in the process of developing an Android app that allows users to capture and upload their picture to a PATCH API endpoint with the key 'avatar'. To accomplish this, I am utilizing the Cordova Camera along with the Advanced HTTP plugin ...

Setting the root directory and output directory can be a bit tricky when dealing with source code scattered across multiple folders. Here's

Utilizing TypeScript in my Node.js project, I previously had a directory structure that looked like this: node_modules src -Models -Routes -Middlewares -Controllers -index.ts package.json tsconfig.json In ...

Utilizing raw queries in TypeORM with NestJS to enforce lowercase column names

Can anyone help me execute a query using nest/typeorm? I'm utilizing Typeorm's "InjectConnection" to run a raw query in my Postgres Database. The issue arises with the column user_roles_role.userId (note that I am specifying 'userId' i ...

Interacting Between Module Components in Angular 2

I have a situation where I have one component that needs to pass data to another component located in a different module. The app.component acts as the parent of these child modules. However, they are only connected through routing, so they are not technic ...

What is the best way to retrieve a nested object array using a Signal in Angular/Typescript?

In my Angular/Typescript code, I am encountering an issue with filtering a nested object array based on the object property where value === 'event'. Despite my efforts, the code is returning the parent object array CalendarModel[] instead of the ...

Error in Protractor Typescript: The 'By' type does not share any properties with the 'Locator' type

https://i.stack.imgur.com/8j2PR.png All the different versions Error. Protractor version : 5.2.0 npm : 3.10.10 node :6.9.5 typescript :2.6.0 The 'By' type does not share any properties with the 'Locator' type What is the solution to ...

Error in file @angular/service-worker/src/module.d.ts at line 30, character 9: The type 'ModuleWithProviders' is not a generic type

https://i.sstatic.net/01bwW.pngI have successfully integrated "angular-push-notifications" into my angular-4 project by following the instructions from the link below: I have configured the "Service Workers" as shown below 1. Install the package using np ...

Issue TS2322 occurs when an element is not compatible with type ReactElement. Mysteriously, this error appears inconsistently within the application

I have successfully resolved the error mentioned, but I am seeking clarification on why it is occurring in this specific instance and not in other parts of my application. Additionally, I am curious as to why a function and a ternary with the same type sig ...

Displaying a disabled div depending on the dropdown selection in Angular Material

My goal is to display filters in a dropdown after they have been selected. Currently, I have static disabled divs and a dropdown where filters can be selected. This is the dropdown: <mat-form-field> <mat-label>{{ 'supplier.showFilters&a ...

Can you provide guidance on integrating TypeScript with React version 15.5?

I'm looking for the best approach to integrate TypeScript and React following the separation of PropTypes into a separate project with version 15.5. After upgrading from 15.4 to 15.5, everything seems to be running smoothly except for a warning in th ...

Issue with modal-embedded React text input not functioning properly

I have designed a custom modal that displays a child element function MyModal({ children, setShow, }: { children: JSX.Element; setShow: (data: boolean) => void; }) { return ( <div className="absolute top-0 w-full h-screen fle ...

What is the correct way to define the onClick event in a React component?

I have been encountering an issue while trying to implement an onClick event in React using tsx. The flexbox and button are being correctly displayed, but I am facing a problem with the onClick event in vscode. I have tried several ideas from the stack com ...

Is there a preferred method for correctly nesting components?

It may seem like a simple question, but I couldn't find the answer in the documentation or code examples. Consider this example: import { FlowIdentification } from "./flow-identification"; @customElement("bb-flow") export class R ...

Experiencing a lengthy installation process of TypeScript on Node.js

I attempted to set up TypeScript on my laptop running MS Windows 8.1 (64-bit). After installing Node.js 64-bit, I ran the command npm install -g typescript. However, the installation appeared to stall with a spinning '/' for over 2 hours. When I ...

Angular 2 radio button problem with model-driven form

I'm facing a challenge with getting radio buttons to function correctly in my reactive form. Below is the code for my component: export class SettingsFormComponent implements OnInit { public sForm: FormGroup; public submitted: boolean; d ...

"An error occurred while trying to resolve "npm" from npm.fontawesome.com

Within my Angular project, I am utilizing a module from When I run the following command: npm --loglevel info install grun locally, it finishes without any issues. However, when I run this command on the build server, an error occurs. In my .npmrc file: ...

What is the best way to hide the back arrow in Cordova UWP for Ionic 4?

I am currently developing a Windows 10 (UWP) application using Ionic 4 (Angular). I want to remove the back arrow from the interface: Example of back arrow I have attempted various solutions, such as implementing in the app.component constructor with in ...