liveBook · Manning

TypeScript compiler

As explained in Secrets of the JavaScript Ninja, Third Edition

A tool that compiles TypeScript source code to JavaScript, optionally targeting a selected ECMAScript version.

The TypeScript compiler checks whether code follows its type rules and can emit JavaScript. Catching a mismatched argument before runtime helps avoid surprising behavior in JavaScript. section 1.3.1

const addOne = (x: number) => x + 1;  #A
addOne("10");  #B

The annotated parameter expects a number, so passing a string produces a compile-time error. section 1.3.1

Configure the compiler through tsconfig.json, which specifies:

  • Which TypeScript files to compile and which external types to include. section 2.1.1
  • How strictly to check types and whether to emit JavaScript; if emitting, which ECMAScript version to target. section 2.1.1
  • Run `npx tsc --init` to create a starter configuration, then `npx tsc` to check and compile the project. Type errors do not necessarily prevent JavaScript output. section 2.1.1

Type annotations are removed from emitted JavaScript rather than checked at runtime. The compiler can also transform syntax for a chosen ECMAScript target, but it does not add polyfills for missing runtime features. section 5.5.1 section 1.1.3

How Secrets of the JavaScript Ninja, Third Edition covers it

How deep it goes
Demonstrates installation, starter configuration, type errors, and output, but defers comprehensive tsconfig options.
Examples
Uses mismatched function arguments and an invalid setTimeout call to demonstrate compiler errors.
What it assumes
Assumes a JavaScript project and command-line use; demonstrates npm installation without explaining npm in depth.
What you can do after
Operate it through npm, tsc --init, and tsc to configure, type-check, and compile project files.
How it connects
Connects type checking with JavaScript, ECMAScript targets, npm, and tsconfig.json.

Also explained in

Compare how 4 books explain TypeScript compiler →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Secrets of the JavaScript Ninja, Third Edition. The section links point to the parts of the book it draws on.