【发布时间】:2021-08-04 16:15:49
【问题描述】:
父类方法更新子类的属性,但我收到这些错误消息。我该如何解决这个问题。
index.ts 代码:
class A {
animate<
K extends keyof this,
T extends {
[key in K]: this[key] extends number ? this[key] : never
}
>(props: Partial<T>) {
const initialValue: Partial<T> = {}
const changeValue: Partial<T> = {}
for (let key in props) {
initialValue[key] = this[key]
changeValue[key] = props[key] - this[key]
}
// ...
}
}
class B extends A {
width: number
heigth: number
name: string
constructor() {
super();
this.heigth = 20
this.width = 20
this.name = 'B'
}
}
const b = new B();
b.animate({
width: 40,
heigth: 40
})
错误信息:
/project/app/index.ts(13,27) 中的错误
TS2536:类型“Extract
/project/app/index.ts(14,7) 中的错误
TS2322:类型 'number' 不可分配给类型 'T[Extract
/project/app/index.ts(14,26) 中的错误 TS2362:算术运算的左侧必须是“any”、“number”、“bigint”类型或枚举类型。
/project/app/index.ts(14,39) 中的错误
TS2536:类型“Extract
【问题讨论】:
标签: typescript