What I want to be able to do is ultimately have access to the class inside a constructor. Accessing this.constructor should be safe (I think?), and would allow for sub-classes to call their own static methods before calling the super:
class BaseProvider {
constructor(url: string) { ... };
}
abstract class GenericProvider {
constructor(something: any) {
let url = this.constructor.getUrl(something);
super(url);
}
static getUrl(something: any): string {
throw new Error("subclass must implement this");
}
}
class SpecificProvider {
// This class does not need to override the constructor, just the static method
static getUrl(something: any): string {
return "https://helloworld"
}
}
I have not been able to find a way to get around this, but if it already exists, awesome. Otherwise, this would go a long way to improving the use of static methods in TypeScript, and a the standard way to handle things like this.
Thanks!
It's a ReferenceError to reference this in the constructor of a derived class before calling super().
This has been that way since the introduction of classes in ES6
I think you want new.target, right?
You are absolutely correct, that is exactly what I was looking for, and it works great!
Thanks! :)
(quick note for others that find this issue; when compiled for the ES5 target, the generated code replaces new.target with this.constructor :))
Yeah, my understanding is that this scenario's why new.target exists. ¯\_(ツ)_/¯