microsoft / microsoft/TypeScript

Properties of instances of anonymous classes have type <any> after an instanceof guard.

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

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

Domain: check: Control Flow Possible Improvement
主要言語
Go
スター
111k
フォーク
14.3k
平均マージ
2日 4時間
マージ済み PR(30日)
132

説明

TypeScript Version: 2.4.1

Code

function create_my_class<T>() {
    return class {
        value: T;
        constructor(
            value: T
        ) {
            this.value = value;
        }
    }
}
const MyClassWithNumber = create_my_class<number>();
const with_number = new MyClassWithNumber(234);

with_number.value = 10;
// with_number.value = "sdf"; // error, and rightly so

const something: any = with_number;

if(something instanceof MyClassWithNumber) {
    something.value = 20;
    something.value = "sdf"; // not an error, but should be one
}

Expected behavior:
The last assignment should be marked as a type error by TS.

Actual behavior:
The last assignment is not marked as error. This is somewhat surprising, as the line

with_number.value = "sdf";

is righteously reported as a type error by TS.

Not a huge issue, but prevented me from using a class factory.

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

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

はじめの一歩

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

調査の方向性

提供された TypeScript の再現コードから始め、instanceof ガードによってジェネリックなクラスファクトリが返す値の型がどのように絞り込まれるかを追跡します。示されている代入を使って動作を検証し、そのうえで、ガードされた文字列の代入が型エラーとして報告される一方、数値の代入は引き続き有効であることを確認します。

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

評価

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

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

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