У меня была ошибка в объявлении метода класса Typescript, но я не понимаю, как сообщение об ошибке связано с ошибкой.
Похоже, в сообщении говорится, что «this» относится к типу any
, но мы находимся в определении класса, и поэтому я подумал, что «this» действительно ясно.
Может кто-нибудь объяснить, как сообщение об ошибке относится к ошибке?
Оригинальный метод:
calcSize = function() {
return this.width * this.length; // Error on this line
};
// Error text: 'this' implicitly has type 'any' because it does not
//have a type annotation.ts(2683)
//app.ts(39, 16): An outer value of 'this' is shadowed by this container.
фикс:
calcSize() {
return this.width * this.length;
};
Полный контекст (исправлено):
class BaseObject {
constructor(
public width: number = 0,
public length: number = 0
) {}
};
class Rectangle extends BaseObject {
constructor(public width: number = 0, public length: number = 0) {
super(width, length);
}
calcSize() {
return this.width * this.length;
};
}