Dva: typescript中使用.then() 提示Property 'then' does not exits on type '{type:string}'

Created on 5 Nov 2019  ·  11Comments  ·  Source: dvajs/dva

What happens?

typescript中使用.then() 提示Property 'then' does not exits on type '{type:string}'

Context

  • Dva Version: 2.6.0-beta.17
  • Node Version:v12.10.0

All 11 comments

2219 能解决你的问题吗?

@sorrycc ,您好,我看commit中 "version": "2.6.0-beta.17", 是需要把dva升级到[email protected]

而且我每次升级到2.6.[email protected] 都会在@connect出现Unable to resolve signature of class decorator when called as an expression
f51a6188ec0ae8642bc06d414afd051

2219 能解决你的问题吗?

我自己按照这个PR里面的方式定义了一下,还是无法实现dispatch(...).then(...),但是可以使用await 读取返回值。如下:

export interface IDispatch<A extends Action = AnyAction> {
  <T extends A>(action: T): Promise<any> | T;
}
export interface IProps {
  dispatch: IDispatch<AnyAction>;
  [key: string]: any;
  children?: React.ReactNode;
}

使用:

class Index extends React.Component<IProps>{

    click = () => {
        const { success, ...rest } = await this.props.dispatch({type: 'global/someAction'});
        // some handle ...
    }
    render(){
        return <div onClick={this.click}>test</div>
    }
}

不要使用 decorator 的语法,使用 export default connect(mapState, mapDispatch)(YourComponent); TS 对于 decocrator 的限制比较多。

@xc1427 是的,但是dva,2.4.1版本还是可以用的

@Moicen 我也重新定义了一下,也是不可以,我试一下你的方法,谢谢

@sorrycc 2.6.0-beta.19 还是存在这个问题,请问如何处理?
image

@sorrycc 2.6.0-beta.19 还是存在这个问题,请问如何处理?
image

强行 as any吧

{type: string} 是从哪出来的。。

image

dispatch({
      type: 'recommend/fetchDataSource',
    }).then(() => {
      // XXXX
    })

{type: string} 是从哪出来的。。

image

dispatch({
      type: 'recommend/fetchDataSource',
    }).then(() => {
      // XXXX
    })

{type: string} 是你dispatch里面的参数类型,如果你的参数是action,dispatch返回的就是action就不能使用.then,如果dispatch的参数是effect,返回的是promise,则可以调用then

Was this page helpful?
0 / 5 - 0 ratings