TypeScript:扩展导入的枚举
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41940349/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TypeScript: extending imported enum
提问by smnbbrv
I can merge enum declarations within a single file e.g.
我可以在单个文件中合并枚举声明,例如
export enum Test {
value1 = <any>'value1',
value2 = <any>'value2'
}
export enum Test {
value3 = <any>'value3'
}
This works fine, but my intention is to have a shared enum which I can extend later, e.g.
这工作正常,但我的目的是有一个共享的枚举,我可以稍后扩展,例如
// test.enum.ts
export enum Test {
value1 = <any>'value1',
value2 = <any>'value2'
}
// place-to-extend-enum.ts
import { Test } from './test.enum';
export enum Test {
value3 = <any>'value3'
}
What I get is
我得到的是
Individual declarations in merged declaration 'Test' must be all exported or all local.
合并声明“测试”中的单个声明必须全部导出或全部为本地声明。
Is there a way to achieve the desired behaviour?
有没有办法实现所需的行为?
回答by Chklang
In refer to https://github.com/Microsoft/TypeScript/pull/6213you can do :
在参考https://github.com/Microsoft/TypeScript/pull/6213你可以这样做:
// test.enum.ts
export enum Test {
value1 = <any>'value1',
value2 = <any>'value2'
}
// place-to-extend-enum.ts
import { Test } from './test.enum';
declare module './test.enum' {
export enum Test {
value3 = <any>'value3'
}
}
... Magic! ;)
... 魔法!;)
回答by VJPPaz
I saw a way that you can add additional function/method in an existing enum. this is by create the function within a namespace similar to the enum type: Here
我看到了一种可以在现有枚举中添加附加功能/方法的方法。这是通过在类似于枚举类型的命名空间中创建函数:这里
enum Weekday {
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday
}
namespace Weekday {
export function isBusinessDay(day: Weekday) {
switch (day) {
case Weekday.Saturday:
case Weekday.Sunday:
return false;
default:
return true;
}
}
}
const mon = Weekday.Monday;
const sun = Weekday.Sunday;
console.log(Weekday.isBusinessDay(mon)); // true
console.log(Weekday.isBusinessDay(sun)); // false
You can see the complete information here https://basarat.gitbooks.io/typescript/docs/enums.htmlat section "Enum with static functions"
您可以在此处https://basarat.gitbooks.io/typescript/docs/enums.html的“具有静态函数的枚举”部分查看完整信息
回答by smnbbrv
After some research I must admit I cannot find a super-proper way to do that.
经过一些研究,我必须承认我找不到一种非常合适的方法来做到这一点。
But there are two possible solutions that are not that bad and not stinking that much.
但是有两种可能的解决方案,既不那么糟糕,也不那么臭。
First is implementing a custom enum - this way is not allowing to consume already existing enums. This is probably the only limitation of this method. Other than that it looks simple and quite native.
首先是实现自定义枚举 - 这种方式不允许使用已经存在的枚举。这可能是这种方法的唯一限制。除此之外,它看起来简单而且非常原生。
Another way is a big hackaround with [merging enums into one value with a separate type declaration. This way allows to consume already existing, realenums; however it is less comfortable to use because there are two entities to be aware of: enum value and enum type.
另一种方法是[将枚举合并为一个具有单独类型声明的值的大hackaround。这种方式允许使用已经存在的真实枚举;但是使用起来不太舒服,因为有两个实体需要注意:枚举值和枚举类型。