microsoft / microsoft/TypeScript

Instance member function becomes instance member property in mapped types

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

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

Needs Investigation
主要言語
Go
スター
111k
フォーク
14.3k
平均マージ
2日 4時間
マージ済み PR(30日)
132

説明

TypeScript Version: 4.0.0-dev.20200512

Search Terms:

  • instance member function becomes instance member property
  • mixin property of exported class expression may not be private or protected

Code

export class Application {
  protected getState() {
    return 'starting';
  }

  component(componentCtor: typeof Object) {
    // mount the component
  }
}

// Ideally, I'd like to write `function repositoryMixin(superClass: typeof Application)`, but that
// triggers "property of exported class expression may not be private or protected"
export function repositoryMixin<T extends MixinTarget<Application>>(superClass: T) {
  return class extends superClass {
    component(componentCtor: typeof Object) {
      super.component(componentCtor);
      // register repositories contributed by the component
    }
  }
}

// helpers to work around TypeScript limitations related to mixins

export type Constructor<T extends object> = new (...args: any[]) => T;

export type MixinTarget<T extends object> = Constructor<
  {
    // Enumerate only public members to avoid the following compiler error:
    //   Property '(some-name)' of exported class expression
    //   may not be private or protected. ts(4094)
    [p in keyof T]: T[p];
  }
>;

Expected behavior:

The code compiles cleanly.

Actual behavior:

$  tsc mixin-bug.ts
mixin-bug.ts:13:5 - error TS2425: Class '{ component: (componentCtor: ObjectConstructor) => void; }' defines instance member property 'component', but extended class '(Anonymous class)' defines it as instance member function.

13     component(componentCtor: typeof Object) {
       ~~~~~~~~~

mixin-bug.ts:14:13 - error TS2340: Only public and protected methods of the base class are accessible via the 'super' keyword.

14       super.component(componentCtor);
               ~~~~~~~~~


Found 2 errors.

Playground Link:

https://www.typescriptlang.org/play/?ts=Nightly#code/MYGwhgzhAECCAO8QEthgC7IPYDtoG8AoaaYLAW3lwFMd0AKMymugYXSwCcAuadAT3jUsAM2gB5AEYArasHQBKAsRLQA9GujksAVzp8AFtVIUqOWuhUBfQjcIi987Hk7UqEZB078AssgAeyDgAPAAq0NT+6LQAJjB+gTihYJwA5tTowQhIqBjOAHz59BA6Qpys4FC8oUpEJK7oOpx4oJAwkdE4cdAlZRVtyqomzOZ0jKYs6OxcvAJCohIycoqDQz2l1JwAdExmFuMjFtOcCgDcKkMa0K6pyBDRnNduWB5eyNQwZHScyJI60TFoJJ+IZjLtJhdoDYSDY7FcjCAyjAONAAO5cADW0BSui60FCgmoAGVgD94OhoChyJ48rgYK5wAC+FgtAEghBCIRIlROBS5sZWHT0JwdPIuGEIlFYjAsEt5PloABeaDmVHQehbTUpVIQXhgHD8ADaAF0lIqFaFzlz-Dy+YToAkgsk0hkJR1pdBZbJ5UroIKcPcRWLOMEVHVVFcAKI4HTkTYYYy4EAg+A6SQoYBaajkSSbZEssAANywyEB6CM0BEWBAICwqKCqWG8GQIE2EU4nBmkKuJAACp2ygJoAByeg4MBxhTDz1iblcJmtKCS+CuKDObuaEjkMAgnBYCm56Ar5CFhOex4r-fLagxLboCD0AAsAAYAJyPhSQw3waBBaAY6h+AWUJjWqb9jXOGFCHyc4gADy+YToAkgsk0hkJR1pdBZbJ5UroIKcPcRWLOMEVHVVFcAKI4HTkTYYYy4EAg+A6SQoYBaajkSSbZEssAANywyEB6CM0BEWBAICwqKCqWG8GQIE2EU4nBmkKuJAACp2ygJoAByeg4MBxhTDz1iblcJmtKCS+CuKDObuaEjkMAgnBYCm56Ar5CFhOex4r-fLagxLboCD0AAsAAYAJyPhSQw3waBBaAY6h+AWUJjWqb9jXOGFCHyc4gA

Related Issues:

  • Remove property-override-method error #34494
  • 3.7: instance member function becomes instance member property in type references [outcome: fixed since 3.7.0-dev.20191016] #33747
  • Support for all type features in declaration files. #35822
  • False error TS4094: ... exported class expression may not be private or protected. #30355
  • Allow .d.ts files to represent anonymous class expressions with private members #36060
  • Unexpected TS4094 with the build parameter declaration: true#17293
  • Generating type definitions for mixin classes with protected members #17744

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

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

はじめの一歩

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

調査の方向性

リンクされている TypeScript Playground の例を使って mixin-bug.ts で報告された動作を再現し、診断 TS2425 と TS2340 を、期待されるエラーのないコンパイル結果と比較します。mapped types、mixin class expressions、およびメソッド/プロパティメンバーの分類を処理するコンパイラーのパスを調査します。例がこれらのエラーなしでコンパイルできれば完了です。

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

評価

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

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

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