11

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?

BlackHoleGalaxy
  • 9,160
  • 17
  • 59
  • 103

1 Answers1

21

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...

kevmo314
  • 4,223
  • 4
  • 32
  • 43
  • 1
    You are right to be surprised. The types do propagate for me, there must be something else going on for the OP. It would be interesting to see what that is. – cartant May 24 '18 at 23:58
  • They don't propagate in some cases (using for example Angular ngrx under some specific circumstances) – BlackHoleGalaxy May 25 '18 at 00:16