19

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 };
ide
  • 17,118
  • 4
  • 56
  • 101
  • Instead of writing `import type * as ExampleType from './ExampleType';` you can write `import type ExampleType from './ExampleType';` – Gabe Levi Sep 22 '15 at 21:49

4 Answers4

33

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.

Gabe Levi
  • 1,857
  • 13
  • 15
  • but how do you easily re-export all types in one line? – faceyspacey.com Feb 11 '17 at 02:49
  • 2
    According to this post, `export type {ExampleType}` is not a supported syntax. https://flowtype.org/blog/2015/02/18/Import-Types.html Is the information in the post out of date? Where is this syntax documented? It is not documented in https://flowtype.org/docs/modules.html either. – Gajus Mar 24 '17 at 11:37
  • FYI, I have filled a related issue with Babel https://github.com/babel/babel/issues/5538 regarding the `babel-plugin-transform-flow-comments`. – Gajus Mar 24 '17 at 11:51
  • For the record: There is an announcement of that syntax on the bottom of the page @Gajus linked to. I tried (Flow 0.46) and it works, that blog post is very old. – Mörre May 27 '17 at 13:29
  • 1
    NOTE: `export type * from './foo'` doesn't seem to work. – vaughan Mar 10 '18 at 20:18
16

The following works nicely

export type { Type } from './types';
locropulenton
  • 3,782
  • 1
  • 29
  • 53
8

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

Gunther
  • 442
  • 5
  • 10
1

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';
James Ko
  • 25,479
  • 23
  • 92
  • 196