Flow: "import type" silently ignored

Created on 1 Aug 2016  路  9Comments  路  Source: facebook/flow

import type requires the file exporting types to have have // @flow comment in the file.

So currently flow doesn't silently ignores any requires of types from files without a @flow comment. This can take a long time to debug...

So if I have two files defs.js and main.js no error is catched by flow:

# defs.js
export type UserID = number;

# main.js
// @flow
import type { UserID } from './defs';
export function myFunc(): UserID {
    return '42';
}

Even importing something that is not exported doesn't fail:

// @flow
import type { xxxxxxxxxxx } from './defs';
export function myFunc(): xxxxxxxxxxx {
    return '42';
}

When adding // @flow to the defs.js file the error shows up. I propose that either:

  • flow complains if the type file you require doesn't have the @flow pragma
  • OR (even better) not require @flow to be there

See more about this feature here https://flowtype.org/docs/modules.html#type-imports--exports

Needs docs opt-in vs. opt-out

Most helpful comment

Encountered this issue also multiple times and it's pretty hard to figure it out why the imported types just don't work. I believe a friendly error/warning would go a really long way in terms of smoothing the dev experience.

All 9 comments

Sorry the docs don't make this point explicit!

FWIW this behavior is not specific to import type but to all kinds of imports, but I agree it's particularly confusing for import type.

I totally agree. I had a problem with a type imported from a different module, where I thought I had generated js.flow files, but it turned out I hadn't. If I explicitly import types, and it doesn't "work", I expect flow to shout loudly about it. In this case, I had imported the type, and type annotated code, and wondered why circle-ci passed the flow tests... Because I hadn't properly installed flow-copy-source.

This issue just hit me again - I was trying to test a complex type configuration, and didn't understand why I couldn't get an error on an imported type, before finally realizing that I had inadvertently removed the //@flow annotation from the file exporting the type. When someone explicitly imports a type, you should definitively error if the type cannot be found.

Encountered this issue also multiple times and it's pretty hard to figure it out why the imported types just don't work. I believe a friendly error/warning would go a really long way in terms of smoothing the dev experience.

If I import a type from a library that does not exist, I don't get any errors when I try to use that type, kind of defeating the point of flow.

@rostero1 flow can catch these errors too but it鈥檚 a config you have to enable. See https://flow.org/en/docs/linting/rule-reference/#toc-untyped-type-import

What's missing here? This is available via a lint: would better documentation on the default behaviour make this better? It's tagged as opt-in vs. opt-out: does this mean there's discussion wether to keep this this opt-in by default? (I personally think it should be opt-out).

I do think we should consider making this an opt-out but might be better to move that to a separate ticket if there's interest

Was this page helpful?
0 / 5 - 0 ratings

Related issues

danvk picture danvk  路  73Comments

blax picture blax  路  83Comments

cletusw picture cletusw  路  52Comments

Gozala picture Gozala  路  54Comments

STRML picture STRML  路  48Comments