Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

types inferring problem with scan operator #3150

Closed
ghetolay opened this issue Dec 1, 2017 · 4 comments
Closed

types inferring problem with scan operator #3150

ghetolay opened this issue Dec 1, 2017 · 4 comments
Labels
TS Issues and PRs related purely to TypeScript issues

Comments

@ghetolay
Copy link
Contributor

ghetolay commented Dec 1, 2017

I'm having a problem when using scan with 2 different type for item and accumulator/seed on ts 2.5+ (didn't try older versions).
You can see it here : https://stackblitz.com/edit/ts-fn-overload-bug?file=test.ts

of('0').pipe(
  scan((acc, str) => acc + parseInt(str), 0)
);

gives the following error :

Argument of type 'UnaryFunction<Observable, Observable>' is not assignable to parameter of type 'UnaryFunction<Observable, Observable>'.
Types of parameters 'source' and 'source' are incompatible.
Type 'Observable' is not assignable to type 'Observable'.
Type 'string' is not assignable to type 'number'.

Even if scan has a signature matching that use case (the last one) :

function scan<T>(accumulator: (acc: T, value: T, index: number) => T, seed?: T): MonoTypeOperatorFunction<T>;
function scan<T>(accumulator: (acc: T[], value: T, index: number) => T[], seed?: T[]): OperatorFunction<T, T[]>;
function scan<T, R>(accumulator: (acc: R, value: T, index: number) => R, seed?: R): OperatorFunction<T, R>;

As it's shown on the stackblitz, order seems to matter so it's most likely a ts bug.
But looking closely on those types, @NaridaL pointed out that the last signature should be enough to cover all cases. Indeed R can totally be inferred with the same type as T or T[].

So the question is : is there any reason for having those subtype signatures ? especially since now it seems to create a bug ?

P.S: Couldn't find a corresponding ts issue but didn't do much digging
P.S2: I think the same applies for reduce.

@ghetolay ghetolay changed the title problem inferring scan types types inferring problem with scan operator Dec 2, 2017
@cartant
Copy link
Collaborator

cartant commented Dec 2, 2017

Probably related to #2897. See discussion therein.

@ghetolay
Copy link
Contributor Author

ghetolay commented Dec 2, 2017

Oh sorry, I missed that.

But now there is a game changer as it's causing errors. I'll dig into typescripts issues see if I can find something.

@ghetolay
Copy link
Contributor Author

ghetolay commented Dec 2, 2017

I thought the problem was because I updated my ts but it's not, it's because I refactored my code to use pipe.
I can go as low as 2.0 with that code and I got the same error while the non-pipeable version, with this on the signature, works fine.

I found this new issue microsoft/TypeScript#20305 but I'm not sure it's exactly the same problem.

@cartant cartant added the TS Issues and PRs related purely to TypeScript issues label Jan 28, 2019
@benlesh
Copy link
Member

benlesh commented May 24, 2021

This has been addressed to the best of our ability in RxJS 7.

@benlesh benlesh closed this as completed May 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
TS Issues and PRs related purely to TypeScript issues
Projects
None yet
Development

No branches or pull requests

3 participants