Fixing "Expression Can't Be Used To Index Type X"
Here we have an object named productPrices
. It has three properties: Apple, Banana, and Orange, each with a price value:
const productPrices = {
Apple: 1.2,
Banana: 0.5,
Orange: 0.8,
};
There is also a function getPrice
that takes in a product name as a parameter:
const g
Transcript
00:00 In this exercise, we have an object of product prices, apple, banana, and orange, and then we have a getPrice function. This, we have to pass in a product name here, and we return product prices product name. So we're indexing into product prices with the product name, which is string. And it says, element implicitly add on any type
00:19 because expression of type string can't be used to index type apple, banana, orange. No index signature with a parameter of type string was found on type apple, banana, orange. Okay, your job is to work out why this error is occurring and what we can do either in the types or at the runtime to figure this out.
00:38 In fact, I think this exercise only has types as its solution, so you don't need to do too much on the runtime. Yeah, good luck.