I have a json string like this.
{
"type": "A",
"desc": "AAA"
}
or
{
"type": "B",
"desc": "BBB"
}
etc.
How can I use enum to parse it with typescript? I can do it like this, but how to handle with desc
field?
enum Type {
A = "A",
B = "B"
}
CodePudding user response:
You may try using a type. Something like this
type Item {
type: "A" | "B";
desc: string;
}
CodePudding user response:
If the properties are related, i.e. type
fully defines the value of desc
, you can create a union type like this:
type Item =
{ type: "A", desc: "AAA" } |
{ type: "B", desc: "BBB" };