Strange Typescript compiler Error: 'this' cannot be referenced in current location, Can anyone explain please?

Viewed 7604

I presume there is a good reason for this but it seems very strange to me. Please take the following code as an example of the issue.

class Foo {
    constructor(referenceMethod: () => void) {
    }
}

class Bar extends Foo {

    TestProperty: string = "boo";

    constructor() {
        super(this.ReferenceMethod);
    }

    ReferenceMethod() {

    }
}

This produces the compiler error

'this' cannot be referenced in current location.

If we initialise the TestProperty inside the constructor the compiler is happy and the world spins round as expected and of course if we don't pass the reference to the referenceMethod to the super call we can set the TestProperty outside the constructor fine.

However I just don't understand why doing both these things together causes a problem. If anyone could shed some light on this to help my understanding would be much appreciated.

3 Answers
Related