타입스크립트(TypeScript) - 실행 환경 만들기
정적(Static) 타입인 타입스크립(TypeScript)는 동적(Dynamic) 타입인 자바스크립트(JavaScript)에서 발생할 수 있는
타입 에러를 방지하고, 타입이 정해져 있어 코드 완성 기능을 사용할 수 있어 효율성을 높여주는 효과가 있습니다.
1. Node.js 설치
타입스크립트를 사용하기 위해서는 Node.js가 설치되어 있어야합니다.
Node.js
Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript engine.
nodejs.org
정상적으로 설치가 끝났다면 다음 명령어를 이용해 버전을 확인 할 수 있습니다.
node -v
2. TypeScript 설치
타입스크립는 NPM 패키지 형태로 다음 명령어를 이용해 설치합니다.
typescript
TypeScript is a language for application scale JavaScript development. Latest version: 4.8.4, last published: a day ago. Start using typescript in your project by running `npm i typescript`. There are 36043 other projects in the npm registry using typescri
www.npmjs.com
npm i typescript -g
정상적으로 설치가 끝났다면 다음 명령어를 이용해 버전을 확인 할 수 있습니다.
tsc -v
3. tsconfig 생성
tsconfig는 타입스크립트의 각종 환경설정이 담겨있는 파일로 타입스크립트 컴파일에 필수입니다.
tsc --init
4. 타입스크립트 컴파일
타입스크립트를 실행하는 방법은 2가지가 있습니다.
1. tsc 명령어로 자바스크립트 파일로 컴파일 후 Node.js로 실행.
2. TS-Node로 실행.
어떤 방법으로 하셔도 무방합니다.
4-1. TS-Node 사용
TS-Node를 설치하고 다음 명령어로 실행합니다.
ts-node
TypeScript execution environment and REPL for node.js, with source map support. Latest version: 10.9.1, last published: 2 months ago. Start using ts-node in your project by running `npm i ts-node`. There are 6788 other projects in the npm registry using ts
www.npmjs.com
ts-node test.ts
4-2. Node.js로 실행
타입스크립트 파일을 컴파일합니다.
tsc test.ts
컴파일된 자바스크립트 파일을 Node.js로 실행합니다.
node test.js