Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to type array map arguments from rxjs 6 withLatestFrom

Before Rxjs 6 we could do:

interface TypeA {
  payload: any;
}

source$.pipe(
  withLatestFrom(source2$, (source1: TypeA, source2: TypeB) => 
       ({ payload: source1.payload, source2 }) ),
)

We could, in the resultSelector method arguments, add proper types for source1 and source2, passed along within the constructed object here.

But now we must do the following:

source$.pipe(
  withLatestFrom(source2$),
  map(([source1, source2]) => ({ source1, source2 }) ),
)

Doing so we are unable to add type on source1 and source2 within array argument. The typing is then lost and IDE doesn't suggest .payload on source1 for example.

How to be able, using the new syntax, to add proper typing with array arguments?

like image 537
BlackHoleGalaxy Avatar asked May 24 '18 21:05

BlackHoleGalaxy


1 Answers

You can add it like you would a tuple:

source$.pipe(
  withLatestFrom(source2$),
  map(([source1, source2]: [TypeA, TypeB]) => ({ source1, source2 }) ),
)

Although I am surprised you don't get typing automatically on it, I thought it did propagate them...

like image 71
kevmo314 Avatar answered Nov 20 '22 07:11

kevmo314