Typescript: for..in over array of string enum type casts to string type

Created on 18 Oct 2017  路  2Comments  路  Source: microsoft/TypeScript




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

Working as Intended

Most helpful comment

I think you meant to use a for-of loop. Thanks JavaScript...

All 2 comments

I think you meant to use a for-of loop. Thanks JavaScript...

@DanielRosenwasser ughuyghuhguh.... thank you!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kyasbal-1994 picture kyasbal-1994  路  3Comments

wmaurer picture wmaurer  路  3Comments

zhuravlikjb picture zhuravlikjb  路  3Comments

MartynasZilinskas picture MartynasZilinskas  路  3Comments

siddjain picture siddjain  路  3Comments