javascriptecmascript-6flowtypecommonjs

Is there a point to doing 'import type' rather than 'import' with Flow?


Flow allows you to use the following syntax to import types:

// SomeClass.js
export default class SomeClass {}

// SomeFile.js
import type SomeClass from './SomeClass';

What's the benefit of using import type instead of import? Does it tell Flow more information and let it perform better static analysis?


Solution

  • For the specific case of classes, it is either example will work. The key thing is that it breaks down like this:

    A JS class produces a value, but Flowtype also interprets a class declaration as a type declaration, so it is both.

    So where is import type important?

    1. If the thing you're importing doesn't have a value, using a value import will in some cases be interpreted as an error, because most JS tooling doesn't know that Flow exists.
    1. If the thing you're importing has a JS value, but all you want is the type