Sub topic 5
Compiling Your First File with `tsc`
Assume you know JavaScript functions and modules, can use a terminal, and have Node.js, npm, and a package.json file available. This chapter uses tsc, the TypeScript compiler, from a project-local installation.
What `tsc` does
TypeScript adds type information to JavaScript code. The compiler checks that information before producing JavaScript. Type annotations help tsc detect mistakes, but they do not remain in the generated JavaScript.
For example, this function accepts only strings:
function greet(name: string): string {
return `Hello, ${name}!`;
}
console.log(greet("Ada"));If you change the last line to console.log(greet(42)), tsc reports an error because a number does not match the parameter type string. Plain JavaScript would allow the call and might expose the problem only while running the program.
The compilation process has two separate results:
A successful check normally produces JavaScript. A failed check can still produce output unless you configure the compiler to prevent it.
Install TypeScript locally
Install TypeScript as a development dependency. A development dependency is a package needed to build or check a project, but not normally needed by the program in production.
mkdir ts-demo
cd ts-demo
npm init -y
npm install --save-dev typescript
npx tsc --versionThe npx command finds the compiler installed in this project. A local installation keeps the compiler version consistent across your team and continuous integration system.
Create a source directory and save the following as src/index.ts:
function greet(name: string): string {
return `Hello, ${name}!`;
}
console.log(greet("Ada"));Compile one file
Pass the source file and an output directory to tsc:
npx tsc src/index.ts --outDir dist
node dist/index.jsThe compiler writes dist/index.js. The generated file contains JavaScript, not the TypeScript annotation:
function greet(name) {
return "Hello, ".concat(name, "!");
}
console.log(greet("Ada"));The exact JavaScript syntax depends on the compiler's target setting and TypeScript version. The important result is that the type annotation has been erased.
When you pass a file directly on the command line, tsc uses command-line defaults and ignores tsconfig.json. This is useful for a quick experiment, but it is not a good project build command because options can become inconsistent.
Read compiler errors
Introduce an error temporarily:
function firstLetter(text: string | undefined): string {
return text.charAt(0);
}With strict checking enabled, the compiler reports that text may be undefined. The union type string | undefined means the value can have either type. Fix the problem by checking before calling charAt:
function firstLetter(text: string | undefined): string {
if (text === undefined || text.length === 0) {
return "";
}
return text.charAt(0);
}
console.log(firstLetter("TypeScript"));This illustrates the main development loop: write TypeScript, run tsc, read the diagnostic, correct the code, and run the compiler again.
A project command
Once a project has a tsconfig.json, run the compiler without a source filename:
npx tscThe configuration file records the source directory, output directory, module system, target JavaScript version, and strictness rules. That shared configuration makes local builds and automated builds behave the same way.
Exercise
What command compiles src/index.ts into the dist directory, and what command runs the result?
Show answer
Run npx tsc src/index.ts --outDir dist, then run node dist/index.js.