microsoft / microsoft/TypeScript

Mixin classes with private/protected constructors are subclassable

オープン
#42,264 コメント 3 件 リアクション 2 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

Bug Domain: classes
主要言語
Go
スター
111k
フォーク
14.4k
平均マージ
1日 19時間
マージ済み PR(30日)
117

説明

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).

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

リンクされた TypeScript Playground から始め、提供された mixin と直接拡張の例で 2 つのコンストラクターアクセスチェックを再現します。mixin によって返されるクラス式のコンストラクターのアクセス可能性を追跡し、その後、new NonSubclassableThroughMixin() が直接サブクラスと同じ private コンストラクターの診断を受けるように、回帰テストを追加または更新します。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
typescript
領域
compilers
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
38/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。