I don't understand what is happening or why it's made to do this under the hood:
If
Somethingis a type, why doesexport { SomeThing } from "elsewhere"produce an error when--isolatedModulesis enabled?Conversely why is
export { SomeThing } from "elsewhere"not an error ifSomethingis a value, or if--isolatedModulesis 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’stranspileModuleAPI, 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 typeonly 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 typeonly 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.