[英]How to de-structure an enum values in typescript?
I have an enum in typescript like below:我在打字稿中有一个枚举,如下所示:
export enum XMPPElementName {
state = "state",
presence = "presence",
iq = "iq",
unreadCount = "uc",
otherUserUnreadCount = "ouc",
sequenceID = "si",
lastSequenceID = "lsi",
timeStamp = "t",
body = "body",
message = "message"
}
And wants to de-structure its value, How can we do this in Typescript?并且想要解构它的价值,我们如何在 Typescript 中做到这一点?
const { uc, ouc, msg, lsi, si, t, body } = XMPPElementName;
As @amadan mentioned , we can use Assigning to new variable names
as in Mozilla doc say Destructuring_assignment , like below:正如@amadan提到的,我们可以像 Mozilla 文档中说的Destructuring_assignment那样使用
Assigning to new variable names
,如下所示:
A property can be unpacked from an object and assigned to a variable with a different name than the object property.一个属性可以从一个对象中解包出来,并分配给一个与对象属性名称不同的变量。
const o = {p: 42, q: true};
const {p: foo, q: bar} = o;
console.log(foo); // 42
console.log(bar); // true
And the method is very good to solve this problem, but if you need to access all items without the need to explicitly define them, you can either on of these two mentiond tag1 tag2并且该方法是非常好的解决了这个问题,但如果你需要访问的所有项目,而无需显式定义它们,您可以在这两个mentiond的标签1 TAG2
const { uc, ouc, msg, lsi, si, t, body } = XMPPElementName;
This doesn't work because XMPPElementName
doesn't have an element named uc
(and equivalently for others).这不起作用,因为
XMPPElementName
没有名为uc
的元素(对于其他元素XMPPElementName
如此)。 If you explicitly name your keys, it will work:如果您明确命名您的密钥,它将起作用:
const {
unreadCount: uc,
otherUserUnreadCount: ouc,
message: msg,
lastSequenceID: lsi,
sequenceID: si,
timeStamp: t,
body: body,
} = XMPPElementName;
it will work.它会起作用。 Alternately, you can just use variables with names that are equal to the keys, not the values:
或者,您可以只使用名称等于键的变量,而不是值:
const {
unreadCount,
otherUserUnreadCount,
message,
lastSequenceID,
sequenceID,
timeStamp,
body,
} = XMPPElementName;
As we know, in typescript an enum is like a plain old javascript object(at-least what the playground js-output is showing or the log showing):正如我们所知,在打字稿中,枚举就像一个普通的旧 javascript 对象(至少是操场 js 输出显示的内容或日志显示的内容):
one way is using a function which generates a new object with {value:value}
structure like below:一种方法是使用一个函数来生成一个具有
{value:value}
结构的新对象,如下所示:
export function convertEnumValuesToObject<T>(enumObj: T): { [index: string]: T[keyof T] } {
const enum_values = Object.values(enumObj);
return Object.assign({}, ...enum_values.map(_ => ({ [_]: _ })));
}
const { uc, ouc, msg, lsi, si, t, body } = convertEnumValuesToObject(
XMPPElementName
);
It would be great to see answers in typescript?在打字稿中看到答案会很棒吗?
You want an enum value-to-value map.您需要一个枚举值到值的映射。 Like you've said enum in JS is just a POJO.
就像你说 JS 中的 enum 只是一个 POJO。 You can create a utility type to help generate the correct type.
您可以创建一个实用程序类型来帮助生成正确的类型。
type EnumValueMap<T extends { [k: string]: string }> = { [K in T[keyof T]]: K }
function convertEnumValuesToObject<T extends { [k: string]: string }>(enumerable: T): EnumValueMap<T> {
return (Object as any).fromEntries(Object.values(enumerable).map(v => [v, v]))
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.