[강의정리] 타입스크립트 다형성

김재만·2022년 8월 3일
0

타입스크립트 제네릭을 활용하여 placeholder 타입을 생성, 다형성 코드를 작성할 수 있다.

//인터페이스 생성
interface SStorage<T> {
	[key:string] : T
}

//클래스 생성
class LocalStorage<T> {
	private storage: SStorage<T> = {}
    set(key:string, value:T){}
    
    remove(key:string){
    	delete this.storage[key]
    }
    get(key:string):T{
    	return this.storage[key]
    }
    clear(){
    	this.storage = {}
    }
}

//다양한 타입의 인스턴스 생성
const stringStorage = new LocalStorage<string>();

const booleanStorage = new LocalStorage<boolean>();

interface Storage

  • 웹 스토리지 API에 접근하기 위한 타입스크립트 내장 인터페이스

마무리

알록달록 스토리지

profile
듣는 것을 좋아하는 개발자입니다

0개의 댓글