1. tsconfig.json
- 타입스크립트 ts 파일들을 .js 파일로 변환할 때 어떻게 변환할 것인지 세부설정하는 파일이다.
2. 기본 세팅
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
}
}
- target : 어떤 버전의 자바스크립트로 바꿔줄 지 정한다.
- ex) es5로 셋팅하면 es5버전 자바스크립트로 컴파일
- module : 자바스크립트 파일간 import 문법 구현할 때 어떤 문법을 쓸지 정한다.
- ex) commonjs는 require문법
- es) es2015, esnext는 import문법
- 보통 es5, commonjs로 세팅을 많이 한다.
3. 추가 세팅
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"noImplicitAny": true,
"strictNullChecks": true
}
}
noImplicitAny : any라는 타입이 의도치않게 발생할 경우 에러를 띄워주는 설정
strictNullChecks : 기본적으로 null과 undefined는 다른 타입에 다 부여할 수 있다. 이렇게 하면 코드를 작성하는 것은 쉽지만 의도치 않게 null과 undefined가 부여될 수 있다. 해당 옵션을 설정하면 더욱 분명하게 핸들할 수 있다.
4**. 전체 세팅**
{
"compilerOptions": {
"target": "es5", // 'es3', 'es5', 'es2015', 'es2016', 'es2017','es2018', 'esnext' 가능
"module": "commonjs", //무슨 import 문법 쓸건지 'commonjs', 'amd', 'es2015', 'esnext'
"allowJs": true, // js 파일들 ts에서 import해서 쓸 수 있는지
"checkJs": true, // 일반 js 파일에서도 에러체크 여부
"jsx": "preserve", // tsx 파일을 jsx로 어떻게 컴파일할 것인지 'preserve', 'react-native', 'react'
"declaration": true, //컴파일시 .d.ts 파일도 자동으로 함께생성 (현재쓰는 모든 타입이 정의된 파일)
"outFile": "./", //모든 ts파일을 js파일 하나로 컴파일해줌 (module이 none, amd, system일 때만 가능)
"outDir": "./", //js파일 아웃풋 경로바꾸기
"rootDir": "./", //루트경로 바꾸기 (js 파일 아웃풋 경로에 영향줌)
"removeComments": true, //컴파일시 주석제거
"strict": true, //strict 관련, noimplicit관련 모드 전부 켜기
"noImplicitAny": true, //any타입 금지 여부
"strictNullChecks": true, //null, undefined 타입에 문제 생기면 에러내기
"strictFunctionTypes": true, //함수파라미터 타입체크 강하게
"strictPropertyInitialization": true, //class constructor 작성시 타입체크 강하게
"noImplicitThis": true, //this 키워드가 any 타입일 경우 에러내기
"alwaysStrict": true, //자바스크립트 "use strict" 모드 켜기
"noUnusedLocals": true, //쓰지않는 지역변수 있으면 에러내기
"noUnusedParameters": true, //쓰지않는 파라미터 있으면 에러내기
"noImplicitReturns": true, //함수에서 return 빼먹으면 에러내기
"noFallthroughCasesInSwitch": true, //switch문 이상하면 에러내기
}
}
strict를 다 켜고 해야 엄격한 체크를 받을 수 있다.