Flow: Cannot re-export imported type

Created on 6 Feb 2016  路  9Comments  路  Source: facebook/flow

This syntax doesn't work.

export type {ImportedType} from './file'

(note that it does work with re-exporting functions, classes etc, Flow understands that and imports/exports correctly, just not types)

feature request

Most helpful comment

The export type { ImportedType } from './file' syntax seems to work fine for me. Presumably this has been added since this issue was opened?

All 9 comments

You can import and then re-export, but unfortunately it seems you have to change the name:

import type {MyType} from './types';
export type MyTypeToo = MyType;

...which is a bit annoying if your external clients have to use one type name, whilst inside the module you're using the other.

You can change the name in import line so that you can re-export it with the original name:

import type {MyType as _MyType} from './types';
export type MyType = _MyType;

Oh I had tried that, and it failed on the client's import. But I think that must have been to do with Flow caching something over the node_modules boundary - because when I restart, this approach now works. Thanks for the prompt.

Still a bit hacky ;)

Would it be possible for Flow to support export type MyType in the future? This would be a huge help for us.

The export type { ImportedType } from './file' syntax seems to work fine for me. Presumably this has been added since this issue was opened?

Stumbled on this today, none of the above solution works for me. Flow doesn't complain about missing modules, but it's not catching any errors either.

Importing directly from the right file works though.

Also tried this :

definition.js

import type {MyType as _MyType} from './types';
export type MyType = _MyType;

Then if I import type { SomeTypeThatDoesntEvenExist } from 'definition', Flow doesn't complain.

What version of flow are you using? This works for me on 0.33.0:

definition.js:

// @flow
export type { MyType } from './types'

test.js:

// @flow
import type { MyType } from './definition'
const foo: MyType = 'a'

Flow output:

common/__tests__/test.js:3
  3: const foo: MyType = 'a'
                         ^^^ string. This type is incompatible with
  3: const foo: MyType = 'a'
                ^^^^^^ number

Found 1 error

And your other example works too.

test.js:

// @flow
import type { MyType, SomeTypeThatDoesntExist } from './definition'
const foo: MyType = 2

Flow output:

common/__tests__/test.js:2
  2: import type { MyType, SomeTypeThatDoesntExist } from './definition'
                           ^^^^^^^^^^^^^^^^^^^^^^^ Named import from module `./definition`. This module has no named export called `SomeTypeThatDoesntExist`.


Found 1 error

Did you maybe forget a // @flow annotation at the top of your test files? That has caught me out before.

Well I just tried again, and it actually works, my bad.

Did you maybe forget a // @flow annotation at the top of your test files? That has caught me out before.

Yeah probably, I think I forgot the annotation on the definition file, and it obviously won't work without it.
Thanks @jamesisaac, and sorry for the noise :p

It seems this issue should be closed since, as @jamesisaac mentioned, the following works fine:

export type { ImportedType } from './file';
Was this page helpful?
0 / 5 - 0 ratings