在React组件中枚举的字符串数

时间:2018-01-29 13:58:27

标签: c# reactjs typescript enums

我有一个ASP.NET Core项目,我正在使用React。

C#实体:

public class Waybill: IEntity
{    
    public int Id { get; set; }

    public WayBillType WayBillType { get; set; }    
}

public enum WayBillType
{
    Moving = 1,
    Incoming = 2,
    Disposal  = 3
}

和打字稿实体:

export class Waybill
{
    id: number;
    wayBillType: WayBillType;
}

enum WayBillType
{
    Moving = 1,
    Incoming = 2,
    Outgoing = 3
}

我如何获得字符串"移动"而不是" 1"在我的React组件中?

2 个答案:

答案 0 :(得分:1)

您可以使用JsonConverter属性并将其Newtonsoft.Json.Converters传递给[JsonConverter(typeof(StringEnumConverter))] public enum WayBillType { Moving = 1, Incoming = 2, Disposal = 3 }

db_1      | ERROR: 1030  Got error 28 from storage engine
db_1      | 180129 13:57:08 [ERROR] Aborting
db_1      |
db_1      | 180129 13:57:08 [Note] /usr/sbin/mysqld: Shutdown complete
db_1      |
re53rc1_db_1 exited with code 1

答案 1 :(得分:1)

您可以使用tagged union type代替枚举:

type WayBillType = "Moving" | "Incoming" | "Outgoing";

如果您关心实际的整数值,可以使用index signature构建类似字典的对象并在其中保留映射:

interface WayBillTypeMap {
    [key: WayBillType]: int
}