【问题标题】:typing redux-observable@2.0.0 epics, type narrowing ofType not behaving as expected键入 redux-observable@2.0.0 史诗,键入缩小 ofType 的行为不符合预期
【发布时间】:2022-11-02 00:20:45
【问题描述】:

我遇到了将项目升级到 rxjs > 7 redux-observable@2 typescript@4.8.4

期望是 ofType 运算符缩小了可区分的联合,但它似乎没有工作。对我来说,它看起来应该。

enum ActionTypes {
  TEST_ACTION = "TEST_ACTION",
  TEST_ACTION2 = "TEST_ACTION2"
}

interface TTestAction extends Action<ActionTypes.TEST_ACTION> {
  payload: string;
}
interface TTestAction2 extends Action<ActionTypes.TEST_ACTION2> {}

type TAppActions = TTestAction | TTestAction2;

export const testEpic = (action$: Observable<TAppActions>) =>
  action$.pipe(
    ofType(ActionTypes.TEST_ACTION),
    tap((action) => {
      // expecting action to be narrowed to TTestAction type with inferred type 
      // params ofType<TAppActions, ActionTypes.TEST_ACTION, TTestAction>
      // and payload to be accessible but action is still typed as the union
    }),
    mapTo({ type: ActionTypes.TEST_ACTION2 })
  );

/**
     export function ofType<
      // All possible actions your app can dispatch
      Input extends Action,
      // The types you want to filter for
      Type extends Input['type'],
      // The resulting actions that match the above types
      Output extends Input = Extract<Input, Action<Type>>
    >(...types: [Type, ...Type[]]): OperatorFunction<Input, Output>
  
  */

codesandbox reproduction

【问题讨论】:

  • 在此处发布您的代码,不要让有兴趣帮助您点击某些外部站点的人。
  • 感谢您的帮助@possum

标签: rxjs redux-observable


【解决方案1】:

https://github.com/redux-observable/redux-observable/pull/681#issuecomment-626802177

我在原始 PR 中发现了这条评论,它添加了 ofType 运算符的类型。我已经使用'as const'将复制更新为缩小范围。将寻求改善这一点。

【讨论】:

    猜你喜欢
    • 2021-09-09
    • 1970-01-01
    • 2021-11-13
    • 2019-01-23
    • 2017-04-22
    • 2020-12-30
    • 2018-09-15
    • 2020-06-03
    • 2017-05-06
    相关资源
    最近更新 更多