1function add(a: number, b: number) {2 return a + b;3}4console.log(add("2", 3));console.log(add(2, 3));tsctype checker!✓1function add(a, b) {2 return a + b;3}4console.log(add("2", 3));console.log(add(2, 3));$ node add.js235add(a: number, b: number): numberTSJS: number: number
Back row: add.ts (TypeScript, with types) and tsc, the type checkerFront row: add.js (plain JavaScript, no types) and the laptop that runs itTokens: the file hopping between them, and the two types tsc removesAt the back: add.ts, the TypeScript you write, and tsc, the type checker. In front: add.js, the JavaScript tsc writes, and a laptop running it. Plain JavaScript runs add("2", 3) and prints 23. TypeScript's types let tsc catch that on line 4 before anything runs; after the fix tsc drops the types and writes add.js, which prints 5. By default tsc still writes the .js file even when it finds type errors; turn on the noEmitOnError setting to stop that. Either way, you see the error first.

Why types

  1. This is JavaScript, the language browsers and Node.js run. add should add two numbers, but line 4 gives it the text "2". JavaScript runs it anyway: + joins text, so it prints 23, not 5. No warning.

The check

Plain JavaScript out

In your editor

What happens

;; step 1/9 · js-bug
add.js  (plain JavaScript)
  1  function add(a, b) {
  2      return a + b;
  3  }
> 4  console.log(add("2", 3));

"2" is text (a string), 3 is a number
"2" + 3  →  "23"   + joins text
node add.js  prints  23

By default tsc still writes the .js file even when it finds type errors; turn on the noEmitOnError setting to stop that. Either way, you see the error first. The add.js shown is exactly what tsc 5.9 writes for this file with no settings. Other settings (such as the target version of JavaScript, or strict mode adding "use strict";) can change small details.