microsoft / microsoft/TypeScript

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

Aperta
#35,824 0 commenti 1 reazione 1 assegnatario Vedi su GitHub

@elibarzilay ci sta già lavorando.

Dal 13/1/2020.

Bug Domain: check: Control Flow Rescheduled
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.