Flow,如何继承类函数类型?

时间:2018-05-20 13:55:36

标签: javascript reactjs flowtype

我做了一个简单的路由器类,当简化时看起来像这样

// @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
}

错误

  

[flow]无法获取RouterState.mutationPush,因为mutationPush [1]的静态中缺少属性RouterState。   (参考文献:[1])       [流]       mutationPush:任何

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

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

/* ... */

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

但无论我在哪里使用此功能,流程助手都说mutationPush: mutationPush代替mutationPush: (pathname: string, data?: Object) ...

1 个答案:

答案 0 :(得分:1)

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

# Save the output of the `dir` command
var = subprocess.run( <dir_command>, stdout=subprocess.PIPE, shell=True)

# Iterate through the lines saved in `var`, and write them to `file`, line by line
with open('path/to/file', 'a') as file:
    for line in var:
         file.write(line)

file.close()