javascript - 解构 Flow 对象类型?
问题描述
我正在为 Apollo 客户端生成流类型,我目前有这个:
type FetchModuleQuery = {|
// Fetch single module
module: ?{|
// ID
id: string,
// Name
name: string,
// Fetch list of assignments for module
assignments: ?Array<?{|
// Created date
createdAt: any,
// ID
id: string,
// Name
name: string
|}>
|}
|};
但是,此数据位于我的父组件<Component1 />
中,我像这样渲染它的子组件:
<Component2 assignments={this.props.module.assignments} />
这很好用;我正在做所有我需要做的检查以保持 Flow 快乐。但是,我想不出最干净的方式来输入 my <Component2 />
; 理想情况下,我想使用这个现有的FetchModuleQuery
对象类型,而不是创建任何新的东西。
有任何想法吗?
解决方案
您可以使用类似于导入和导出真实模块的语法来导入和导出流类型。对于您的情况,您可以提取assignments
为另一种类型并执行以下操作:
组件1.js:
export type FetchModuleQueryAssignments = ?Array<?{|
// Created date
createdAt: any,
// ID
id: string,
// Name
name: string
|}>;
export type FetchModuleQuery = {|
...,
assignments: FetchModuleQueryAssignments,
|};
组件2.js:
import type { FetchModuleQueryAssignments } from './Component1';
推荐阅读
- python-3.x - 如何连接到本地 Postgresql 服务器并从 Python 桌面应用程序创建数据库
- python - 在 while 循环中,speech_recognition sr.listen 急剧减慢
- javascript - /** 在 js 中相对于文件而不是评论意味着什么
- go - golang 模块名称更改导致本地测试失败
- sharepoint - 使用 SharePoint 或 WOPI 在浏览器中渲染/编辑 MS Office 文档
- javascript - 具有高图表的多个集群标题
- html - 使内部 div 填充列高度 - 适用于 Chrome,但不适用于 Safari/Firefox
- json - 将 Koa 请求正文转换为字符串时转义特殊字符
- python - 从断言中给出的字符串中提取电子邮件地址并在列表中给出它们的函数
- jgit - 区分 JGit 中的轻量级标签和带注释的标签