프로젝트/[typescript]typeChain

[typeChain 1] 타입스크립트란?

samsaraA 2018. 7. 18. 16:45


typescript로 간단한 blockChain을 만든다.

typeScript란 ? superset of javaScript.

자바스크립트는 엄격한 규칙이 없어서 수정해서 사용하기 쉬운 장점을 가지고있다.

하지만 팀프로젝트를 한다던가, 큰 프로젝트를 한다던가 버그를 최소화하고 싶을땐 단점이 된다.

타입스크립트는 자바스크립트에 없는 규칙들을 가지고있다.

언어를 예측가능하게 하고, 읽기 쉽게 하여 자바스크립트를 더 잘 활용할 수 있게한다.



git repo 생성 , 세팅

https://github.com/samsara1019/typechain
git clone https://github.com/samsara1019/typechain
git remote add origin https://github.com/samsara1019/typechain
yarn init


typescript 다운로드

yarn global add typescript




tsconfig.json

//typescript에게 어떻게 javascript로 변환할지 옵션을 주는 파일
{
"compilerOptions": {
"module" : "commonjs", //node.js를 평범하게 사용하고, 다양한걸 import, export 할 수 있게함.
"target" : "es2015",//어떤 버전의 javascript로 컴파일할지
"sourceMap": true //sourcemap 처리를 하고싶은지
},
//어떤 파일이 컴파일 과정에 포함되는지 알려주는 부분
"include": ["index.ts"],
"exclude": ["node_modules"]
}


설명을 위해 주석을 달아놨지만, json파일에는 주석이 권장되지 않기 때문에 실제 파일에서는 지웠다.



소스맵?

설명이 부족해서 찾아봤는데, 원본소스와 변경된 소스를 매핑해준다.

자바스크립트를 압축했을때, 디버깅이 어려운 문제를 해결

소스맵의 스펙





index.ts

alert("hello");



ts->js로 변환해보자


터미널에서

tsc

를 해주면 변환된 파일이 생긴다.




yarn start

터미널에서 yarn start를 하면 tsc = 타입스크립트 변환, node index.js = index.js 실행 까지 되게 설정


package.json

"scripts" : {
"start" : "node index.js",
"prestart" : "tsc"
}




index.ts 를

console.log("hello");

로 바꾸고 yarn start 해보면 콘솔에 hello가 뜬다.