【问题标题】:Flow, how to inherit class function typings?Flow,如何继承类函数类型?
【发布时间】:2018-05-20 13:55:35
【问题描述】:

我制作了一个简单的路由器类,简化后看起来像这样

// @flow
import { Container } from 'unstated'

type State = {
  history: Object[],
  pathname: string,
  data: Object | null
}

class RouterState extends Container<State> {
  state = {
    history: [{ pathname: '/onboarding/sign-in', data: null }],
    pathname: '/onboarding/sign-in',
    data: null
  }


  mutationPush = (pathname: string, data?: Object | null = null) => {
    if (pathname !== this.state.pathname) {
      const history = [...this.state.history, { pathname, data }]
      this.setState({
        history,
        pathname,
        data
      })
    }
  }
}
export default RouterState

不幸的是,RouterState.mutationPush 没有返回类型,因为它不是静态函数,这是由于 extends Container 的使用以及在我的 React 应用程序中的使用方式而导致的。我正在尝试找出一种方法,可以像这样导出类中每个单独函数的类型。

编辑具体用例是

type Props = {
  push: RouterState.mutationPush
}

那个错误

[流程] 无法获取 RouterState.mutationPush,因为 RouterState [1] 的静态数据中缺少属性 mutationPush。 (参考文献:[1]) [流动] 突变推送:任意

Edit2:我尝试在类中添加类型导出

export type mutationPush: (pathname: string, data?: ?Object) => void

/* ... */

mutationPush: mutationPush = (pathname, data) => ...

但是无论我在哪里使用这个功能,流程助手都会说这个mutationPush: mutationPush而不是mutationPush: (pathname: string, data?: Object) ...

【问题讨论】:

    标签: javascript reactjs flowtype


    【解决方案1】:

    您可以使用$PropertyType 实用程序类型来获取属性或方法的类型。

    type Props = {
      push: $PropertyType<RouterState, 'mutationPush'>
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多