microsoft / microsoft/TypeScript

Compiler should complain about implicit return value of child class constructor, if it does not satisfy child class type

オープン
#13,819 コメント 9 件 リアクション 0 件 担当者 0 名 GitHub で見る

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

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

説明

TypeScript Version:  2.1.5

Since TypeScript 2.1 (due to #7574) the return value of a constructor call is the return value of the super constructor. So that

class Child extends Base {}

is compiled to

function Child() {
     return _super !== null && _super.apply(this, arguments) || this;
}

Actual behavior:
The actual problem is, that the implementation of Child(see example below) will be compiled without errors, despite of the fact that the instance (child) does not satisfy its type Child, because someMethod does not exist on child.

class Base {
    hello = 'base';
    constructor() {
        return {
            hello: 'world'
        };
    }
}

class Child extends Base {  // <-- compiler does not complain
    someMethod() {}
}

const child = new Child();
child.someMethod();  // <-- compiler does NOT complain, despite of "someMethod" does not exist on {hello: 'world'}

Expected behavior:
The compiler should complain about the implicit return value of the child class constructor, if this value is not a type of the child class. So that either an explicitly defined return value for the child class constructor should be forced like...

class Child extends Base { 

   constructor() {
     super();
     return { 
       someMethod() {},
       /* ... inherited members */
     }
   }
   someMethod() {}
}

or the return value has to be extended (const base = super()) to fulfill the type Child or all members of the child class have to be optional.

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

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

はじめの一歩

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

調査の方向性

この issue には TypeScript のインライン再現コードがありますが、ソースファイルやテストは指定されていません。まず派生コンストラクターのケースを再現し、コンパイラーによるコンストラクターの戻り値の型の処理を追跡してください。完了条件は、暗黙の子コンストラクターの戻り値が子クラスの型を満たせない場合にコンパイラーが報告することです。

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

評価

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

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

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