Flow - 键入一个全局类型为 属性 类型的变量

Flow - type a variable with global type property type

我在 flow-typed 目录中有一个全局类型,如下所示:

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

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

每个 属性 都有自己的类型,从 data/redux/redux.flow.js 文件中导入。

我想知道现在是否可以在我的应用中的某处执行如下操作:

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

所以基本上我也不必将 Profile 类型设为全局。

?

是的。您可以使用 $PropertyType:

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

Flow Utility Types - Property Type

Possible Bug with PropertyType