class A<T>
{
some: { [K in keyof T]: (x: T[K]) => T[K] }
}
interface IB {
b: number
}
class B<T extends IB> extends A<T>
{
constructor()
{
super()
/**
* Type '{ b: (x: T["b"]) => number; }'
* is not assignable to type '{ [K in keyof T]: (x: T[K]) => T[K]; }'.
*/
this.some = {
b: x => 2*x
}
}
}
interface IC {
b: number
c: boolean
}
class C<T extends IC> extends B<T>
{
constructor()
{
super()
/**
* Type '{ b: (x: T["b"]) => number; c: (x: T["c"]) => boolean; }'
* is not assignable to type '{ [K in keyof T]: (x: T[K]) => T[K]; }'
*/
this.some = {
b: x => 4*x,
c: x => !x
}
}
}
你好。我嘗試在基類「A」中設置通用約束,目的是自動推斷派生類中「某些」屬性的類型。不幸的是,我不明白爲什麼我會像上面提到的那樣得到TS錯誤。從我的角度來看,一切似乎都沒問題。從打字稿中的基類泛型推斷子類屬性類型
謝謝!
這是一個非常詳細的答案:)現在一切都很清楚,你的幫助。非常感謝! – user2340487