如何导出从其他文件导入的Flow类型定义?

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.

  • 根据这篇文章,`export type {ExampleType}`不是受支持的语法。https://flowtype.org/blog/2015/02/18/Import-Types.html帖子中的信息是否过时?该语法在哪里记录?也没有在https://flowtype.org/docs/modules.html中进行记录。 (2认同)
  • 注意:“export type * from './foo'”似乎不起作用。 (2认同)

loc*_*ton 11

这很有效

__CODE__

编辑:react-native-tab-view中获取此语法


Gun*_*her 7

接受的答案是陈旧的,并在我的结尾发出警告.鉴于视图的数量,这里是一个与流量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)

资源