[Typescript] index signature

Woong·2023년 3월 22일
0

Typescript

목록 보기
1/2
  • 타입스크립트에서, 여러 key 를 가지고 key 에 value 가 존재하는 경우 index signature 를 사용할 수 있다.

    • [key:T]:U 형식으로 표현한다.
    • key 는 string, number, symbol, Template literal 만 허용된다.
  • ex) 국가코드를 Key 로 가지는 예제

interface MyInterface {
  country : {
    [key: string] {
      name: string    
    }
  }
}
  • 객체 선언하기
const data: MyInterface = {
  "KR": { name: "대한민국" },
  "US": { name: "미국" },
  "JP": { name: "일본" }	
}

0개의 댓글