Ilja
Ilja

Reputation: 46479

Flow, how to inherit class function typings?

I've made a simple router class that when simplified looks like this

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

Unfortunately RouterState.mutationPush does not return type because it is not a static function, which is expected due to usage of extends Container and how this is used in my React app. I am trying to figure out a way where I can export type of each individual function in class like this.

Edit specific use case for this would be

type Props = {
  push: RouterState.mutationPush
}

That errors with

[flow] Cannot get RouterState.mutationPush because property mutationPush is missing in statics of RouterState [1]. (References: [1]) [Flow] mutationPush: any

Edit2: I tried adding type exports in class so

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

/* ... */

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

but wherever I use this function, flow helper says this mutationPush: mutationPush instead of mutationPush: (pathname: string, data?: Object) ...

Upvotes: 2

Views: 335

Answers (1)

noppa
noppa

Reputation: 4066

You can use $PropertyType utility type to get the type of a property or method.

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

Upvotes: 1

Related Questions