TIL
152. [TypeScript] TS 설치하기 TIL 23.05.09
새싹_v
2023. 5. 9. 18:30
728x90
TypeScript 설치하기
1. npm init -y
//package.json
{
"name": "2",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
2. npm install typescript
3. npx tsc --init
Created a new tsconfig.json with:
TS
target: es2016
module: commonjs
...생략...
target : 어떤 JS버전으로 변환할 것인지 설정
module : import 문법을 어떻게 변환할 것인가
- commonjs는 import 문법을 require 문법으로
- es2015, esnext 는 import 문법으로
설치하면 아래 사진과 같이 생성된다.
TS 👉 JS 전환
// ts -> js로 전환 후 실행
npx tsc
hello.ts 👉 hello.js 전환 후 실행
//위 과정 없이 TS코드 바로 실행
npx ts-node hello.ts
브라우저는 TS를 이해 못하기 때문에 TS 👉 JS 변환해줘야 한다.
728x90