polymorphism
-
[ TypeScript ] 타입스크립트의 Classes 와 Interfaces.TIL 2024. 3. 13. 21:09
✅ Classes 추상(abstract)클래스 추상 클래스는 오직 다른 클래스가 상속 받을 수 있는 클래스이다. 하지만 직접 새로운 인스턴스를 만들 수는 없다. // classes abstract class User{ constructor( private firstname:string, private lastname:string, public nickname:string ){ abstract getNickname():void } } class Player extends User{ // 추상 메서드는 추상 클래스를 상속받는 클래스들이 반드시 구현(implement)해야하는 메서드이다. getNickname(){ console.log(this.nickname) } } ❗ 접근 가능한 위치 구분 선언한 클래스..
-
[ TypeScript ] 타입스크립트의 Polymorphism 과 Generic.TIL 2024. 3. 12. 21:38
✅ Polymorphism (다형성) type SuperPrint = { (arr: number[]): void, (arr: string[]): void, (arr: boolean[]): void, (arr: (number|string|boolean)[]): void } const superPrint: SuperPrint = (arr) => { arr.forEach(e => console.log(e)); } superPrint([1, 2, 3]) superPrint(["a", "b", "c"]) superPrint([true, false, true]) superPrint([1, "b", true]) => 위와 같이 다양한 경우를 커버하는 함수를 작성할 때, 모든 조합의 Call Signature를 con..