Index Signature : { [Key: T]: U } 형식으로 객체가 여러 Key를 가질 수 있으며, Key와 매핑되는 Value를 가지는 경우 사용합니다.

// 인터페이스(Interface)
// 인덱스 가능 타입 - 인덱스 시그니처(Index Signature)

// 배열 데이터의 타입을 지정해 줄 수 있다.
interface Fruits {
  // item의 타입은 number로 지정되야 한다.
  // 속성의 값으로는 문자열이 와야된다.
  [item: number]: string;
}

const fruits: Fruits = ['Apple', 'Banana', 'Cherry'];
console.log(fruits); // 배열 0,1,2번째 인덱스 => nunber로 지정되어 있다.

// 객체
interface User3 {
  [key: string]: unknown;
  name: string;
  age: number;
}

const heropy2: User3 = {
  name: 'Heropy',
  age: 85,
};

// 객체 데이터에 대괄호 표기법을 통해 데이터 추가.
// 문자 인덱싱을 통해 데이터를 넣고 있다.
// 인덱싱 시그니처를 통해 데이터를 넣는게 가능하다.
heropy2['isValid'] = true;
heropy2['emails'] = ['[email protected]', '[email protected]'];
console.log(heropy2);

interface Payload {
  [key: string]: unknown;
}

function logValues(payload: Payload) {
  for (const key in payload) {
    console.log(payload[key]);
  }
}
// 인덱스 시그니쳐가 있어서 에러가 발생하지 않는다.
// 인덱스 시그니쳐가 없다면 에러 발생
logValues(heropy2);