【发布时间】:2023-01-10 20:54:03
【问题描述】:
我有两个具有相同可选键但值不同的接口:
interface Obj1 {
a?: string
b?: string
c?: string
}
interface Obj2 {
a?: boolean
b?: boolean
c?: boolean
}
Obj1 用作函数参数,另一个 Obj2 是该函数的返回值。我希望返回类型仅标识 Obj1 上的给定键。因此,如果 Obj1 仅包含 a 和 b,则 Obj2 也将仅包含 a 和 b。
我尝试了下面的方法,但是我得到了一个 ts 错误Type 'Property' cannot be used to index type 'ValueType'
type Obj1KeysWithObj2Values<KeyType extends Obj1, ValueType extends Obj2> = {
[Property in keyof KeyType]: ValueType[Property]
}
【问题讨论】:
-
你会用你试图使用它的函数的签名来更新你的问题吗?
标签: typescript