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