18

In a file where I export all the classes of my package on lines like the following:

export {default as BoundList, IBoundListOption, TBoundListFilterFn} from './list/BoundList';

errors of the form are generated:

TS1205: Cannot re-export a type when the '--isolatedModules' flag is provided.

How do I export classes now?

This problem occurred in CRA2.1. There was forced to isolatedModules=true. I'm making a component library on CRA2.1

Justin Grant
  • 44,807
  • 15
  • 124
  • 208
Khusamov Sukhrob
  • 681
  • 1
  • 8
  • 22
  • https://github.com/babel/babel-loader/issues/603 Looks like there's some discussion here that includes workarounds. – CollinD Dec 04 '18 at 19:06

4 Answers4

13

CRA v3.4.1 facilitates type re-exports under --isolatedModules. The contained @babel/preset-typescript in version v7.9.0+ (see corresponding Babel release and announcement) and TypeScript support TS 3.8 type-only imports and exports. You can now write:

export type { MyListType } from "./list/BoundList"

// or
import type { MyListType } from "./list/BoundList"
export type { MyListType }
// note additional "type" keyword

Take a look at this answer for more info on the import/export syntax.

ford04
  • 66,267
  • 20
  • 199
  • 171
10

github.com/babel/babel-loader/issues/603 (thanks to @CollinD for the link) includes a workaround for how to re-export imported types. This comment on that issue has the best explanation of a workaround:

You can still do are-export if it's clear that you're exporting a type:

import { T as a_T } from "./a";
export type T = a_T;

You can also do export * from "./a";.

If I'm reading that GitHub issue correctly, only TS types can be re-exported, but values (e.g. classes) can't be re-exported. So if TS knows that you're importing a type (not a class) then you can re-export it.

Here's another example that's simpler:

import { T } from "./a";
export type T = T;
Community
  • 1
  • 1
Justin Grant
  • 44,807
  • 15
  • 124
  • 208
0

Yes - node_modules/fork-ts-checker-webpack-plugin/package.json is "version": "0.2.2".

It seems the change was made in Microsoft/TypeScript#15538 , so if you test with 2.3 you wouldn't see the error. But it will start breaking when 2.4 is released.

Still, none of this should be an issue if isolatedModules is overriden to true.

Singham
  • 303
  • 2
  • 8
0

tsconfig.json

"compilerOptions": {
    ...
    "isolatedModules": false,
    ...
}
Dharman
  • 30,962
  • 25
  • 85
  • 135