'React: No index signature with a parameter of type 'string' was found on type N

I have a React component, I'm trying to access a property from my component state by key but I'm getting a warning from my editor:

Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'Item'. No index signature with a parameter of type 'string' was found on type 'Item'.

const [form, setForm] = useState<Item | null>(null);


type ItemType = {
  key: string,
  value: string,
  size: string[]
}

const items: ItemType[] = [
  {
    key: 'my_key_1',
    value: 'my_value_1',
    size: ['S', 'M']
  },
  {
    key: 'my_key_2',
    value: 'my_value_2',
    size: ['S']
  },
  {
    key: 'my_key_3',
    value: 'my_value_3',
    size: ['S', 'L']
  },
];

items.forEach(item => {

  // this line will give me a warning, 
  // but if I pass a string directly form?.['my_key_2'] it will not complain about it
  console.log(form?.[item.key]);
});


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source