Assigning to a string fails if it was previously compared to an enum

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

@elibarzilay がすでに取り組んでいます。

2020年1月13日 から。

評価

この issue はまだ評価されていません。

説明

Bug Domain: check: Control Flow Rescheduled

TypeScript Version: What is installed on the TypeScript playground

Assigning to a string fails if it was previously compared to an enum

If you run the code below in the TypeScript playground, line widgetName += 'AddSomething' errors out with Type 'string' cannot be assigned to type 'Widgets'.

However, if I cast the enum on the previous line to string (widgetName == <string> Widgets.ComparableHomeSales), everything compiles fine.

In addition, if I replace widgetName += 'AddSomething'; with widgetName = widgetName + 'AddSomething';, it also compiles fine.

Code

enum Widgets {
    ComparableHomeSales = 'ComparableHomeSales',
    PriceHistory = 'PriceHistory'
}

class WidgetDescription {
    private name: Widgets;

    constructor(name: Widgets) {
        this.name = name;
    }

    get Name(): Widgets {
        return this.name;
    }
}

// code implementation
let widgetDescription: WidgetDescription = new WidgetDescription(Widgets.ComparableHomeSales);
let widgetName: string = widgetDescription.Name;

if (widgetName == Widgets.ComparableHomeSales) {
    widgetName += 'AddSomething'; // <-- ERROR
}

Expected behavior:

widgetName += 'AddSomething'; should work

Actual behavior:

widgetName += 'AddSomething'; does not work

Playground Link:
Link

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

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

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

はじめの一歩

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

microsoft/TypeScript のほかの issue

microsoft/TypeScript の issue をすべて見る

似ている issue

Go の issue をもっと見る

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

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