I don't understand what is happening or why it's made to do this under the hood:
If
Something
is a type, why doesexport { SomeThing } from "elsewhere"
produce an error when--isolatedModules
is enabled?Conversely why is
export { SomeThing } from "elsewhere"
not an error ifSomething
is a value, or if--isolatedModules
is not enabled?Why does specifying
export type { SomeThing } from "elsewhere"
fix that error?
(Background information)
There is a new TypeScript feature:
Type-Only Imports and Export
This feature is something most users may never have to think about; however, if you’ve hit issues under
--isolatedModules
, TypeScript’stranspileModule
API, or Babel, this feature might be relevant.TypeScript 3.8 adds a new syntax for type-only imports and exports.
import type { SomeThing } from "./some-module.js"; export type { SomeThing };
import type
only imports declarations to be used for type annotations and declarations. It always gets fully erased, so there’s no remnant of it at runtime. Similarly,export type
only provides an export that can be used for type contexts, and is also erased from TypeScript’s output.
I know how to use it and when it's necessary but I don't know why, i.e. apparently when --isolatedModules
is enabled then code like the following ...
import type { SomeThing } from "./some-module.js";
export { SomeThing };
... produces a compiler error i.e. ...
Re-exporting a type when the '--isolatedModules' flag is provided requires using 'export type'.ts(1205)
... and the fix is to use export type { SomeThing }
instead of export { SomeThing }
.
Incidentally though, apparently import { SomeThing } from "./some-module.js"
is OK, doesn't produce an error message, import type { SomeThing } from "./some-module.js"
is not required.
By the way this is not the same topic as What is `export type` in Typescript? which is about defining a type
and prefixing it with export
, in 2017 before this new feature was implemented.
There is this Babel Github issue, where it is explained:
There's also a great elaborated explanation on this Reddit thread: