TypeScript Compiler

LOOPY·2021년 8월 18일
post-thumbnail

1. Compilation Context(컴파일 맥락)

  • 어떤 파일을 컴파일 할건지, 어떤 컴파일 옵션을 사용할건지 (대상과 방식)
  • tsconfig.json에 저장

2. tsconfig.json

1) compileOnSave

  • 파일을 저장과 동시에 컴파일
  • default값은 false

2) extends

  • 값으로 다른 json파일 경로를 적으면 해당 파일 내용을 상속받아 적용

3) files, include, exclude

  • file이나 include 속성이 없으면 전체 파일을 컴파일
  • 영향력은 include < exclude < files
    (exclude로 배제되어있어도 files에 포함되어있으면 컴파일 됨)
  • exclude 설정이 없으면 node_modules, bower_components, jspm_package, 4개 파일만 default로 제외
    (이 중 outDir은 include해도 제외됨)

4) compileOptions

  • typeRoots와 types
    • 설정이 없으면 node_modules/@types라는 모든 경로 찾아 사용
    • typeRoots 사용하면 배열 안에 들어있는 경로들 아래서만 가져옴
    • types 사용하면 배열 안의 모듈 혹은 ./node_modules/@types/ 안의 모듈 이름에서 찾아옴 ([]빈 배열 넣으면 이 시스템 사용하지 않겠다는 의미)
      ⭐ typeRoots와 types는 같이 사용하지 않기
  • target과 lib
    • target: 빌드 결과물의 버전 지정(default ES3)
    • lib: 기본 type definition 라이브러리 지정(default는 버전에 따라) -> 지정된 배열 안에서만 라이브러리 사용하게 됨
  • outDir, outFile, rootDir
    • rootDir 지정하지 않으면 가장 상단의 ts파일을 컴파일
    • rootDir 경로 지정하면 해당 폴더 내 파일을 우선으로 컴파일
  • 🌟 strict (꼭! 사용하기)
    • noImplicitAny, noImplicitThis, strictNullChecks, strictFunctionTypes, strictPropertyInitialization, strictBindCallApply, alwaysStrict 옵션 전부 켜는 효과
    • noImplicitAny 옵션: 명시적이지 않게 any타입을 표현식과 선언에 사용하면 에러 발생
      +) suppressImplicitAnyIndexErrors 옵션: noImplicitAny 사용할 때 인덱스 객체에 인덱스 signature가 없는 경우 오류 발생하는데, 이를 예외처리 해줌
    • noImplicitThis 옵션: 명시적이지 않게 any타입을 this 표현식에 사용하면 에러 발생
    • strictNullChecks 옵션: null과 undefined가 자신이나 any에게만 할당이 가능하게 함
      -> 해당 값을 가질 수 있는 경우 union type 활용하기
      (예외: undefined에 void 할당 가능)
    • strictFunctionTypes 옵션: 함수 타입에 대한 bivariant 매개변수 검사를 비활성화
      -> 인자타입을 반공변적으로
    • strictPropertyInitialization 옵션: 정의되지 않은 클래스의 속성이 생성자에서 초기화되었는지 확인 / strictNullChecks와 같이 사용해야 함
    • strictBindCallApply 옵션: bind, call, apply에 대한 더 엄격한 검사 수행
    • alwaysStrict 옵션: 각 소스파일에 대해 JavaScript의 scrict mode로 코드를 분석하고 엄격하게 사용을 해제
profile
2년차 프론트엔드 개발자의 소소한 기록을 담습니다 :-)

0개의 댓글