typescript 초기 설정

2022. 7. 7. 12:22카테고리 없음

/*

    # mkdir typechain

    # code typechain

    # npm init -y
    >> package.json 생성


    * package.json
    {
    "name": "type",
    "version": "1.0.0",
    "description": "",
    "main": "index.js",
    "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1"
    },
    "keywords": [],
    "author": "",
    "license": "ISC"
    }


    : main 삭제, scripts 수정
    : scripts 안의 내용물 삭제

    {
    "name": "type",
    "version": "1.0.0",
    "description": "",
    "scripts": {

    },
    "keywords": [],
    "author": "",
    "license": "ISC"
    }


    #npm i -D typescript  (-D >> 타입스크립트가 devDependencies에 설치됨)
    >> 타입스크립트 설치

    * package.json
    {
        "name": "type",
        "version": "1.0.0",
        "description": "",
        "scripts": {},
        "keywords": [],
        "author": "",
        "license": "ISC",
        "devDependencies": {
            "typescript": "^4.7.4"
        }
    }


    * package-lock.json
    "dependencies": {
    "typescript": {
        "version": "4.7.4",
        "integrity": "sha512-C0WQT0gezHuw6AdY1M2jxUO83Rjf0HP7Sk1DtXj6j1EwkQNZrHAg2XPWlq62oqEhYvONq5pkC2Y9oPljWToLmQ==",
        "dev": true
    }
   

    : src 폴더 생성 >> index.ts 파일 생성

    typescript 작성 -> 컴파일

    # touch tsconfig.json
    >> 작동 안되면 가장 바깥 폴더에 수동으로  tsconfig.json 파일 생성

    {
    "include": ["src"], // ts -> js 변환을 위해 src 폴더를 전부 확인한다.
    "compilerOptions": {
        "outDir": "build" // js 파일이 생성될 경로
    }

    // >> package.json에가서 build라는 script를 작성해야함
        "scripts": {
            "build":"tsc"
        },

    # npm run build >> build 폴더안에 index.js로 변환이 된다!

    * tsconfig.json에서 target 작성
    >> 원하는 버전의 JS으로 변환 가능
    {
    "include": ["src"], // ts -> js 변환을 위해 src 폴더를 전부 확인한다.
    "compilerOptions": {
        "outDir": "build", // js 파일이 생성될 경로
        // >> package.json에가서 build라는 새스크립트를 만들어야함
        "target": "ES6" // 변환될 JS 버전
    }
}
}
*/

'

 

package.json 에서 script 에 start 추가

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

powershell말고 bash에서 

# npm run build && npm run

 

# npm i -D ts-node

>> 빌드없이 타입스크립트 실행가능

 

#npm i nodemon 노드몬 설치

+ package.json

>>

"scripts": {
    "build": "tsc",
    "dev": "nodemon --exec ts-node src/index.ts",
    "start": "node build/index.js"
  },

 # npm run dev

>> console 에 실시간으로 찍힘