#3.2 Polymorphism
#3.3 Generics Recap
#3.4 Conclusions
Generic
// <>:제네릭 (타입스크립트가 작성한 코드를 보고 알아내서 작성한 코드 기준으로 바꿔준다.)
type SuperPrint = {
<JJW>(arr: JJW[]): JJW
}
const superPrint: SuperPrint = (arr) => arr[0];
//어떤 타입이 와도 받을 수 있다.
superPrint([1, 2, 3, 4]);
superPrint([true, false, true]);
superPrint(['a', 'b', 'c']);
superPrint([1, 2, true, false, 'hello']);
TypeScript
복사
//타입스크립트에게 첫 번째 파라미터로 배열이 올거라고 말해주고
//M은 두 번째 파라미터로 들어온다고 말해준다.
type SuperPrint = <T, M>(a: T[], b: M) => T
TypeScript
복사