如何在启用isolatedModules=true选项的情况下创建包



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

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

生成表单错误:

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

现在如何导出类?

此问题发生在CRA2.1中。已强制隔离Modules=true。我正在CRA2.1 上制作一个组件库

CRA v3.4.1.有助于--isolatedModules下的类型再导出。v7.9.0+版本中包含的@babel/preset-typescript(请参阅相应的Babel发布和公告(和TypeScript仅支持TS 3.8类型的导入和导出。你现在可以写:

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

有关import/export语法的更多信息,请查看此答案。

github.com/babel/babel-loader/issues/603(感谢@CollinD提供的链接(包括如何重新导出导入类型的解决方法。关于这个问题的评论对解决方法有最好的解释:

如果很明显你正在导出一种类型,你仍然可以进行导出:

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

您也可以执行export * from "./a";

如果我正确阅读了GitHub的问题,那么只有TS类型可以重新导出,但值(例如类(不能重新导出。所以,如果TS知道你正在导入一个类型(而不是类(,那么你可以重新导出它

下面是另一个更简单的例子:

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

是-node_modules/fork-ts-checker-webpack-plugin/package.json是"版本":"0.2.2"。

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

尽管如此,如果isolatedModules被重写为true,那么这些都不应该成为问题。

tsconfig.json

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

相关内容

最新更新