[英]How to get typeof generic model's parameter inside of another type?
我有一个采用类型 T 的接口:
export interface ValueTimestampModel<T> {
value: T;
timestamp: string;
}
我有一个类型应该使用上面的类型通过将 T 的每个参数转换为 ValueTimestampModel 来转换给定的 T 类型的ValueTimestampModel
:
export type ModelWithTimestamp<T> = {
[P in keyof T]?: ValueTimestampModel<typeof T[P]>
}
所以期望的结果将是 -> 如果我们有 model:
{
street: string;
streetNo: number;
}
那么ModelWithTimestamp
版本应该是这样的:
{
street: {
value: string,
timestamp: string
},
streetNo: {
value: number,
timestamp: string
}
}
不幸的是,部分typeof T[P]
返回编译错误:
'T' only refers to a type, but is being used as a value here.
在这种情况下,typescript 似乎不接受typeof
。 有没有其他方法可以实现我正在寻找的东西?
您不需要typeof
, T[P]
已经是类型T
中的属性P
的类型:
export interface ValueTimestampModel<T> {
value: T;
timestamp: string;
}
export type ModelWithTimestamp<T> = {
[P in keyof T]?: ValueTimestampModel<T[P]>
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.