ReactiveX / ReactiveX/rxjs

(TypeScript limitation?) scan typings still not completely accurate

Open
#6,649 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

7.x 8.x TS
Dominant language
TypeScript
Stars
31.7k
Forks
3k
PR merge metrics
No merged PRs in 30d

Description

Frequently, a user may want to initialize state with some initial value so the scan is called on each pass, but the reducer will never return that value type.

In this scenario the types still come out wrong:

import { of, scan } from 'rxjs';

// result$ should be type `Observable<number>`, but is `Observable<number | null>`
const result$ = of(1, 2, 3).pipe(
  scan((state, value) => {
    return (state ?? 0) + value;
  }, null as number | null)
);

result$.subscribe(console.log); // 1, 3, 6

Or maybe it's this, which will have all sorts of type errors:

import { of, scan, map } from 'rxjs';

const result$ = of(1, 2, 3).pipe(
  scan((state, value) => {
     if (state.type === 'init') {
        return { type: 'first', message: `First we got ${value}` };
     } else {
        return { type: 'update', message: `${state.message}, then we got ${value}` };
     }
  }, { type: 'init' }),
  map(({ message }) => message + '.'),
)

result$.subscribe(console.log);

/*
First we got 1.
First we got 1, then we got 2.
First we got 1, then we got 2, then we got 3.
*/

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the scan entry point and reproduce both TypeScript examples from the issue, checking the inferred Observable result types and the map callback errors. Trace the scan typings and existing type tests, if present; done means the examples infer the intended accumulator result types and compile without unrelated errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
developer-experience
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.