microsoft / microsoft/TypeScript

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

Abierto
#35,824 0 comentarios 1 reacción 1 asignado Ver en GitHub

@elibarzilay ya está trabajando en esto.

Desde el 13/1/2020.

Bug Domain: check: Control Flow Rescheduled
Lenguaje dominante
Go
Estrellas
111k
Forks
14.4k
Merge medio
1 d 19 h
PR fusionados (30 d)
117

Descripción

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

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Evaluación

Este issue todavía no se ha evaluado.

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.