【问题标题】:How to type array map arguments from rxjs 6 withLatestFrom如何从 rxjs 6 withLatestFrom 键入数组映射参数
【发布时间】:2018-11-04 05:22:57
【问题描述】:

在 Rxjs 6 之前我们可以这样做:

interface TypeA {
  payload: any;
}

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

我们可以在 resultSelector 方法参数中为 source1source2 添加适当的类型,并在此处在构造对象中传递。

但现在我们必须做到以下几点:

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

这样做我们无法在数组参数中的 source1 和 source2 上添加类型。然后输入会丢失,例如 IDE 不会在 source1 上建议 .payload

如何使用新语法添加正确的数组参数类型?

【问题讨论】:

    标签: typescript rxjs rxjs6


    【解决方案1】:

    您可以像添加元组一样添加它:

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

    虽然我很惊讶你没有在上面自动输入,但我认为它确实传播了它们......

    【讨论】:

    • 您感到惊讶是对的。这些类型确实为我传播,OP 必须有其他事情发生。看看那是什么会很有趣。
    • 它们在某些情况下不会传播(例如在某些特定情况下使用 Angular ngrx)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-07
    • 1970-01-01
    • 1970-01-01
    • 2022-12-05
    • 2023-03-17
    相关资源
    最近更新 更多