microsoft / microsoft/TypeScript
Mixin classes with private/protected constructors are subclassable
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
Bug Report
🔎 Search Terms
mixin, private constructor
🕗 Version & Regression Information
This is the behavior in every version I tried, and I reviewed the FAQ for entries about Classes.
Versions tried:
- 3.7.4
- 4.1.3
- Nightly
⏯ Playground Link
Playground link with relevant code
💻 Code
function PreventSubclassingMixin<TBase extends new(...args: any[]) => any>(Base: TBase) {
return class NonSubclassable extends Base {
private constructor(...args: any[]) {
super();
}
}
}
class Base {
constructor() {}
}
class NonSubclassableThroughExtension extends Base {
private constructor() {
super();
}
}
const NonSubclassableThroughMixin = PreventSubclassingMixin(Base);
new NonSubclassableThroughExtension(); // Constructor of class 'NonSubclassableThroughExtension' is private and only accessible within the class declaration.(2673)
new NonSubclassableThroughMixin(); // Doesn't throw an error
🙁 Actual behavior
NonSubclassableThroughExtension correctly throw an error — I've extended a superclass Base, which has a publicly accessible constructor, and provided a private constructor in the derived class. I can't access the derived class' constructor anymore:
new NonSubclassableThroughExtension(); // Constructor of class 'NonSubclassableThroughExtension' is private and only accessible within the class declaration.(2673)
The PreventSubclassingMixin supposedly works the same — it creates a class expression that extends the Base class with a public constructor and defines a private constructor, and returns it. The returned derived mixin class is supposedly identical to the example above. However, if I apply the mixin to the Base: const NonSubclassableThroughMixin = PreventSubclassingMixin(Base), and try to initialize it, Typescript won't throw any error.
🙂 Expected behavior
new NonSubclassableThroughMixin() should throw a Constructor of class 'NonSubclassableThroughExtension' is private and only accessible within the class declaration.(2673).
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the linked TypeScript Playground and reproduce the two constructor-access checks in the supplied mixin and direct-extension examples. Trace constructor accessibility for the class expression returned by the mixin, then add or update a regression test so new NonSubclassableThroughMixin() receives the same private-constructor diagnostic as the direct subclass.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100