2

I am looking at another person's code and in the code, they have code that is essentially this:

class foo extends bar {
    constructor() {
        super(param1, param2, new certainObject(this, otherParams));
    }
}

The problem I am having with this code is that it says gives the error that 'super' must be called before accessing 'this' in the constructor of a derived class.

I am wondering: is there was a way to use this in the call to super() or am I going to have to completely restructure the code?

dragonFire
  • 98
  • 1
  • 9

1 Answers1

0

I don't think there's a direct non-hackish way to use this before super call and not as a part of that call (and this is exactly what the error is telling you).

Here's what the TypeScript book says about it. There's also a related (not the same) issue opened in GitHub to allow at least some non this related code to be executed before super.

Igor Soloydenko
  • 11,067
  • 11
  • 47
  • 90
  • 3
    In fact, this is not anything that TypeScript can address. It is part of the ES2015 spec, as [this answer](https://stackoverflow.com/a/31079103/2887218) describes. – jcalz Oct 13 '17 at 18:23
  • @jcalz oh, good there's an answer already. Should we mark this question as "duplicate" then? – Igor Soloydenko Oct 13 '17 at 18:24
  • Hmm, well the question isn't exactly a duplicate of the other question because the other question is asking if they can skip `super()` entirely. The accepted answer is very applicable here, though. – jcalz Oct 13 '17 at 18:30
  • @jcalz you are right. I am not familiar with the strict criteria for a pair of questions to be considered duplicates... – Igor Soloydenko Oct 13 '17 at 18:31