如何创建启用了 isolatedModules=true 选项的包?

How to create a package with the isolatedModules=true option enabled?

在一个文件中,我将我的包的所有 类 导出到如下行:

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

生成的表格错误:

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

现在如何导出 类?

这个问题出现在CRA2.1。被迫将 isolatedModules=true。 我正在 CRA2.1

上制作组件库

是 - node_modules/fork-ts-checker-webpack-plugin/package.json 是 "version":“0.2.2”。

似乎更改是在 Microsoft/TypeScript#15538 中进行的,因此如果您使用 2.3 进行测试,您将不会看到错误。但是当 2.4 发布时它会开始崩溃。

不过,如果将 isolatedModules 重写为 true,none 这应该是个问题。

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 对该问题有最好的解决方法解释:

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";.

如果我没看错 GitHub 问题,则只能重新导出 TS 类型,但无法重新导出值(例如 classes)。因此,如果 TS 知道您正在导入一个类型(而不是 class),那么您可以重新导出它。

这是另一个更简单的例子:

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

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。你现在可以写:

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

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

查看 this answer 了解有关 import/export 语法的更多信息。

tsconfig.json

"compilerOptions": {
    ...
    "isolatedModules": false,
    ...
}