JavaScript의 단점
- JavaScript는 동적 언어이기 때문에 변수의 타입이 실행시에 결정되어 오류가 발생하기 쉽고 찾기 까다로움
- 객체의 성질을 외부에서 수정 가능
TypeScript의 장점
- 안전성과 가독성 증가
- 언어에서 안정성을 보장하여 테스트 코드의 양이 감소
TypeScript 시작하기
npm install typescript -g
- 작업 공간에서 package.json과 tsconfig.json 생성
npm init -y
tsc --init
- tsconfig.json에서 자바스크립트 관련 기능을 주석을 제거하여 작동
/* JavaScript Support */
// "allowJs": true, /* JavaScript 파일을 허용. Use the 'checkJS' option to get errors from these files. */
// "checkJs": true, /* JavaScript 파일에 타입을 체크해서 에러 리포트를 띄움. */
// "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */
- 사용할 JavaScript 파일을 만든후 그 파일의 .d.ts 파일 생성
npx tsc 만든파일.js --declaration --allowJs --emitDeclarationOnly --outDir types
- 만든 JS파일을 참조할 TypeScript 파일을 만들어 실행
npx ts-node 또만든파일.ts