티스토리 뷰

TypeScript

[TypeScript] Deep Types

Dongi 2022. 1. 23. 23:54

 

 

 

안녕하세요 동기 여러분! 오늘은 중첩 메서드의 인터페이스를 작성하는 방법에 대해 알아봅시다!

 

 

 

 

 

 

Deep Types

아래의 코드를 봅시다.

interface Characters {
    heros: {
        blackWidow: {
            alias: string;
            gender: string;
            height: number;
        }
    }
}

class Marvel implements Characters{
    heros = {
        blackWidow: {
            alias: 'Black Widow',
            gender: 'female',
            height: 170
        }
    }
}

const infoBW = new Marvel();

console.log(infoBW.heros);

인터페이스

  • Characters라는 인터페이스를 만들고
  • hero안에 blackWidow안에 alias, gender, height 프로퍼티들을 만들고 값으로 타입들을 넣어줍니다. interface는 타입 선언이기 때문에 : (colon)을 사용하면 됩니다.

 

클래스

  • Marvel이라는 클래스를 만들고 implements로 Characters를 가져와서 타입 검사를 준비해둡니다.
  • hero는 프로퍼티 어사인먼트를 선언하기 위해 옆에 = (equal)을 사용합니다. blackWidow는 프로퍼티이기 때문에 옆에 : (colon)을 사용하고 alias, gender, height는 blackWidow의 값이자 프로퍼티이기 때문에 옆에 : (colon)을 사용해줍니다.

 

출력

  • heros를 호출하기 위해 infoBW라는 변수에 new Marvel();로 Marvel의 모양의 새로운 객체를 만들어 선언합니다.
  • 출력문 infoBW.heros 

 


 

오늘의 느낌

느낌적인 느낌

'TypeScript' 카테고리의 다른 글

[TypeScript] Extending Interfaces  (0) 2022.01.24
[TypeScript] Composed Types  (0) 2022.01.24
[TypeScript] Interfaces and Classes  (0) 2022.01.23
[TypeScript] Interfaces and Types  (0) 2022.01.23
[TypeScript] Narrowing After a Type Guard  (0) 2022.01.23
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/09   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
글 보관함