Typescript: Cannot export { ... } interface when --isolatedModules is passed

Created on 1 Aug 2019  路  10Comments  路  Source: microsoft/TypeScript


TypeScript Version: 3.5.3


Search Terms: isolatedModules re-export

Code

interface MyInterface {}
export {
    MyInterface
}

Expected behavior:

This should compile regardless of whether --isolatedModules is passed.

OR the error message could indicate that this is not an accepted way to export an interface in an isolated module. (https://github.com/Microsoft/TypeScript/issues/28481)

Actual behavior:

This fails to compile with the error message:

Cannot re-export a type when the '--isolatedModules' flag is provided.ts(1205)

when --isolatedModules is passed.

Related Issues: https://github.com/Microsoft/TypeScript/issues/28481

Bug Error Messages Fixed good first issue help wanted

Most helpful comment

But how then we should export interfaces?

All 10 comments

The error message is slightly off; it should say export not re-export if the symbol originates in a declaration in the same file

As in Cannot export a type when the '--isolatedModules' flag is provided.ts(1205)?

I agree, that would make the error message more clear, but is it strictly true? You can safely export the interface inline regardless of the --isolatedModules flag, e.g.:

export interface MyInterface {}

which using export instead of re-export implies is not possible.

Error: A type cannot be exported in an 'export {...}' list under the '--isolatedModules' flag.

Related error message on the original declaration: Consider adding an 'export' keyword here instead..

@rickbutton , Ryan Cavanaugh pointed out that babel-plugin-typescript chokes on export { Foo } because it can't tell if the type originated in the current module or not.
So we do want an error message here, but likely a different one, like the one Daniel suggested ^.

makes sense to me! I will gladly submit a PR to update the error message, when the new error message is chosen.

But how then we should export interfaces?

TypeScript 3.8+ will provide export type which is specifically for cases in isolatedModules, Babel, and transpileModule where you need to use an export { ... } statement with a type declaration.

So the original code could be written as

interface MyInterface {}

export type {
    MyInterface
}

The current error message was changed to

Re-exporting a type when the '--isolatedModules' flag is provided requires using 'export type'.

so I think this was fixed in #35200.

fwiw, I love this solution

yep, fixed!

Was this page helpful?
0 / 5 - 0 ratings