Rxjs: Partition operator example throws typescript error

Created on 12 Dec 2018  路  8Comments  路  Source: ReactiveX/rxjs

Documentation Related To Component:

partition operator
https://www.learnrxjs.io/operators/transformation/partition.html
https://github.com/ReactiveX/rxjs/blob/master/src/internal/operators/partition.ts

Please check those that apply

  • [ ] typo
  • [ ] documentation doesn't exist
  • [ ] documentation needs clarification
  • [x] error(s) in example
  • [ ] needs example

Description Of The Issue

Opening the example in Stackblitz we are getting typescript error:

Argument of type 'UnaryFunction<Observable<number>, [Observable<number>, Observable<number>]>' is not assignable to parameter of type 'OperatorFunction<any, any>'.
  Type '[Observable<number>, Observable<number>]' is not assignable to type 'Observable<any>'.
    Property '_isScalar' is missing in type '[Observable<number>, Observable<number>]'.

typescript is not complaining about this usage:

const [evens, odds] = partition((val: number) => val % 2 === 0)(source);

Result is the same.

I did not check other example or other online editors but IMHO it would be the same.

I do not have more time for making PR, also I am not sure about whether implementation should be fixed not to confuse typescript or syntax in examples should follow the usage I have provided here.
Sorry.

Thank you for your time and happy coding :)

EDIT:
I have just check jsBin and jsFiddle. I have find out that they are not relevant as they does not support typescript errors. I have tested const num: Number = 'text'; and they both did not complain at all.

Help Wanted feature

Most helpful comment

Partition should really have been a static function, and not an operator. That's the real solution here. And also updating the docs.

TODO:

  1. deprecate the partition operator.
  2. Move it to be a static creation function.
  3. Update the docs and tests accordingly.

All 8 comments

I have just found https://github.com/ReactiveX/rxjs/issues/4119, but I have got the same error in my local project using:

WebStorm 2018.3
Build #WS-183.4284.130, built on November 19, 2018
JRE: 1.8.0_152-release-1343-b15 amd64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
Windows 7 6.1
Angular CLI: 7.1.0
Node: 8.11.4
OS: win32 x64
Angular: 7.1.1
... animations, cdk, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router

Package                           Version
-----------------------------------------------------------
@angular-devkit/architect         0.11.0
@angular-devkit/build-angular     0.11.0
@angular-devkit/build-optimizer   0.11.0
@angular-devkit/build-webpack     0.11.0
@angular-devkit/core              7.1.0
@angular-devkit/schematics        7.1.0
@angular/cli                      7.1.0
@ngtools/webpack                  7.1.0
@schematics/angular               7.1.0
@schematics/update                0.11.0
rxjs                              6.3.3
typescript                        3.1.6
webpack                           4.23.1

So now blame who? :D

That error is the "correct" error. Partition isn't usable within pipe because it's not really an operator, for the reason I explained in 4119:

Partition returns a UnaryFunction, not an OperatorFunction, and pipe only takes OperatorFunctions as arguments. It seems like partition was broken (at least when used in pipe) with the move to pipeable operators, but the clean up work to deprecate/replace it hasn't been priority as partition isn't that widely used. See these issues:

https://github.com/ReactiveX/rxjs/issues/3797
https://github.com/ReactiveX/rxjs/issues/2995
https://github.com/ReactiveX/rxjs/issues/3807

So TS is right to complain about partition being used in pipe, but you can still use partition as a function in its own right, I believe.

(and all the StackBlitz stuff in that thread is resolved now as far as I know, so StackBlitz is giving you an accurate error)

Ok, so then examples should be fixed as they use partition as pipe now:

const [evens, odds] = source.pipe(partition(val => val % 2 === 0));

:)

Partition should really have been a static function, and not an operator. That's the real solution here. And also updating the docs.

TODO:

  1. deprecate the partition operator.
  2. Move it to be a static creation function.
  3. Update the docs and tests accordingly.

I'll grab this one if no one has started working on this?

I wonder who made the statistics that tell that partition isn't widely used. I think there are valid scenarios where you want to have to safetly partitioned Observables where you know that they will never emit the same value. My current solution is really extremely ugly and verbose.

this.isMobileLayout = this.breakpointObserver.observe([Breakpoints.Handset]).pipe(map((result) => result.matches));
        this.isDesktopLayout = this.isMobileLayout.pipe(map((value) => !value));

Hello,
Is there any update related to this or what is the current situation with the partition operator?

partition as an operator used within pipe does not make sense and is deprecated.

partition as a stand alone "creation function" can be used just fine.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

LittleFox94 picture LittleFox94  路  3Comments

cartant picture cartant  路  3Comments

Zzzen picture Zzzen  路  3Comments

cartant picture cartant  路  3Comments

jakovljevic-mladen picture jakovljevic-mladen  路  3Comments