Question
Typescript error: An outer value of 'this' is shadowed by this container
I had an error in a Typescript class method declaration, but I don't understand how the error message relates back to the bug.
The message seems to be saying that 'this' is of type any
, but we are in a class definition, and so I thought 'this' was really clear.
Can someone please explain how the error message relates back to the bug?
Original method:
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.
fix:
calcSize() {
return this.width * this.length;
};
Full context (fixed):
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;
};
}