liveBook · Manning

Type checking

As explained in Secrets of the JavaScript Ninja, Third Edition

The process of verifying that expressions, values, and operations conform to a language's declared or inferred type rules.

JavaScript can let a type mistake survive until the code runs. Type checking examines code before execution to find operations and arguments that do not fit their expected types. TypeScript adds this check to JavaScript code, so an error can surface while you write or compile it rather than as a runtime surprise. section 2.1 section 1.3.1

A string where a number is expected

Given `const addOne = (x: number) => x + 1`, TypeScript reports an error for `addOne("10")`. Without that check, JavaScript’s coercion would produce the string `"101"`. Checking does not remove coercion from JavaScript: `1 + "2"` still produces `"12"`. It helps by identifying that result as a string when a later use expects something else. section 1.3.1 section 2.1

$ npx tsc
timeout.ts:1:12 - error TS2345: Argument of type 'number' is not assignable to parameter of type 'TimerHandler'.
 
1 setTimeout(200, () => {
             ~~~
 
Found 1 error in timeout.ts:1

The compiler output pinpoints a second argument mistake: `setTimeout` expects a callback first, but receives the number `200`. Ordinary JavaScript accepts the syntax; the TypeScript checker reports the mismatch before the code runs. section 2.1 section 2.1.1

A practical checking workflow separates finding errors from running code:

  • Install TypeScript as a development dependency, create a starter `tsconfig.json` with `npx tsc --init`, then run `npx tsc` to check the project’s `.ts` files. The configuration controls matters such as checking strictness and whether JavaScript is emitted. section 2.1.1
  • Do not assume a reported error prevents output: in the shown setup, `tsc` still emits JavaScript after finding the `setTimeout` error. section 2.1.1
  • Use editor feedback for errors and type information while working, and run full-project checks in continuous integration. A runtime that directly runs TypeScript by stripping types does not necessarily check them; keep checking as a separate step. section 2.1.4

How Secrets of the JavaScript Ninja, Third Edition covers it

How deep it goes
Works through compiler setup, an argument-type diagnostic, and checking versus execution, but defers detailed configuration options.
Examples
Uses a string passed to a number-typed function and a `setTimeout` call with its arguments reversed.
What it assumes
Assumes Node.js with `node`, `npm`, and `npx` available, while introducing TypeScript installation and starter configuration.
What you can do after
Operate/configure it with `tsconfig.json` and `npx tsc`, while retaining separate checks when running type-stripped code.
How it connects
Connects type checking to type coercion, TypeScript compilation, editor feedback, and continuous integration.

Also explained in

Compare how 7 books explain type checking →

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.