728x90
반응형

Polymorphism 다형성 : 여러 타입을 받아들임으로써 여러 형태를 가지는 것. generic type

  • concrete type : number, boolean 등 명시가 가능한 타입
  • generic type : 타입의 placeholder. replace가 가능한 것

다양한 경우의 인자를 수용하는 함수를 작성할 때, 모든 경우의 Call Signature를 작성하는 것은 어렵기 때문에 다형성을 가질 수 있는 generic type을 이용한다.

generic type : 제네릭은 선언 시점이 아니라 생성 시점에 타입을 명시하여 하나의 타입만이 아닌 다양한 타입을 사용할 수 있도록 하는 기법

내가 함수를 사용한 방식을 토대로 Call Signature를 생성한다는 점에서 any와 다르다

type SuperPrint={
  <T>(arr:T[]):T;
}

const superPrint:SuperPrint=(arr)=>{return arr[0]}

// 내가 함수를 사용한 방식을 토대로 Call Signature를 생성한다.
const a=superPrint([1,2,3])
const b=superPrint([true,false,true])
const c=superPrint(["a","b"])
const d=superPrint([1,2,"a","b",true])

 

 

728x90
반응형

'IT > Typescipt' 카테고리의 다른 글

Typescript Classes, Interface  (0) 2022.11.06
Typescript Functions - Call Signatures, Overloading  (0) 2022.11.05
Typescript Foundation  (0) 2022.11.05

+ 최근 게시글