Skip to content

for..in over array of string enum type casts to string type #19311

@garbles

Description

@garbles

TypeScript Version: 2.5.3

Code

Playground example here

enum Topping {
  Cheese = 'cheese',
  Olives = 'olives',
}

type Pizza = {
  toppings: Topping[];
};

const pizza = {
  toppings: [Topping.Cheese]
};

function calculateToppingPrice(topping: Topping): number {
  // ... whatever
  return 0;
}

let pizzaCost = 10;

for (let topping in pizza.toppings) {
  // Argument of type 'string' is not assignable to parameter of type 'Topping'.
  pizzaCost += calculateToppingPrice(topping);
}

Expected behavior:

The let topping in the for..in loop is of type Topping

Actual behavior:

The let topping in the for..in loop is of type string

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions