Home > Back-end >  Why property does not exist on type in rxjs pipe function?
Why property does not exist on type in rxjs pipe function?

Time:05-13

Why I losing the type in my pipe function? How to fix that?

Property 'n' does not exist on type '{}'

stackblitz

import { of, map, Observable, Subject, pipe, tap } from 'rxjs';
import { exhaustMap, withLatestFrom } from 'rxjs/operators';

const action = new Subject<{ n: number }>();
const id$ = of(1);

const bar = () =>
  pipe(
    withLatestFrom(id$),
    exhaustMap(([{ n }, id]) => of([n, id])), // <--- Property 'n' does not exist on type '{}'
    tap(() => {
      console.log('in bar pipeline');
    })
  );

const foo = action.pipe(bar());

foo.subscribe();

action.next({ n: 1 });

CodePudding user response:

pipe infers all types from passed parameters. Take a look on https://github.com/ReactiveX/rxjs/blob/master/src/internal/util/pipe.ts

Try to specify type for first parameter for auto-inferring for all.

withLatestFrom<{ n: number }, number[]>(id$),

CodePudding user response:

Add the proper data type:

const bar = () =>
  pipe(
    withLatestFrom(id$),
    exhaustMap(([{ n }, id]: [ {n: number}, number ]) => of([n, id]))
    tap(() => {
      console.log('in bar pipeline');
    })
  );
  • Related