6

I'm using redux-promise-middleware with redux-thunk in order to chain my promises:

import { Dispatch } from 'redux';

class Actions {
    private static _dispatcher: Dispatch<any>;
    public static get dispatcher(): Dispatch<any> {
        return Actions._dispatcher;
    }
    public static test() {
        this.dispatcher({
            type: 'MY_ACTION',
            payload: new Promise(resolve => resolve('hi'));
        }).then(result => {
            console.log(result); // this works
        });
    }
}

The code above works but also generates a warning during compile time:

TS2339: Property 'then' does not exist on type '{ type: string; payload: Promise<{}>; }'

It sounds like I need to include Promise<...> somewhere as a type so typescript knows that then is in fact a property on the object that's returned by dispatcher() but I haven't been able to remove the error.

https://github.com/gaearon/redux-thunk/issues/103

import { Dispatch } from 'redux';
import { ThunkAction } from 'redux-thunk';
import { getStore, IState } from './my_store';

let store = getStore();

// Create myThunkAction function with a type of ThunkAction<R, S, E>
let myThunkAction: ThunkAction<Promise<string>, IState, null> =
    (dispatch: Dispatch<IState>, getState: () => IState) => {
        return new Promise<string>((resolve, reject) => {

            // do async stuff with getState() and dispatch(), then...
            resolve('done!');

        });
    }

store.dispatch(myThunkAction)
.then(() => {
    // do stuff after the thunk has finished...
});

Seems related but where I can specify the action type i.e. MY_ACTION?

4

1 回答 1

4

正如您在这个 ts 操场上看到的那样,该变量a公开了与 的类型相同的键Dispatch<any>,并且您可以看到,如果您将鼠标悬停在错误上,则错误消息与您的情况相同。为了访问 promise(以及then函数),您必须访问payloadDispatch 对象。

this.dispatcher({ ... }).payload.then(....);

编辑1:

如果我们看一下redux的类型,我们可以很快找到 Dispatcher 接口。

export interface Dispatch<S> {
    <A extends Action>(action: A): A;
}
export interface Action {
  type: any;
} 

然后通过一些重写和一些伪代码的自由使用,我们可以推断出 Dispatch 的类型是一个函数,它接受一个参数,巫婆是一个对象,并返回一个与参数相同类型的对象。

type Dispatch: (action: {type: any, ...}) => {type: any, ...}

输入对象和输出对象都属于以下类型:

interface {
    type: any,
    [key: string]: value
}

总之,要么 1)你没有使用 redux 的官方类型,2)redux 的官方类型是错误的,或者 3)你在你的生活环境中遗漏了一些东西,实际上代码不起作用。

编辑2:

我没有尝试过这段代码,所以我不知道它是否真的能解决你的问题。但是您可以尝试重新定义 Dispatch 接口。

declare module 'redux' {
    export interface Action {
       type: any;
    }
    export interface Dispatch<S> {
        <A extends Action>(action: A): Promise<S>;
    }
}

正如你在这个操场上看到的那样,它是有效的打字稿,但我以前没有自己做过,所以这可能无法开箱即用。

如果这不起作用,您可以尝试定义与模块同名的命名空间。

namespace redux {
    export interface Action {
       type: any;
    }
    export interface Dispatch<S> {
        <A extends Action>(action: A): Promise<S>;
    }
}

我之前还没有尝试过,所以我不能保证它会起作用。

于 2017-11-01T20:59:03.950 回答