Skip to content

Protected constructors should be accessible on subclasses's static methods #9734

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
tinganho opened this issue Jul 14, 2016 · 1 comment
Closed
Assignees
Labels
Bug A bug in TypeScript

Comments

@tinganho
Copy link
Contributor

I think the below should be allowed.

class A {
    protected constructor() {

    }
}

class B extends A {
    static create() {
        new B(); // ERROR, but should not error.
    }
}

I wanted to override a static factory and customise it a bit. But it seems like I wasn't allowed to do it.

And my motivation is that private constructors are accessible on a class's static methods.

class A {
    static create() {
        new this();
    }
    protected constructor() {

    }
}

And a natural extension of the private case is to allow protected constructors on subclasses's static methods.

@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Jul 14, 2016
@RyanCavanaugh
Copy link
Member

Definitely a bug; this should be equivalent to the allowed

class A {
    protected constructor() {

    }
}

class B extends A {
    protected constructor() { super(); }
    static create() {
        new B(); // OK
    }
}

@mhegazy mhegazy added this to the TypeScript 2.0.1 milestone Jul 14, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants