question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support for both public and protected constructor overload signatures

See original GitHub issue

Suggestion

TypeScript already supports abstract class, which cannot be instantiated (of course, I know they can — it’s just the compiler that disallow that). Non–abstract classes, on the other hand, can be instantiated using any constructor overload they provide. There are cases, however, when you may want to allow creation of a class using only one constructor overload and provide another one for subclasses to use. The latter is what I would call “protected constructor,” which could be defined with the addition of the modifier protected. However, it looks like TypeScript currently doesn’t support overloads with different access modifiers; I wonder why, as signature matching is merely a static check and doesn’t change the way the method is actually invoked. If different overload signatures were allowed to support different access modifiers, the scenario with a protected constructor and a public one I described would be permitted.

🔍 Search Terms

protected constructor abstract constructor

✅ Viability Checklist

My suggestion meets these guidelines:

  • This wouldn’t be a breaking change in existing TypeScript/JavaScript code
  • This wouldn’t change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn’t a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript’s Design Goals.

⭐ Suggestion

Just allow having different access modifiers for different overload signatures. There’s no need I can see to disallow that and it wouldn’t be difficult at all to implement.

📃 Motivating Example

Suppose I want to use the composition pattern and provide default sub–instances in the public constructor but still allow subclasses to pass their own sub–instances, as follows:

class Controller {}

class View {

    constructor();

    protected constructor(controller: Controller);

    constructor(readonly controller: Controller = new Controller()) {}
}

class CustomController extends Controller {}

class CustomView extends View {

    constructor() {
        super(new CustomController());
    }
}

💻 Use Cases

Additionally to the “protected constructor” use case I described above, since such a feature requires, as previously mentioned, allowing different access modifiers across overload signatures, this change would also permit a lot of other things. I suppose you all can understand that having protected signatures for a subclass and exposing only public signatures for other sites may turn useful.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:2
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
andrewbranchcommented, Apr 9, 2021

AFAIK the reasoning in #9592 is still true, but I’ll leave this open as a suggestion so people can discuss, since the old issue is locked.

0reactions
alaboudicommented, Mar 13, 2022

It would be nice to see this feature

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to mimic private and public constructor overloads in ...
I have a class that is intended to be constructed from two different places, outside of the class, and inside ...
Read more >
Types of breaking changes - .NET - Microsoft Learn
ALLOWED: Adding the sealed or abstract keyword to a type when there are no accessible (public or protected) constructors.
Read more >
Chapter 8. Classes - Oracle Help Center
Inheritance of public and protected Class Members ... Two methods or constructors, M and N , have the same signature if they have...
Read more >
Inheritance — What your mother never told you, C++ FAQ
When my base class's constructor calls a virtual function on its this object, ... The idea of the Public Overloaded Non-Virtuals Call Protected...
Read more >
Default constructors - cppreference.com
Concurrency support library (C++11) ... override specifier (C++11) ... A type with a public default constructor is DefaultConstructible.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found