如何导出从另一个文件导入的流类型定义?

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 };

这个问题的最简单形式是 "how do I export a type alias?",简单答案是 "with export type!"

对于你的例子,你可以这样写

/**
 * @flow
 */

import type * as ExampleType from './ExampleType';
export type { ExampleType };

你可能会问"why is ExampleType a type alias?"嗯,写的时候

type MyTypeAlias = number;

您正在明确创建别名 MyTypeAlias 的类型别名 number。当你写

import type { YourTypeAlias } from './YourModule';

您正在隐式创建类型别名 YourTypeAlias,它为 YourModule.jsYourTypeAlias 导出设置别名。

下面的效果很好

export type { Type } from './types';

接受的答案是旧的,并向我发出警告。考虑到观看次数,这里有一个与 flow 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;
  }

source

我刚刚发现需要一行代码来为 ES6 默认值 类 执行此操作,建立在@locropulenton 的回答之上。假设你有

// @flow

export default class Restaurants {}

Restaurants.js 文件中。要从同一目录中的 index.js 文件导出它,请执行以下操作:

export type {default as Restaurants} from './Restaurants';