BaGyun
빠균's 개발노트
BaGyun
전체 방문자
오늘
어제
  • 분류 전체보기 (71)
    • Today I Learned (44)
      • 오류 (8)
      • JavaScript (15)
      • TypeScript (9)
      • NodeJS (0)
      • NestJS (0)
      • Database (3)
      • ORM (1)
      • 알고리즘 (2)
      • 개인공부 (4)
      • Deploy (0)
      • Git (1)
    • 개인프로젝트 (4)
    • 알고리즘 (14)
      • 프로그래머스 (14)
    • 면접질문 (5)
    • 회고 (3)

인기 글

반응형

블로그 메뉴

  • 홈
  • 태그
  • 방명록
hELLO · Designed By 정상우.
BaGyun

빠균's 개발노트

Today I Learned/TypeScript

[TypeScript] Nomad Coder - TypeScript 2주 챌린지(7일차)

2022. 8. 2. 16:17

다형성, 제네릭, 클래스, 인터페이스를 모두 합쳐보자.

 

다형성은 다른 모양의 코드를 가질 수 있게 해주는 것,

다형성을 이룰 수 있는 방법은 제네릭을 사용해야한다!

interface SStorage<T> {
  [key:string]: T  // key가 제한되지 않은 오브텍트를 정의하게 해줌
}

class localStorage<T> {
  private storage: SStorage<T> = {}
  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 stringsStorage = new LocalStorage<string>()

stringsStorage.get("ket")
stringsStorage.set("hello", "how are you")

const booleansStorage = new LocalStorage<boolean>()

booleansStorage.get("xxx")

'Today I Learned > TypeScript' 카테고리의 다른 글

[TypeScript] Nomad Coder - TypeScript 2주 챌린지(9일차)  (0) 2022.08.04
[TypeScript] Nomad Coder - TypeScript 2주 챌린지(6일차)  (0) 2022.08.01
[TypeScript] Nomad Coder - TypeScript 2주 챌린지(5일차)  (0) 2022.07.29
[TypeScript] Nomad Coder - TypeScript 2주 챌린지(4일차)  (0) 2022.07.28
[TypeScript] Nomad Coder - TypeScript 2주 챌린지(3일차)  (0) 2022.07.27
    'Today I Learned/TypeScript' 카테고리의 다른 글
    • [TypeScript] Nomad Coder - TypeScript 2주 챌린지(9일차)
    • [TypeScript] Nomad Coder - TypeScript 2주 챌린지(6일차)
    • [TypeScript] Nomad Coder - TypeScript 2주 챌린지(5일차)
    • [TypeScript] Nomad Coder - TypeScript 2주 챌린지(4일차)
    BaGyun
    BaGyun

    티스토리툴바