如何在打字稿中访问枚举值
我有一个界面:
export interface Person {
firstName?: string;
lastName?: string;
status?: Status;
}
export enum Status {
Single,
Married,
Divorced
}
如果我创建一个人
Person john = {
firstName: "John",
lastName: "Smith",
status: Status.Married,
}
访问约翰的身份
var johnStatus = john.status;
,并且决定通过数字
(1)我如何获得它,以便我获得实际值(单身,已婚和离婚),而不是数字?我是Typescript的新手,似乎在线上没有很多信息。我尝试添加关键字,例如'typeof'或'keyOf',但它们不起作用。我可以使用其他任何关键字吗?我需要创建助手方法吗?感谢任何帮助!
I have an interface:
export interface Person {
firstName?: string;
lastName?: string;
status?: Status;
}
export enum Status {
Single,
Married,
Divorced
}
if I create a Person
Person john = {
firstName: "John",
lastName: "Smith",
status: Status.Married,
}
And I decide to access John's status by
var johnStatus = john.status;
I would get a number (1)
How do I make it so that I get the actual values (Single, Married, and Divorced) instead of a number? I am very new to typescript and it doesn't seem like there are a lot of info on this online. I tried adding keyword such as 'typeof' or 'keyof' but they don't work. Is there any other keywords I can use. Do I need to create a helper method? I will appreciate any help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
或者你可以做
or you can do