Module Types

 

It is often useful to share types in between modules (files). Flow supports importing and exporting types.

exports.js

1
2
export type MyObject = { /* ... */ };
export interface MyInterface { /* ... */ };

imports.js

1
import type {MyObject, MyInterface} from './exports';

Importing and exporting types

In Flow, you can export type aliases and interfaces from one file and import them in another.

exports.js

1
2
export type MyType = { /* ... */ };
export interface MyInterface { /* ... */ }

imports.js

1
2
3
import type TypeA from './exports';
import type {TypeA} from './exports';
import type {TypeA, TypeB} from './exports';

Importing and exporting values

Flow also supports importing the type of values exported by other modules using typeof.

exports.js

1
2
3
4
export default const myNumber = 42;
export class MyClass {
  // ...
}

imports.js

1
2
import typeof myNumber from './exports';
import typeof {MyClass} from './exports';

Just like other type imports, this code will be stripped away by a compiler and will not add a dependency on the other module.