Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flowtype – how to export and import types?

I've followed https://flow.org/en/docs/install/, and flow is working fine when used in single files, like this:

 // @flow 

type NumberAlias = number;

const n: NumberAlias = "123";

Flow will correctly point out that:

  5: const n: NumberAlias = "123";
                            ^^^^^ string. This type is incompatible with
  5: const n: NumberAlias = "123";
              ^^^^^^^^^^^ number

The problem arises when I try to export a type from moduleA and import that type in moduleB:

(moduleA.js)

// @flow 

export type NumberAlias = number;

(moduleB.js)

// @flow

import type { NumberAlias } from './moduleA';

const n: NumberAlias = 123;

Flow complains:

src/moduleB.js:3
  3: import type { NumberAlias } from './moduleA';
                                      ^^^^^^^^^^^ ./moduleA. Required module not found

Isn't this exactly how it's described in https://flow.org/en/docs/types/modules/?

Folder structure is:

src/
    moduleA.js
    moduleB.js
.flowconfig
package.json
like image 448
AleksG Avatar asked Jul 12 '17 07:07

AleksG


People also ask

What is import type?

import type only imports declarations to be used for type annotations and declarations. It always gets fully erased, so there's no remnant of it at runtime. Similarly, export type only provides an export that can be used for type contexts, and is also erased from TypeScript's output.

What are export types in Reactjs?

There are two types of exports: Named and Default.

What is export type in TypeScript?

TypeScript supports export = to model the traditional CommonJS and AMD workflow. The export = syntax specifies a single object that is exported from the module. This can be a class, interface, namespace, function, or enum.

What is module flow?

JavaScript projects in Flow are composed of “modules” which are just single files that encapsulate some logic. A module can “export” variables/functions/classes so that other modules can make use of them. Additionally they can “import” variables/functions/classes from other modules.


1 Answers

This worked as you'd expect when I set it up, using the two files provided sitting in a src folder under my project's root. moduleB.js:

Flow error

If I fail to put // @flow in Module A, the compiler treats NumberAlias as any, and fails to complain. I initially forgot to add it, and this threw me for a few minutes.

The only way I could find to produce the "moduleA not found" error was to change the file extension (or file name) for moduleA. For example, if the file is called moduleA.ts, ModuleA.js, or simply moduleA, the import will fail to resolve as above.


Incidentally, both

import type { NumberAlias } from './moduleA';

and

import type { NumberAlias } from './moduleA.js';

resolve just fine in my local environment. You might try the second format?


Provided these conditions are met:

  • Both files live in the same folder
  • Both have a .js (or .jsx) extension
  • The filenames match the import statements exactly
  • Both contain the // @flow comment

The code you provided works using Flow 0.57.3 with Node 8.7.0. In light of the error message, I'd suggest triple checking the filenames and extensions especially.

like image 184
Mike Patrick Avatar answered Oct 21 '22 12:10

Mike Patrick