【发布时间】:2019-09-08 23:15:20
【问题描述】:
我从后端获取 JSON 数据(我可以对其进行一些控制,因此如果有帮助,可以在某种程度上对其进行更改),如下所示:
{
"attributes": [
{
"type": "string",
"value": "foobar"
},
{
"type": "number",
"value": 1234
},
{
"type": "annotated_string",
"value": {
"value": "barfoo",
"comment": "This is a comment"
}
}
]
}
我想找到一个可以代表这个的 TypeScript 类型。我阅读了有关条件类型的信息,它们似乎与问题的一部分相匹配。
value 取决于 type 字段的类型(实际上是值)。
我想出了这个:
type AttributeGroup = {
attributes: Attribute[] // <-- This requires a generic parameter
}
enum AttributeType { "string" , "number", "annotated_string"}
type Attribute<T extends AttributeType> = {
type: T;
value: ExtractMyParameter<T>;
}
type AnnotatedString = {
value: string;
comment: string;
}
type ExtractMyParameter<T> =
T extends AttributeType.number ? number :
T extends AttributeType.string ? string :
T extends AttributeType.annotated_string ? AnnotatedString :
never;
let attr: Attribute<AttributeType.string> = { // <-- Ugly to have to define it twice
type: AttributeType.string, // <-- I don't want to do this but instead: type: "string"
value: "1234"
}
有没有一种好方法可以将我的数据表示为 TypeScript 中的类型? 上面的代码由于各种原因不起作用:
-
Attribute[]缺少类型参数,但数组的每个元素可能具有不同的类型。我想这可能会以某种方式通过 Union 类型解决? - 显式创建实例时必须专门定义 Attribute 很丑(这只是为了测试,所以还不错)
我认为必须有更好的方法来处理此类数据。
【问题讨论】:
-
TypeScript 完成的输入纯粹是编译时的事情。类型不能根据直到运行时才会出现的值而改变。
-
你可能想使用quicktype.io - 他们有一个很棒的 JSON 到我使用的 Typescript 类型生成器!
标签: typescript typescript-generics