ide*_*ide 17 javascript flowtype
给定从另一个模块导入的类型定义,如何重新导出它?
/**
* @flow
*/
import type * as ExampleType from './ExampleType';
...
// What is the syntax for exporting the type?
// export { ExampleType };
Run Code Online (Sandbox Code Playgroud)
Gab*_*evi 29
这个问题的最简单形式是"如何导出类型别名?" 简单的答案就是"带export type!"
举个例子,你可以写
/**
* @flow
*/
import type * as ExampleType from './ExampleType';
export type { ExampleType };
Run Code Online (Sandbox Code Playgroud)
您可能会问"为什么是ExampleType类型别名?" 好吧,当你写作
type MyTypeAlias = number;
Run Code Online (Sandbox Code Playgroud)
您正在显式创建别名的MyTypeAlias别名number.当你写作
import type { YourTypeAlias } from './YourModule';
Run Code Online (Sandbox Code Playgroud)
您隐式创建了YourTypeAlias别名YourTypeAlias导出的类型别名YourModule.js.
接受的答案是陈旧的,并在我的结尾发出警告.鉴于视图的数量,这里是一个与流量0.10+兼容的更新答案.
MyTypes.js:
export type UserID = number;
export type User = {
id: UserID,
firstName: string,
lastName: string
};
Run Code Online (Sandbox Code Playgroud)
user.js的:
import type {UserID, User} from "MyTypes";
function getUserID(user: User): UserID {
return user.id;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13728 次 |
| 最近记录: |