call object value by variable

Roman to Integer 테스터 만드는 중 오브젝트를 이름으로 불러오는 방법

Element implicitly has an ‘any’ type because expression of type ‘string’ can’t be used to index in ReactJs
const romanChart = {
  I: 1,
  V: 5,
  X: 10,
  L: 50,
  C: 100,
  D: 500,
  M: 1000,
};

...

let number = romanChart[singleRoman];        //에러 발생

...


This happens because you try to access romanChart property using string name. TypeScript understands that name may have any value, not only property name from romanChart. So TypeScript requires to add index signature to romanChart, so it knows that you can use any property name in romanChart.

해결책

type romanChart = {
  [key: string]: number,
};