从命名空间重新导出 Typescript 枚举? [英] re-export Typescript enum from namespace?
问题描述
我在模块some-lib"中有一个枚举定义.我想从我的模块中的命名空间重新导出它,如下所示:
I have an enum definition in module 'some-lib'. I want to re-export it from a namespace in my module, something like this:
import { PaymentType } from 'some-lib';
namespace Payout {
export enum PaymentType = PaymentType;
}
我没有任何运气.我想这样做是为了给枚举取别名并将其放入不同的命名空间中,以避免与具有相同名称的其他类型发生冲突.我不想在我的代码中定义枚举的重复副本,并且必须维护所有枚举值.
I'm not having any luck. I want to do this in order to alias the enum and put it into a different namespace to avoid collisions with other types with the same name. I don't want to have to define a duplicate copy of the enum in my code, and have to maintain all the enum values.
Typescript 目前有什么办法支持这个吗?
Is there any way Typescript supports this currently?
推荐答案
是的,有一种方法可以做到这一点,例如:
Yes, there's a way to do this, e.g.:
import { PaymentType as _PaymentType } from 'some-lib';
namespace Payout {
export import PaymentType = _PaymentType;
}
或者:
import * as SomeLibTypes from 'some-lib';
namespace Payout {
export import PaymentType = SomeLibTypes.PaymentType;
}
参考:https://github.com/Microsoft/TypeScript/issues/20273#issuecomment-347079963
这篇关于从命名空间重新导出 Typescript 枚举?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!