TS2536: Type 'xxx' cannot be used to index type 'this'
父类的方法修改子类的属性,类型注释怎么写,试了很多写法,还是会报错
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) {
changeValue[key] = this[key]
changeValue[key] = props[key] - (this as any)[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
})
Error message:
[tsl] ERROR in /typescript/app/index.ts(16,26)
TS2536: Type 'Extract<keyof T, string>' cannot be used to index type 'this'.
[tsl] ERROR in /typescript/app/index.ts(17,7)
TS2322: Type 'number' is not assignable to type 'T[Extract<keyof T, string>] | undefined'.
[tsl] ERROR in /typescript/app/index.ts(17,26)
TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因为有个减法,这是最最最没办法的办法