microsoft / microsoft/TypeScript

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

Offen
#35,824 0 Kommentare 1 Reaktion 1 zugewiesene Person Auf GitHub ansehen

@elibarzilay arbeitet bereits daran.

Seit 13.1.2020.

Bug Domain: check: Control Flow Rescheduled
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
2 T. 4 Std.
Gemergte PRs (30 T.)
132

Beschreibung

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

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.