TypeScript:导出复杂接口
请查看下面的代码:TypeScript:导出复杂接口,typescript,Typescript,请查看下面的代码: enum ActionTypeEnum { GET_WAREHOUSE_ITEM_LIST_INITIAL = 'GET_WAREHOUSE_ITEM_LIST_INITIAL', GET_BASKET = 'GET_BASKET', } interface Action { type: ActionTypeEnum, } export {ActionTypeEnum}; // works fine export {Action};
enum ActionTypeEnum {
GET_WAREHOUSE_ITEM_LIST_INITIAL = 'GET_WAREHOUSE_ITEM_LIST_INITIAL',
GET_BASKET = 'GET_BASKET',
}
interface Action {
type: ActionTypeEnum,
}
export {ActionTypeEnum}; // works fine
export {Action}; //Cannot re-export a type when the '--isolatedModules' flag is provided.
据我所知,可以导出ActionTypeEnum
,因为它不依赖于任何东西。据我所知,无法导出
Action
,因为它使用ActionTypeEnum
,无法单独导出
请告诉我如何导出操作
,以及我对问题的理解是否正确
谢谢!:-) 。编译器在以下情况下发出投诉:
隔离模块
和export{SomeThing}
与类型或接口一起使用李>
这是因为,对于独立的模块,每个模块都是独立的,这使得像babel
这样的Transpiler很难决定SomeThing
是否有JavaScript表示。如果SomeThing
是类、函数或对象,则transpiler需要用JavaScript表示它。另一方面,如果某物
是类型
或接口
,则transpiler不能用JavaScript表示它
怎么办
一个选项是内联导出接口{}
语句,如下所示:
enum ActionTypeEnum1 {
// ...
}
export interface Action1 { // <-------- inline export
type: ActionTypeEnum1;
}
export { ActionTypeEnum1 };
enum ActionTypeEnum2 {
// ...
}
interface Action2 {
type: ActionTypeEnum2;
}
export type { Action2 } // <-------- export type { }
export { ActionTypeEnum2 }
我得到'Action2'只引用一个类型,但在这里被用作一个值。
错误现在:-(@JacekWojcik您正在运行什么版本的TypeScript?导出类型{}
语法需要TypeScript3.8
或更高版本。>tsc--版本3.8.3我正在使用VS code,依赖项:“TypeScript”:“^3.8.3”@JacekWojcik我不确定我还能提供什么帮助。也许你可以将一个简单的示例推送到GitHub存储库,以帮助我们重现错误。同时,这里是游乐场上的示例: