Skip to content

Allow "this.constructor" in a constructor before super #30537

@ricmoo

Description

@ricmoo

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions