// Words 라는 타입은 string 타입의 프로퍼티를 가지고, value 타입도 string 인 Object
type Words = {
[key: string]: string;
};
class Dict {
// property가 constructor부터 바로 초기화되지 않고 아래 줄에서 수동으로 초기화 시킴
private words: Words;
constructor() {
this.words = {};
}
// Word 클래스를 마치 타입처럼 사용함
// 파라미터 부분에 클래스를 입력해 준 적이 없지만, 클래스를 타입으로 쓸 때는 가능...
// 앞의 word 라는 파라미터가 이 Word 클래스의 인스턴스이기를 원할 때 이렇게 사용 가능
add(word: Word) {
if (this.words[word.term] === undefined) {
this.words[word.term] = word.def;
}
}
def(term: string) {
return this.words[term];
}
}
class Word {
constructor(public term: string, public def: string) {}
}
const kimchi = new Word('kimchi', '한국의 음식');
const dict = new Dict();
dict.add(kimchi);
dict.def('kimchi');

근데… js에서는 접근 잘만되고 잘 출력됨 ㅋㅋ..
console.log(dict.def('kimchi'));
console.log(dict.words);
console.log(dict.words.kimchi);
// 출력결과
한국의 음식
{ kimchi: '한국의 음식' }
한국의 음식