Recap

2022. 5. 22. 14:52·타입스크립트

해시맵 만들기. 사전 같은 거.

해싱 알고리즘을 쓰는 완벽한 해시맵이 될 거고 단어 사전을 만들어 보자.

type Word = {
	[key:string]: string
}

// 예시
let dict :Words = {
	"potato": "food"
}

property의 이름은 모르지만, 타입만을 알 때 사용한다.

제한된 양의 property 혹은 key를 가지는 타입을 정의해 주는 방법으로 object의 type을 선언 해야할 때 쓸 수 있다.

이 object는 제한된 양의 property만을 가질 수 있고 property에 대해서는 미리 알진 못하지만 타입만 알고 있을 때 쓰면된다. 

type Words = {
    [key:string]: string
}

class Dict {
    private words: Words
    constructor() {
        this.words = {}
    }
    add(word:Word) {
        if (this.words[word.term] === undefined) {
            this.words[word.term] = word.def;
        }
    }
    def(term:string) {
        return this.words[term]
    }
    // 단어 삭제
    del(term:string) {
        delete this.words[term]
    }
    // 업데이트
    update(key:string, newTerm:string) {
        this.words[key] = newTerm
    }
}

class Word {
    constructor(
        public term:string,
        public def :string,
    ) {
    // 단어의 정의를 추가하거나 수정하는 메서드, 단어를 출력하는 메서드
    }
}

const kimchi = new Word("kimchi", "한국의 음식");
const dict = new Dict()
dict.add(kimchi)
dict.def("kimchi")

 

 

 

 

'타입스크립트' 카테고리의 다른 글

[TS]인터페이스  (0) 2022.11.21
'타입스크립트' 카테고리의 다른 글
  • [TS]인터페이스
JoyYellow
JoyYellow
  • JoyYellow
    JoyYellow
    JoyYellow
  • 전체
    오늘
    어제
    • 분류 전체보기 (128)
      • Vue (7)
      • React (10)
      • 알고리즘 풀이 (29)
      • 타입스크립트 (2)
      • Microsoft (4)
      • TIL(Today I Learned) (16)
      • Devops (4)
      • CS(Computer Science) (2)
      • Spring (1)
      • Incomplete (0)
      • JS소스모듈 (10)
      • TDD (2)
      • 스프링부트 (0)
      • CSS (8)
      • Next.js (0)
  • 블로그 메뉴

    • 홈
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    노개북
    노마드코더
    개발자북클럽
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.1
JoyYellow
Recap
상단으로

티스토리툴바