TypeScript Is a Checker, Not a Runtime · TypeScript

Ch 1 · Why TypeScript Is Worth Your Time — card 2 of 4.

This trips up almost everyone once. Browsers do not run TypeScript. Node does not run TypeScript. There is no TypeScript engine anywhere in production. What exists is a compiler, tsc, which does exactly two things:

That means types cost you nothing at runtime. They are not shipped, not parsed, not evaluated by the user's browser. They exist only while you are writing and building. It also means TypeScript cannot protect you from something that goes wrong at runtime — a broken API, a missing file, a user typing nonsense. It protects you from being wrong about your own code.

The block below is TypeScript. The two annotations — : number and : string — vanish from the compiled output. Run it and you will see the compiler accepted it and Node executed the plain JavaScript underneath.

const hours: number = 12;
const client: string = "Rehman Textiles";
const rate: number = 2500;

console.log(client + " owes " + (hours * rate));