extends (2) 썸네일형 리스트형 Generic 오늘은 TypeScript에서 Generic을 어떻게 사용하는지에 대해 알아보겠다. Genric은 단일 타입이 아닌 다양한 타입에서 작동하는 function, class, interface를 만들 때 사용된다. 간단히 다음 예시를 살펴보자! function printValGeneric(val: T): T { console.log(val); return val; } const a = printValGeneric('hi'); const a2 = printValGeneric('hi'); 'printValGeneric' 함수는 입력받은 값을 출력 및 반환하는 함수이다. 먼저 문법부터 살펴보면 함수를 선언할 때 'T'라는 타입변수를 로 감싸서 입력받고 있다. 또한 'T' 타입의 인수 'val'을 입력받고 있고, .. Type의 확장 Typescript에서 타입지정을 위해 Type alias와 Interface를 사용할 수 있는데, 이렇게 지정된 type들을 확장하는 기능도 지원하고 있다. 어떻게 확장할 수 있는지 배워보도록 하자! Intersection interface animal { readonly name: string; age: number; } type bird = animal & { wing: number; } type alias 기능을 사용할 때는 &를 사용해 연산하듯이 두 타입을 합칠 수 있다. 이를 `intersection`이라고 부르고, interface나 type alias로 지정된 타입을 모두 결합할 수 있다. 상속 interface animal { readonly name: string; age: number.. 이전 1 다음