ide
ide

Reputation: 20838

How do you export a Flow type definition that is imported from another file?

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

Upvotes: 23

Views: 18581

Answers (4)

locropulenton
locropulenton

Reputation: 4853

The following works nicely

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

Upvotes: 17

James Ko
James Ko

Reputation: 34629

I just found a need one-liner to do this for ES6 default classes, building on @locropulenton's answer. Suppose you have

// @flow

export default class Restaurants {}

in a Restaurants.js file. To export it from an index.js file in the same directory, do this:

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

Upvotes: 2

Gunther
Gunther

Reputation: 581

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+ .

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

Upvotes: 9

Gabe Levi
Gabe Levi

Reputation: 1937

The simplest form of this question is "how do I export a type alias?" and the simple answer is "with 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

type MyTypeAlias = number;

You are explicitly creating the type alias MyTypeAlias which aliases number. And when you write

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

You are implicitly creating the type alias YourTypeAlias which aliases the YourTypeAlias export of YourModule.js.

Upvotes: 38

Related Questions