Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Wmstone opened this issue Nov 5, 2019 · 11 comments

Comments

@Wmstone
Copy link

Wmstone commented Nov 5, 2019

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
@sorrycc
Copy link
Member

sorrycc commented Nov 6, 2019

#2219 能解决你的问题吗?

@Wmstone
Copy link
Author

Wmstone commented Nov 7, 2019

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

@Wmstone
Copy link
Author

Wmstone commented Nov 7, 2019

而且我每次升级到2.6.0@-beta.14 都会在@connect出现Unable to resolve signature of class decorator when called as an expression
f51a6188ec0ae8642bc06d414afd051

@Moicen
Copy link

Moicen commented Nov 7, 2019

#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>
    }
}

@xc1427
Copy link
Contributor

xc1427 commented Nov 7, 2019

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

@Wmstone
Copy link
Author

Wmstone commented Nov 7, 2019

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

@Wmstone
Copy link
Author

Wmstone commented Nov 7, 2019

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

@Wmstone Wmstone closed this as completed Nov 11, 2019
@Liu-Ya
Copy link

Liu-Ya commented Nov 26, 2019

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

@hblee12294
Copy link

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

强行 as any吧

@juzhiyuan
Copy link

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

image

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

@awefeng
Copy link

awefeng commented Mar 31, 2020

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

image

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants