liveBook · Manning

Chapter 2: Using modern JavaScript tools

What you will learn in chapter 2 of Secrets of the JavaScript Ninja, Third Edition

The chapter introduction explains how modern tools added guardrails to JavaScript development and why Node.js is used to run those tools.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The chapter introduction explains how modern tools added guardrails to JavaScript development and why Node.js is used to run those tools.

Key terms: guardrails, compile time, IDE, TypeScript, ESLint, Prettier, compile-time type checking, code rules, Node.js, runtime

2.1 Type checking with TypeScript

This section shows how TypeScript catches JavaScript type mismatches before runtime and distinguishes compile-time type checking from JavaScript’s runtime coercion.

Key terms: TypeScript, superset, type checker, compile time, runtime, dynamically typed, statically typed, implicit type coercion, weakly typed

2.1.1 Installing and running the TypeScript compiler

This section sets up the TypeScript compiler, runs it to check and compile a project, and shows how to check types without emitting JavaScript.

Key terms: package.json, dev dependency, typescript, tsc, tsconfig.json, --init, type checker, .ts files, timeout.js, --noEmit

2.1.2 Type annotations and declarations

This section explains why untyped function parameters can trigger TypeScript’s implicit-any error and how annotations and named type declarations provide types the checker can use.

Key terms: type inference, any, implicit any, noImplicitAny, strict, type annotation, function type, parameter types, return type, type declaration

2.1.3 Type narrowing and predicates

This section explains how TypeScript narrows types through runtime checks, where that inference falls short, and how to use type predicates and assertions cautiously.

Key terms: union operator, type narrowing, in checks, duck typing, runtime, compiled JavaScript, mutable structures, as const, isString, isShortString

2.1.4 Running TypeScript without compilation

This section explains how running TypeScript directly can speed development, what type stripping does and does not do, and why compilation and separate type checks still matter.

Key terms: --experimental-strip-types, enum, namespace, erasableSyntaxOnly, type-stripping, type checking, Deno, compilation, minification, transpiled

2.2 Enforcing code rules with ESLint

The section explains why static analysis complements testing and introduces ESLint as a tool for finding possible mistakes beyond TypeScript’s type checks.

Key terms: unit testing, code coverage, static analysis, TypeScript, ESLint, type misuse

2.2.1 Setting up ESLint in a TypeScript project

This section sets up ESLint for a TypeScript project and shows how type-aware rules catch issues that ordinary ESLint rules cannot.

Key terms: package.json, eslint, @eslint/js, TypeScript, typescript-eslint, eslint.config.mjs, recommended rules, strictTypeChecked, stylisticTypeChecked, type checker

2.2.2 Disabling ESLint rules

This section explains why an ESLint rule can flag valid code and how to choose between disabling the rule project-wide and making a documented exception.

Key terms: strict, @typescript-eslint/no-non-null-assertion, non-null assertion operator, eslint.config.mjs, TypeScript configuration block, case-by-case basis, inline configuration, one-line rule exception, explanatory comment

2.2.3 Enforcing a distinctive code style

The section explains how teams can use ESLint rules to enforce their chosen JavaScript style, using variable shadowing and plugin rules as examples.

Key terms: ESLint rules, good parts, code style, shadowing, no-shadow, recommended rule set, ESLint plugins, eslint-plugin-unicorn, code style rules

2.3 Automating formatting with Prettier

This section explains why teams use Prettier to make code formatting more consistent without prolonged debates over style.

Key terms: ESLint, line length limit, Prettier, opinionated code formatter

2.3.1 Setting up Prettier

This section sets up Prettier, shows how its default settings format JavaScript, and explains how to exclude generated files.

Key terms: Prettier, dev dependency, Prettier config, singleQuote, useTabs, tabWidth, semi, .prettierignore, package-lock.json

2.3.2 Running Prettier from a commit hook

The section explains how to use Husky to run Prettier before commits, how to set up its hook, and when the cost of pre-commit checks calls for a different approach.

Key terms: editor integration, ESLint, Prettier, Husky, pre-commit hook, prepare, npm lifecycle scripts, .husky, core.hooksPath, prepare.sh

2.4 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 1: JavaScript is everywhere Chapter 3: Working with Node.js and npm →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.