Why does TS with --isolatedModules not allow export declarations of type even when they are aliased?

156 views Asked by At

TS doesn't allow the following when the --isolatedModules is true:

export { Thing } from "./thing";

it also doesn't allow variations like this:

import { Thing } from "./thing";
export { Thing };

TS 3.8 has new syntax to tell TS that an import or export is type-only, but we can't use it just yet.

The nicest workaround I know of is:

import { Thing as ThingType } from "./thing";
export type Thing = ThingType;

The above works because TS knows that the exported Thing is a type, since it's created via a type alias.

Why doesn't the below also work? TS seems to have enough information to know that Thing is type-only:

import { Thing as ThingType } from "./thing";
type Thing = ThingType;
export { Thing };

As a data point, Babel's TS plugin has no problem figuring out that Thing is a type in that example.

Since the point of --isolatedModules is to ensure that single-file transpilation is safe, it seems that goal can be achieved while allowing export declarations like in the last example above.

So is TS's behavior here a bug? If it's intentional, then what is the reason for it?

0

There are 0 answers