object index signatures

김범주·2022년 7월 18일
0

typescript

목록 보기
21/23
post-thumbnail
interface MyType {
  'font-size' : number,
  [key :string] : number | MyType,
}

let obj = {
  'font-size' : 10,
  'secondary' : {
    'font-size' : 12,
    'third' : {
      'font-size' : 14
    }
  }
}

이런식으로 짜면 object가 연속되는 경우에도 에러없이 작성이 가능

interface StringOnly {
  age : number,   ///가능
  [key: string]: string | number,
}
-------------------------------------------------------------------------------
interface StringOnly {
  [key: string]: string
}

let obj :StringOnly = {
  name : 'kim',
  age : '20',
  location : 'seoul'
}

이런식으로 사용가능!

profile
개발꿈나무

0개의 댓글