TypeScript

타입스크립트 설정

정중식 2019. 12. 1. 18:07
  • yarn global add typescript // 모든 결과물에 타입스크립트가 필요해서 글로벌로 설치해줬음
  • tsconfig.json 파일 생성 
    • 타입스크립트에게 어떻게 자바스크립트로 변환하는지 알려주고, 몇몇 옵션을 설정해주기위한 파일임
{
  "compilerOptions": {
    "module": "commonjs", // node.js를 평범하게 사용하고 다양한 걸 import하거나 export할 수 있게 만드는거임
    "target": "ES2015", // 어떤 버전의 javascript로 컴파일 되고 싶은지적는것
    "sourceMap": true
  }, // 다음으로 어떤 파일들이 컴파일 과정에 포함되는지 TypeSCript에게 알려줌
  // 컴파일 과정에서 포함할 파일의 배열을 적으면 됨
  "include": ["index.ts"],
  "exclude": ["node_modules"]
}

// 니콜라스가 프로젝트를 할때 해놓는 셋업들임
// 설정 심화
{
  "compilerOptions": {
    "module": "commonjs",
    "target": "ES2015",
    "sourceMap": true,
    "outDir": "dist" // 컴파일시 이 디렉토리에 js코드 파일 저장
  },
  "include": ["src/**/*"], // typescript 코드들을 대상으로 컴파일
  "exclude": ["node_modules"]
}


index.ts => index.js로 컴파일

  • 터미널에서 tsc입력하는 방법
    • .ts파일에 있는 코드를 컴파일해서 index.js,index.js.map 파일을 생성해줌
  • package.json의 scripts 설정하는 방법
 "scripts": {
    "start": "node index.js",
    "prestart": "tsc"
  }
  • Node.js는 타입스크립트를 이해하지 못하기때문에 일반적인 javascript 코드로 컴파일하는 작업이 필요