liveBook · Manning

TypeScript

As explained in Secrets of the JavaScript Ninja, Third Edition

A statically typed superset of JavaScript that compiles to JavaScript.

JavaScript can accept a call that looks reasonable but fails only when it runs. TypeScript adds compile-time type checking to JavaScript: it is a superset of the language, so ordinary JavaScript code is also TypeScript code. Its checker can flag mismatched arguments before a browser or Node.js encounters them. section 2.1 section 2.0

// Incorrect code! The time should come after the callback.
setTimeout(200, () => {
  console.log("This will never run.");
});

The arguments to setTimeout are reversed. TypeScript can flag the call even though the code is valid JavaScript; the resulting behavior differs between browser and Node.js runtimes. section 2.1

Annotate a parameter when callers must supply a particular type: `const doubleNumber = (x: number) => x * 2;` makes `doubleNumber("abc")` a type error. On conversion to JavaScript, the annotation is removed; it does not insert a runtime check. This distinction matters when values arrive from code whose behavior you cannot assume. section 5.5.1 section 5.5

Function types offer a few practical choices:

  • Annotate rest arguments as an array, such as `...remainingNumbers: number[]`. A default value can supply a parameter’s inferred type; `string | null` explicitly permits either type. section 5.5.1
  • Let TypeScript infer a return type from possible returns, or declare one after the parameter list. An explicit return type can expose a missing branch—for example, a sign function that handles positive and negative numbers but forgets zero. section 5.5.2

Static checking does not replace JavaScript’s runtime behavior: implicit coercion still exists. Editors such as VS Code can display type information and errors while you work. TypeScript can also transpile syntax for a chosen ECMAScript target, but it does not supply missing runtime APIs through polyfills. section 2.1 section 1.3.1 section 1.1.3

Running TypeScript need not always involve a compilation wait. Node.js 22.6.0 and newer can strip types with `--experimental-strip-types`, but stripping does not run the type checker. That flag excludes enum and namespace syntax; TypeScript 5.8’s `erasableSyntaxOnly` option can enforce compatible syntax. Compilation remains useful for browsers and older runtimes. section 2.1.4

How Secrets of the JavaScript Ninja, Third Edition covers it

How deep it goes
Explains parameter annotations, inferred and explicit return types, type removal, and direct execution; stops short of a full type-system treatment.
Examples
Uses a reversed setTimeout call, mismatched function arguments, and a return type that reveals an unhandled zero case.
What it assumes
Assumes familiarity with JavaScript function calls and parameters while defining TypeScript’s checking role and annotation syntax.
What you can do after
Apply it via the TypeScript checker to annotate functions, inspect inferred types, and catch argument or return errors before execution.
How it connects
Connects static type checking to JavaScript coercion, function types, IDE integration, transpilation, and polyfills.

Also explained in

Compare how 6 books explain TypeScript →

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.