목차

, , , ,

tsc

tsc는 TypeScript 소스의 타입을 검사하고 설정에 따라 JavaScript와 선언 파일을 생성하는 TypeScript compiler CLI이다.

Summary

Installation

Debian / Ubuntu, RHEL / Fedora, macOS, Windows에서 Node.js와 npm을 준비한 뒤 프로젝트에 설치한다.

npm install --save-dev typescript
npx tsc --version

Yarn 또는 pnpm 프로젝트에서는 같은 패키지를 개발 의존성으로 추가한다.

yarn add --dev typescript
yarn tsc --version
 
pnpm add --save-dev typescript
pnpm exec tsc --version
TypeScript upstream은 tsc 자체에 대한 별도 APT, DNF/YUM, Homebrew, winget 설치 절차를 제공하지 않는다. 각 OS에서 공식적으로 지원되는 Node.js 설치를 준비한 다음 프로젝트 패키지 매니저로 설치한다.

Global installation

기존 전역 설치 명령은 일회성 실험이나 시스템 전체에서 직접 tsc를 호출할 때 사용할 수 있다. 장기 유지 프로젝트에는 로컬 설치를 우선한다.

npm install --global typescript
tsc --version

Windows MSBuild (optional)

Visual Studio의 MSBuild 기반 프로젝트는 NuGet Package Manager Console에서 compiler 패키지를 설치할 수 있다. Node.js client 설치와는 별도 경로이다.

Install-Package Microsoft.TypeScript.MSBuild

Usage

npx tsc
npx tsc src/index.ts
npx tsc --project tsconfig.production.json
npx tsc --noEmit
npx tsc --watch
npx tsc --build

Options

CLI commands

Build and watch

–build –clean은 project reference 설정에 기록된 출력물을 삭제한다. 먼저 –build –clean –dry로 대상을 확인한다.

Common compiler options

Examples

Initialize and type-check a project

npm install --save-dev typescript
npx tsc --init
npx tsc --noEmit

Compile a specific project

npx tsc --project ./tsconfig.production.json

Compile files directly

npx tsc src/index.ts src/worker.ts --target es2022 --module nodenext --outDir dist
파일 이름을 직접 넘기면 가까운 tsconfig.json을 사용하지 않는다. 프로젝트 설정이 필요하면 파일 목록 대신 –project를 사용한다.

Generate declarations only

npx tsc src/index.ts --declaration --emitDeclarationOnly --outDir types

Build project references

npx tsc --build --verbose
npx tsc --build --dry

Config

tsconfig.json의 위치, 주요 필드, 상속, project reference, 검증 방법은 tsconfig.json 구성을 참고한다.

Help

검증 환경에 tsc가 설치되어 있지 않아 특정 버전의 도움말 원문은 수록하지 않았다. 프로젝트에 설치된 버전을 기준으로 확인한다.

npx tsc --help
npx tsc --help --all
npx tsc --version

Troubleshooting

tsc command not found

프로젝트 로컬 설치는 실행 파일을 전역 PATH에 추가하지 않는다. 프로젝트 루트에서 npx tsc, pnpm exec tsc, 또는 yarn tsc를 사용한다.

Expected tsconfig options are ignored

명령줄에 .ts 파일을 직접 지정했는지 확인한다. 설정 파일을 반드시 적용하려면 다음처럼 실행한다.

npx tsc --project ./tsconfig.json --showConfig

Unexpected files are compiled

최종 설정과 대상 파일을 차례로 확인한다.

npx tsc --showConfig
npx tsc --listFilesOnly

Compiler version differs between machines

전역 tsc 대신 프로젝트의 typescript 개발 의존성을 lockfile에 기록하고 package manager를 통해 실행한다.

Compatibility

See Also

History