'Why property does not exist on type in rxjs pipe function?
Why I losing the type in my pipe
function? How to fix that?
Property 'n' does not exist on type '{}'
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 });
Solution 1:[1]
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$),
Solution 2:[2]
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');
})
);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Фарид Ðхмедов |
Solution 2 | Andres2142 |