给定从另一个模块导入的类型定义,如何重新导出它?
/**
* @flow
*/
import type * as ExampleType from './ExampleType';
...
// What is the syntax for exporting the type?
// export { ExampleType };
给定从另一个模块导入的类型定义,如何重新导出它?
/**
* @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;
您正在显式创建 aliases 的类型MyTypeAlias
别名number
。当你写
import type { YourTypeAlias } from './YourModule';
您正在隐式创建类型别名YourTypeAlias
,该别名YourTypeAlias
为YourModule.js
.
以下效果很好
export type { Type } from './types';
接受的答案是旧的,并且对我提出警告。鉴于观看次数,这是与 flow 0.10+ 兼容的更新答案。
MyTypes.js:
export type UserID = number;
export type User = {
id: UserID,
firstName: string,
lastName: string
};
用户.js:
import type {UserID, User} from "MyTypes";
function getUserID(user: User): UserID {
return user.id;
}
我刚刚发现需要单线来为 ES6 默认类执行此操作,基于@locropulenton 的答案。假设你有
// @flow
export default class Restaurants {}
在一个Restaurants.js
文件中。要从index.js
同一目录中的文件导出它,请执行以下操作:
export type {default as Restaurants} from './Restaurants';