microsoft / microsoft/TypeScript

JS ES6 class derived member functions typed with literals don't get assigned the correct type

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

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

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

説明

Bug Report

In rare cases (such as when literals are used in a type constraint), ES6 class overridden functions will cause strange issues when returning said literals from the derived function.

🔎 Search Terms

ts es6 class derived

🕗 Version & Regression Information
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about ES6 classes
⏯ Playground Link

Playground link with relevant code

💻 Code
class Base
{
    test()
    {
        if (Math.random() > 0.5)
            return 5;

        return false;
    }
}

class Derived extends Base
{
    // error here: for some reason, Derived::test() is typed as "() => boolean"
    // even though the literal false is being returned, and as such it says it can't
    // match Base::test
    test()
    {
        return false;
    }
}

🙁 Actual behavior

The derived function's auto-typing assumes it is the literals' actual type, such as number or boolean, instead of the literals themselves. Derived::test, if not overridden, is typed as () => false, which should match () => 5 | false, but because it's typed as boolean instead it breaks (this only seems to happen if we're overriding a base member function)

🙂 Expected behavior

The derived function should be able to match if the typing is handled as normal.

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

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

はじめの一歩

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

調査の方向性

リンクされた TypeScript Playground の再現コードから始め、Base::test と Derived::test に対して推論される型を比較します。オーバーライドされた ES6 メソッドがリテラルの戻り値型をどのように推論するかを追跡し、その後、リテラル型を失うことなく、派生関数が基底のリテラルユニオンに対して受け入れられることを確認します。

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

評価

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

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

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