Flow: Type checking not working with imported types

Created on 22 Sep 2016  路  6Comments  路  Source: facebook/flow

I'm trying to import a type from a file, and it doesn't seem to properly detect the type.

The type looks like this...

export type Resource = {
  endpoint: string,
  fetch(): void
}

And I'm importing it...

/* @flow */
import { Resource } from "./types";

const getSomething = (resource: Resource) => {
  resource.foo(); // This SHOULD blow up, right?
}

I'd expect for flow to tell me, foo is not a method or something, which it DOES do when I just put the type inline instead...

type Resource = {
  endpoint: string,
  fetch(): void
}

const getSomething = (resource: Resource) => {
  resource.foo(); // Now it actually works and says foo doesn't exist
}

vs

Most helpful comment

Yup, it was not having flow in the types/index.js file. Thanks for the heads up!

All 6 comments

It looks like you don't have an @flow comment at the top of the file exporting the type, so you'll need to add that.

FWIW: If we can manage to agree on https://github.com/facebook/flow/pull/2392 and land it, then that will help protect against this kind of scenario in the future.

Closing this out, but please re-open if adding @flow doesn't solve your issue.

PS: It聽also looks like you're also using import rather than import type. You'll need to use the latter to import a type alias export from another module:

import type { Resource } from "./types";

Ahh, I definitely have /* @flow */ in there as you can see above, but didn't try the import type ... will give that a shot. Thanks!

Oh, I guess I also didn't have flow in the type file, maybe that was it too. Will post after I try!

Yup, it was not having flow in the types/index.js file. Thanks for the heads up!

Was this page helpful?
0 / 5 - 0 ratings