본문 바로가기

Web_Study/TypeScript

TypeScript_10:Summary

 Summary_Generic & Interface & Polymorphism

 

※  Generic : Generic은 type을 concrete가 아닌 placeholder type으로 받아줌

  • 같은 코드를 다른 타입에 대해서 쓸 수 있도록 해줌

Example API

//이미 선언된 자바스크립트의 웹스토리지 API를 위한 interface


//나만의 interface를 가지기 위해 SStorage로 이름 명명! 
//Storage는 존재하는 자바스크립트의 웹스토리지 API
interface SStorage<T>{
	[key:string]: T
}

class LocalStorage<T>{
	private storage:SStorage<T> = {}
    
    //API디자인 구현을 보여주기 위함
    set(key:string, value:T){
        this.storage[key]=value;
    }
    remove(key:string){
    	delete this.storage[key]
    }
    get(key:string):T {
    	return this.storage[key]
    }
    clear(){
    	this.storage = {}
    }
}

const stringStorage = new LocalStorage<string>()
stringStorage.get("key")
//generic을 바탕으로 callsignature 생성해줌

const booleanStorage = new LocalStorage<boolean>();
booleanStorage.get("xxx")

 

generic으로 string 생성
generic으로 boolean 생성

'Web_Study > TypeScript' 카테고리의 다른 글

TypeScript_Project2:Lib Configuration  (0) 2022.07.11
TypeScript_Project1:Target  (0) 2022.07.01
TypeScript_9:Interfaces_2  (0) 2022.06.14
TypeScript_9:Interfaces  (0) 2022.06.10
TypeScript_8:Class_2  (0) 2022.06.10