【问题标题】:React, redux, redux observable: Promise resolve typeReact、redux、redux observable:Promise 解析类型
【发布时间】:2018-03-13 01:37:33
【问题描述】:

我正在尝试定义一个承诺的类型。 以下是部分代码或者如果你想在github上查找它:https://github.com/Electra-project/Electra-Desktop/blob/master/src/app/header/epics.ts

export function getStakingInfo(action$: ActionsObservable<HeaderActions>, store: any): any {
  return action$.ofType(ActionNames.GET_STAKING_INFO)
    .map(() => store.getState().electra.electraJs) // get electraJs object from the store
    .filter((electraJs: any) => electraJs) // check if electraJs exists
    .map(async (electraJs: any) => electraJs.wallet.getStakingInfo())
    .switchMap(async (promise: Promise<WalletStakingInfo>) => new Promise((resolve) => {
      promise
        .then((data: WalletStakingInfo) => {
          resolve({
            payload: {
              ...data
            },
            type: ActionNames.GET_STAKING_INFO_SUCCESS
          })
        })
        .catch((err: any) => {
          resolve({
            type: ActionNames.GET_STAKING_INFO_FAIL
          })
        })
    }))
    .catch((err: any) =>
      Observable.of({
        type: ActionNames.GET_STAKING_INFO_FAIL
      }))
}

我收到一条错误消息,指出上述代码部分中的resolve 不是类型定义的new Promise((resolve) =&gt; {。但是我不确定解决的类型。

任何人都可以指导我这里应该使用什么类型的解决方案?

【问题讨论】:

    标签: reactjs typescript redux redux-observable


    【解决方案1】:

    您可以像这样定义自己的类型,例如:

    type Resolve = (action: { payload?: WalletStakingInfo; type: ActionNames; }) => void;
    
    export function getStakingInfo(action$: ActionsObservable<HeaderActions>, store: any): any {
        return action$.ofType(ActionNames.GET_STAKING_INFO)
            .map(() => store.getState().electra.electraJs) // get electraJs object from the store
            .filter((electraJs: any) => electraJs) // check if electraJs exists
            .map(async (electraJs: any) => electraJs.wallet.getStakingInfo())
            .switchMap(async (promise: Promise<WalletStakingInfo>) => new Promise((resolve: Resolve) => {
                promise
                    .then((data: WalletStakingInfo) => {
                        resolve({
                            payload: {
                                ...data
                            },
                            type: ActionNames.GET_STAKING_INFO_SUCCESS
                        })
                    })
                    .catch((err: any) => {
                        resolve({
                            type: ActionNames.GET_STAKING_INFO_FAIL
                        })
                    })
            }))
            .catch((err: any) =>
                Observable.of({
                    type: ActionNames.GET_STAKING_INFO_FAIL
                }))
    }
    

    【讨论】:

    • 你能解释一下Resolve是什么类型的吗?我还没有看到有 () 的类型。
    • Resolve 在我上面的例子中只是一个函数类型。没有论据可能会更清楚,例如type F = () =&gt; number; 描述了一种不带参数并返回数字的函数类型。然后可以这样使用:const f: F = () =&gt; 0;.
    猜你喜欢
    • 2018-08-28
    • 2020-11-05
    • 2020-11-23
    • 1970-01-01
    • 1970-01-01
    • 2022-07-31
    • 2021-10-27
    • 1970-01-01
    • 2021-11-03
    相关资源
    最近更新 更多