javascript 如何导出从另一个文件导入的 Flow 类型定义?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32602957/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you export a Flow type definition that is imported from another file?
提问by ide
Given a type definition that is imported from another module, how do you re-export it?
给定从另一个模块导入的类型定义,您如何重新导出它?
/**
* @flow
*/
import type * as ExampleType from './ExampleType';
...
// What is the syntax for exporting the type?
// export { ExampleType };
回答by Gabe Levi
The simplest form of this question is "how do I export a type alias?" and the simple answer is "with export type
!"
这个问题最简单的形式是“如何导出类型别名?” 简单的答案是“用export type
!”
For your example, you can write
对于你的例子,你可以写
/**
* @flow
*/
import type * as ExampleType from './ExampleType';
export type { ExampleType };
You may ask "why is ExampleType
a type alias?" Well, when you write
你可能会问“为什么是ExampleType
类型别名?” 那么,当你写
type MyTypeAlias = number;
You are explicitly creating the type alias MyTypeAlias
which aliases number
. And when you write
您正在显式创建MyTypeAlias
aliases的类型别名number
。当你写
import type { YourTypeAlias } from './YourModule';
You are implicitly creating the type alias YourTypeAlias
which aliases the YourTypeAlias
export of YourModule.js
.
您正在隐式创建类型别名YourTypeAlias
,该别名YourTypeAlias
为YourModule.js
.
回答by locropulenton
The following works nicely
以下效果很好
export type { Type } from './types';
回答by Gunther
The accepted answer is old and throwing warnings on my end. Given the amount of views, here's an updated answer that's compatible with flow 0.10+ .
接受的答案是旧的,并对我发出警告。鉴于观看次数,这里有一个与 flow 0.10+ 兼容的更新答案。
MyTypes.js:
MyTypes.js:
export type UserID = number;
export type User = {
id: UserID,
firstName: string,
lastName: string
};
User.js:
用户.js:
import type {UserID, User} from "MyTypes";
function getUserID(user: User): UserID {
return user.id;
}
回答by James Ko
I just found a need one-liner to do this for ES6 default classes, building on @locropulenton's answer. Suppose you have
我刚刚发现需要单行代码来为 ES6 默认类执行此操作,基于@locropulenton 的回答。假设你有
// @flow
export default class Restaurants {}
in a Restaurants.js
file. To export it from an index.js
file in the same directory, do this:
在一个Restaurants.js
文件中。要从index.js
同一目录中的文件导出它,请执行以下操作:
export type {default as Restaurants} from './Restaurants';