Typescript는 기본적으로는 Javascript와 다르지 않다. 하지만 Typescript 파일을 Javascript 파일로 변환하는 과정(compiling)이 있어야 Nodejs 등에서 인식을 할 수 있으므로, 그에 필요한 사전설정이 필요하다.
typescript를 사용하기 위한 설치 방법은 다른 사람들이 잘 정리해놓아서 따로 기재하진 않겠다.
package.json
{
"name": "typechain",
"version": "1.0.0",
"description": "Learing Typescript by making a Blockchain with it",
"main": "index.js",
"repository": "https://github.com/swoo1226/typechain.git",
"author": "phot_o_matic",
"license": "MIT",
"scripts": {
"start": "node index.js", //yarn start 실행 시, prestart가 먼저 실행된다.
"prestart": "tsc" //tsc는 ts 파일을 js와 js.map 파일로 변환시키는 명령어이다.
}
}
변환시켜줄 ts 파일의 기초적인 문법은 다음과 같다.
index.ts
const name = "Sangwoo",
age = 29,
gender = "male"; //변수 및 값 설정
const sayHi = (name, age, gender?) => {
console.log(`Hello ${name}, you are ${age}, you are a ${gender}`)
};
sayHi(name, age) //"Hello Sangwoo, you are 29, you are a undefined"
// 함수 설정 시 parameter 뒤에 "?"를 붙이면, 이 후 함수 호출 시 parameter 입력을 선택적으로 할 수 있다.
// 만약 "?" 없이 parameter를 정의하고, 호출 시에 제외하게 되면 아예 compiling 자체가 불가능하다고 메시지가 뜬다.
// 또한 어떤 부분의 parameter 누락인지도 알려준다.
sayHi(name, age, gender);
export {};
| Typescript로 블록체인 만들기 4) Block 만들기 (0) | 2019.04.28 |
|---|---|
| Typescript로 블록체인 만들기 3) class와 private, public (0) | 2019.04.26 |
댓글 영역