Typescript 枚举、接口和映射
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50065649/
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 enum, interface and mapping
提问by Ben
In TypeScript, in an array or similar data structure, how can I map a string to an id, while ensuring that only a certain range of ids is allowed?
在 TypeScript 中,在数组或类似数据结构中,如何将字符串映射到 id,同时确保仅允许特定范围的 id?
Here is what I want to do. This works fine. However, I am wondering if there is a more concise way of achieving this?
这是我想要做的。这工作正常。但是,我想知道是否有更简洁的方法来实现这一目标?
enum ETypeId {
alpha = "a",
beta = "b",
gamma = "g"
}
interface IType {
id: ETypeId,
title: string,
}
myTypes: IType[] = [
{ id: ETypeId.alpha, title: "Alpha" },
{ id: ETypeId.beta, title: "Beta" },
{ id: ETypeId.gamma, title: "Gamma" }
];
As is, I have to do the following to get from the id
to the title
:
按原样,我必须执行以下操作才能从id
到title
:
function getTypeForTypeId( typeId: ETypeId ): IType {
return myTypes.find( type => type.id == typeId );
}
Can I use a different data structure that makes some of the above code more concise, or is this already as good as it gets?
我可以使用不同的数据结构来使上面的一些代码更简洁,还是已经达到了它的目的?
Explanation:
解释:
"a"
is what gets stored in my databaseETypeId.alpha
is how I access it in my code"Alpha"
is what gets displayed to the user.
"a"
是存储在我的数据库中的内容ETypeId.alpha
是我在代码中访问它的方式"Alpha"
是显示给用户的。
回答by mykhailo.romaniuk
Agree with Sergi Dote Teixidor'sanswer that Mapis the best option for such problem. However, based on the described problem, I think that it could be simplified to Map<ETypeId, string>
:
同意Sergi Dote Teixidor 的回答,即Map是解决此类问题的最佳选择。但是,基于所描述的问题,我认为它可以简化为Map<ETypeId, string>
:
enum ETypeId {
alpha = "a",
beta = "b"
}
const types: Map<ETypeId, string> = new Map( [
[ ETypeId.alpha, "Alpha" ],
[ ETypeId.beta, "Beta" ],
]);
Just in case you want to initialize your structure once and make TypeScript protect you from changing values inside your map:
以防万一您想初始化一次结构并使 TypeScript 保护您免受更改地图中的值的影响:
enum ETypeId {
alpha = "a",
beta = "b"
}
interface ReadonlyMap<TKey, TValue> {
get(key: TKey):TValue;
}
const types: ReadonlyMap<ETypeId, string> = new Map( [
[ ETypeId.alpha, "Alpha" ],
[ ETypeId.beta, "Beta" ],
]);
// Static analyzer error if you try to change the value:
types.set(ETypeId.alpha, "NewValue");
回答by Sergi Dote Teixidor
you can use a map:
你可以使用地图:
- you can retrieve directly any element
- you can loop all elements if you want
- 您可以直接检索任何元素
- 如果需要,您可以循环所有元素
example:
例子:
enum ETypeId {
alpha = "a",
beta = "b",
gamma = "g"
}
interface IType {
id: ETypeId,
title: string,
}
const myMap: Map<string, IType> = new Map( [
[ ETypeId.alpha, { id: ETypeId.alpha, title: "Alpha" } ],
[ ETypeId.beta, { id: ETypeId.beta, title: "Beta" } ],
[ ETypeId.gamma, { id: ETypeId.gamma, title: "Gamma" } ]
]);
console.log(myMap.get(ETypeId.alpha)) // -> {id: "a", title: "Alpha"}