//index signature 문법
interface StringOnly {
[key : string] : string
//모든 문자로된 속성은 string 타입이다.
}
const car이다:StringOnly = {
name : 'carName',
age : '32', //string이어야 한다.
}
//깊은 object의 type (Recursive) : 쓸 일이 거의 없다.
interface TypeFont {
'font-size': TypeFont|number
}
const cssFont:TypeFont = {
'font-size':{
'font-size':{
'font-size' : 123
}
}
}
// 조건부 타입변환기(keyof 연산자)
type Car1 = { //type을 잘못 지정한 경우
color : boolean,
model : boolean,
price : boolean | number
}
type TypeChanger<MyType> = { //생성
[key in keyof MyType] : string
}
type NewType = TypeChanger<Car1> //실행 : 전부 다 string 으로 변한다.
const realCar:NewType = {
color : 'red',
model: 'super',
price : '얼마'
}
'Frontend > TypeScript(완)' 카테고리의 다른 글
[TS] TS연습하기16 : 다이나믹 타입 지정(완) (0) | 2023.04.13 |
---|---|
[TS] TS연습하기14 : d.ts (0) | 2023.04.13 |
[TS] TS연습하기13 : declare (0) | 2023.04.13 |
[TS] TS연습하기12 : array와 tuple, rest parameter (0) | 2023.04.13 |
[TS] TS연습하기11 : React와 타입스크립트(TSX) (0) | 2023.04.13 |
댓글