如何导出从另一个文件导入的流类型定义
How do you export a Flow type definition that is imported from another file?
给定从另一个模块导入的类型定义,如何重新导出它?
/**
* @flow
*/
import type * as ExampleType from './ExampleType';
...
// What is the syntax for exporting the type?
// export { ExampleType };
这个问题最简单的形式是"如何导出类型别名",简单的答案是"使用export type
!"
例如,你可以写
/**
* @flow
*/
import type * as ExampleType from './ExampleType';
export type { ExampleType };
你可能会问"为什么ExampleType
是一个类型别名?"当你写
type MyTypeAlias = number;
您正在显式地创建别名MyTypeAlias
,别名number
。当你写
import type { YourTypeAlias } from './YourModule';
您正在隐式地创建类型别名YourTypeAlias
,它别名YourModule.js
的YourTypeAlias
导出。
下面的代码运行得很好
export type { Type } from './types';
接受的答案是旧的,并在我这端抛出警告。考虑到视图的数量,这里有一个与流0.10+兼容的更新答案。
MyTypes.js:
export type UserID = number;
export type User = {
id: UserID,
firstName: string,
lastName: string
};
User.js:
import type {UserID, User} from "MyTypes";
function getUserID(user: User): UserID {
return user.id;
}
源我在@locropulenton的答案上找到了一个需要的一行代码来为ES6默认类做这个。假设你有
// @flow
export default class Restaurants {}
在Restaurants.js
文件中。要从同一目录下的index.js
文件导出它,请执行以下操作:
export type {default as Restaurants} from './Restaurants';
相关文章:
- Angular 2中的@Angular包缺少TypeScript类型定义
- 如何存根Typescript接口/类型定义
- 在搜索类型定义时,何时使用环境光
- 在 JavaScript 中为所有事件类型定义回调函数
- 为命名空间上公开的方法创建类型定义
- 如何在打字稿中声明 NPM 的模块公共类型定义
- 何时有时将对象类型定义为将实例绑定方法镜像为构造函数上的静态实用程序函数很有用
- TypeScript:类型定义(接口)的引用子类型
- 构造函数的纯脚本 FFI 类型定义
- ternJS - 生成 JSON 类型定义文件
- Jquery将响应类型定义为json
- 使用knockout映射JSON无法填充类型定义的对象属性
- (TypeScript)正在为Parse SDK(浏览器和节点)创建类型定义
- 找不到'core-js'的类型定义文件
- 属性的类型定义
- 如何导出从另一个文件导入的流类型定义
- 为什么visual studio不能识别typescript类型定义?
- 如何在许多Visual Studio(2013)项目中处理TypeScript类型定义?
- 如何为不可变js类型写一个流类型定义
- 如何使用未由库公开但在类型中实现的类型定义