Search code examples
flowtypeflow-typed

Flow - type a variable with global type property type


I have a global type in flow-typed directory like this:

import type { UI, User, Projects, Profile } from 'data/redux/redux.flow';

declare type State = {
  +ui: UI,
  +user: User,
  +projects: Projects,
  +profile: Profile,
}

each property has its own type imported from data/redux/redux.flow.js file.

I wonder is it possible now to do something like below somewhere in my app:

type Props = {
  profile: State.profile, //or maybe State$Profile e.t.c
}

so basically I dont have to make Profile type global also.

?


Solution

  • Yes. You can use $PropertyType:

    type Props = {
      profile: $PropertyType<State, 'profile'>
    }
    

    Flow Utility Types - Property Type

    Possible Bug with PropertyType