$属性类型得到数组项
问题描述:
我有一个类型定义是这样的:
type Blah = {
fields: {
value: string
}[]
}
,我们在这里看到的字段是一堆阵列条目。我想通过$PropertyType
解决。
做$PropertyType<Blah, 'fields'>
给了我们数组,是否有可能获得元素?
我不能这样做的原因type Field = {value:string}
然后键入Blah = {fields:Field []} is because I don't have control over type
Blah`,我从第三方包导入它。
答
您可以使用新的$ElementType
utility来做到这一点。
type Blah = {
fields: {
value: string
}[]
}
type Fields = $PropertyType<Blah, 'fields'>
type Field = $ElementType<Fields, number>
const field: Field = {
value: 'blah'
}
哇绝对太酷了!谢谢! – Noitidart