지난 번에는 객체 파라미터를 적용시키기 위하여 interface라는 개념을 도입했다.
하지만 interface는 javascript로 compile 시킬 때에 반영이 되지 않는다.
따라서, compile 후 react 등과 같이 기타 라이브러리에서 사용하고자 한다면
typescript에서부터 class로 정의하는 것이 좋다.
index.ts
class Human {
public name: string;
public age: number;
public gender: string;
constructor(name: string, age: number, gender: string) {
this.name = name;
this.age = age;
this.gender = gender;
}
}
// class의 privacy 설정 중 public과 private은 차이가 있다.
// private으로 설정 시에는, 클래스의 외부에서 해당 항목에 대하여 접근 및 호출이 불가하다.
// 따라서, 외부에서 사용할 것이라면 public으로 설정한다.
const areum = new Human("areum", 30, "Female");
// areum이라는 이름의 Human instance를
const sayHi = (param: Human): string => {
return (`Hello ${param.name}, you are ${param.age}, you are a ${param.gender}!`)
};
console.log(sayHi(areum)); // "Hello areum, you are 30, you are a Female"
export {};
| Typescript로 블록체인 만들기 4) Block 만들기 (0) | 2019.04.28 |
|---|---|
| Typescript로 블록체인 만들기 1) Typescript 환경 설정과 compiling - Typescript (0) | 2019.04.26 |
댓글 영역