当使用promise-middleware + thunk链接promise时,Typescript错误“属性'然后'不存在'

时间:2017-11-01 20:49:17

标签: javascript typescript redux redux-thunk redux-promise-middleware

我正在使用redux-promise-middleware和redux-thunk来实现我的承诺:

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
        });
    }
}

上面的代码有效但在编译期间也会生成警告:

  

TS2339:属性'then'在类型'{type:string;   有效载荷:Promise&lt; {}&gt ;; }“

听起来我需要将Promise<...>包含在某个类型中,因此打字稿知道then实际上是dispatcher()返回的对象的属性,但我还没有能够删除错误。

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...
});

似乎相关,但我可以指定行动类型,即MY_ACTION

1 个答案:

答案 0 :(得分:4)

正如您所看到的,in this ts playground变量a公开了与Dispatch<any>类型相同的密钥,正如您所看到的那样,如果您将鼠标置于错误上,则错误消息是与你的情况相同。为了访问promise(以及then函数),您必须访问Dispatch对象的payload

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

<强> EDIT1:

如果我们看一下typings for redux,我们可以很快找到Dispatcher接口。

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

然后通过一些重写和一些自由使用的psudocode,我们可以推断出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>;
    }
}

这是有效的打字稿,你可以在this playground中看到,但我之前没有必须这样做,所以这可能无法开箱即用。

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

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

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