20

给定从另一个模块导入的类型定义,如何重新导出它?

/**
 * @flow
 */

import type * as ExampleType from './ExampleType';

...

// What is the syntax for exporting the type?
// export { ExampleType };
4

4 回答 4

34

这个问题的最简单形式是“如何导出类型别名?” 简单的答案是“用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,该别名YourTypeAliasYourModule.js.

于 2015-09-22T21:49:16.207 回答
16

以下效果很好

export type { Type } from './types';
于 2017-05-25T06:10:26.690 回答
9

接受的答案是旧的,并且对我提出警告。鉴于观看次数,这是与 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;
  }

资源

于 2018-02-24T02:28:14.893 回答
1

我刚刚发现需要单线来为 ES6 默认类执行此操作,基于@locropulenton 的答案。假设你有

// @flow

export default class Restaurants {}

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

export type {default as Restaurants} from './Restaurants';
于 2018-05-26T04:03:04.123 回答