이 글을 포스팅하게 된 이유는 TypeScript
관련 설정을 자주 할 일이 없었고, 볼 때마다 이 설정이 무슨 역할을 하는지 잊어버렸다. 따라서 나중에 내가 편하게 찾아보기 위해서 정리한다.
vite
로 react-ts 템플릿이 적용된 프로젝트를 생성하게 되면
yarn create vite 프로젝트-명 --template react-ts
다음과 같은 tsconfig.json
가 생성된다.
{
"compilerOptions": {
"target": "ESNext",
"lib": ["DOM", "DOM.Iterable", "ESNext"],
"module": "ESNext",
"skipLibCheck": true,
/* Bundler mode */
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx",
/* Linting */
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true
},
"include": ["src"],
"references": [{ "path": "./tsconfig.node.json" }]
}
각각 무슨 역할을 하는지 알아보자.
(아래에서 언급되는 “컴파일러”는 TypeScript 컴파일러를 의미한다.)
JavaScript
의 ECMAScript
버전이다.JavaScript
파일을 생성한다.ES3
이다.{
"compilerOptions": {
"target": "ES3" | "ES5" | "ES6" | "ES2016" | "ES2017" | "ES2018" | "ES2019" | "ES2020" | "ESNext"
}
}
DOM
키워드를 추가하면 DOM API
관련 타입 정보가 추가되어 컴파일러가 타입 검사하는데 사용할 수 있다.{
"compilerOptions": {
"lib ": ["DOM", "DOM.Iterable", "ESNext", ...]
}
}
{
"compilerOptions": {
"module ": "CommonJS" | "AMD" | "System" | "ES2015" | "ESNext"
}
}
skipLibCheck
의 옵션은 컴파일 시간과 라이브러리 호환성의 트레이드 오프가 존재한다.{
"compilerOptions": {
"skipLibCheck": true
}
}
bundler
로 설정한 경우 컴파일러에서 모듈 해석을 수행하지 않고 번들러가 처리한다.{
"compilerOptions": {
"allowImportingTsExtensions": "Classic" | "Node" | "bundler"
}
}
.ts
, .tsx
, .d.ts
확장자를 가진 파일만 컴파일한다.{
"compilerOptions": {
"allowImportingTsExtensions": true
}
}
JSON
파일을 모듈로 사용할 수 있게 하는 옵션이다.{
"compilerOptions": {
"resolveJsonModule": true
}
}
import
| export
키워드가 없으면 해당 파일은 전역 스코프를 갖는다.// file1.ts
export const message = 'Hello, world!';
// file2.ts
console.log(message);
{
"compilerOptions": {
"isolatedModules": true
}
}
JavaScript
파일을 생성하지 않고 오직 타입 검사로만 사용하는 옵션이다.{
"compilerOptions": {
"noEmit": true
}
}
jsx
파일을 어떻게 컴파일할지 정하는 옵션이다.react
: React.createElement
함수를 사용하여 컴파일한다.preserve
: jsx
코드를 그대로 유지한다.react-native
: React Native
에서 사용하는 JSX 컴파일러를 사용한다.{
"compilerOptions": {
"jsx": "react" | "preserve" | "react-native"
}
}
strictNullChecks
null
, undefined
를 허용하지 않는다.noImplicitAny
any
타입을 허용하지 않는다.strictFunctionTypes
type Func = (arg: string) => number;
const func: Func = (arg: any) => {
return arg;
}
strictBindCallApply
call
, apply
, bind
의 타입 검사를 엄격하게 한다.function add(a: number, b: number): number {
return a + b;
}
const partialAdd = add.bind(null, '1');
strictPropertyInitialization
// before
class Example {
value: number;
}
const ex = new Example();
console.log(ex.value); // undefined
// Class 'Example' incorrectly implements interface 'Example'.
// Property 'value' has no initializer and is not definitely assigned in the constructor.
// after
class Example {
value: number = 0;
}
const ex = new Example();
console.log(ex.value); // 0
alwaysStrict
use strict
가 적용된다.{
"compilerOptions": {
"strict": true
}
}
{
"compilerOptions": {
"noUnusedLocals": true
}
}
{
"compilerOptions": {
"noUnusedParameters": true
}
}
fallthrough
현상을 검사하는 옵션이다.fallthrough란,
switch
문의case
에서 다음case
로 넘어갈 때break
부재로 인해 발생하는 현상을 말한다.
{
"compilerOptions": {
"noFallthroughCasesInSwitch": true
}
}
src/**/*.ts
| src/**/*.js
{
"include": ["src/**/*.ts"]
}