I am trying to apply template literal with optional chaining.
type Item = {
itemId:number,
price: number};
type ItemType = {
A:Item,
B:Item
};
const data : ItemType = {
A:{itemId:1, price:2},
B:{itemId:2, price:3}
};
let Itemid = `data?.${variable}?.itemId`
where variable is a string with A or B as value. I am not sure if optional chaining and template literals are supported together. Any Leads is appreciated.
Edited:
I am receiving "string cant be used to index type Item' when tried with data?.[variable]?.itemId. I have updated with type now.
Edited: Removing type of variable helped in solving above error message.