【发布时间】:2021-01-20 15:15:17
【问题描述】:
我想知道,是否可以为使用字符串数组作为键的 TypeScript 类型添加前缀或后缀。
type First = {
one: string
two: string
}
type Second = keyof First
type Third = {
[S in Second]: any
}
使用这种方法,Third 接受任何类型的属性one 和two。这太棒了,因为我只需要更改第一种类型就可以更新其他类型。
现在我想要第四种类型,它与Third 完全相同,但我想为键添加前缀。比如美元符号什么的。
想要的结果:
type Fourth = {
$one: any
$two: any
}
我可以对其进行硬编码,但如果第一个类型已更改,我将不得不调整第四个类型。
谢谢。
【问题讨论】:
标签: typescript typing