Chapter 2: Using modern JavaScript tools
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
- JavaScript's missing development guardrails. Explain why early JavaScript was seen as unsuitable for serious programming and what changed that perception.
- Roles of three modern tools. Distinguish the contributions of TypeScript, ESLint, and Prettier to writing JavaScript.
- Early detection of type mismatches. Explain why TypeScript can flag an invalid JavaScript setTimeout call before it runs, even though the same code is valid TypeScript syntax.
- TypeScript project and configuration. Describe how to prepare the example project: use package.json to track dependencies, install typescript as a dev dependency, and create a starter tsconfig.json with tsc --init. Explain what kinds of decisions the configuration supplies to tsc.
- Type checking and emitted output. Explain what happens when tsc runs on the example project, including its default file selection, the reported type error, and the JavaScript output. Describe how tsc --noEmit changes that behavior.
- Type inference and implicit any. Explain why TypeScript gives the pluralize function’s untyped parameters the any type and why that produces a compiler error in the section’s configuration.
- Function type annotation choices. Describe how annotating individual parameters or the function itself supplies parameter types, and what checking and inference those types enable.
- Property checks and narrowing. Explain how checking for a property narrows a union type and why an object’s properties, rather than its declared type name, determine whether it qualifies.
- Function inference and predicates. Explain why TypeScript narrows from `isString` but not `isShortString`, and how an `x is string` predicate can produce a misleading `never` branch.
- Direct TypeScript execution. Explain why running TypeScript directly can speed development and describe the Node.js support and syntax limits given here.
- Type stripping versus checking. Distinguish running TypeScript by stripping types from checking whether its types are valid.
- Continuing need for compilation. Explain why direct execution does not eliminate compilation or transpilation.
- Static analysis and ESLint. Describe how static analysis helps catch possible mistakes without running code, and distinguish the roles assigned here to TypeScript and ESLint.
- TypeScript linting project setup. Describe the setup sequence: create a package.json, install TypeScript, eslint, @eslint/js, and typescript-eslint, generate a TypeScript configuration, and create eslint.config.mjs.
- Core and type-aware rules. Distinguish the configuration’s unconditional ESLint recommended rules from its typescript-eslint strictTypeChecked and stylisticTypeChecked rule sets, and explain the benefit and cost of type-aware linting.
- Unnecessary condition diagnostics. Explain why the first example condition triggers both no-constant-condition and @typescript-eslint/no-unnecessary-condition, while the second triggers only the type-aware rule.
- Projectwide versus local exceptions. Distinguish rewriting code, turning a rule off for the project, and keeping it enabled while making case-by-case exceptions.
- Explained inline rule exceptions. Describe how ESLint inline configuration comments make a next-line exception and why an explanation is useful.
- Rules for clearer code. Explain why a team might use ESLint rules to discourage certain JavaScript patterns and enforce a shared code style.
- Prettier’s opinionated formatting. Explain why the section favors Prettier over relying on an ESLint line-length rule for formatting.
- Project Prettier setup. Describe installing Prettier as a development dependency, creating an empty project config, and running it to tidy a JavaScript file.
- Editor formatting and commit hooks. Explain why editor integration is recommended for immediate formatting, and why a Husky pre-commit hook is useful when collaborators may not use the same editor setup.
- Husky prepare script setup. Describe the role of Husky's prepare script and the adjustment needed when package.json is in a subdirectory of the Git repository.
What it builds on
- Type checking (chapter 1) · The earlier discussion of type checking as a JavaScript practice.
- Linting and formatting (chapter 1) · The earlier introduction to linting and formatting as development practices.
- Transpilers and polyfills give us access to tomorrow’s JavaScript today (chapter 1) · The basic idea of transpiling JavaScript syntax.
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.
- JavaScript's missing development guardrails: Explain why early JavaScript was seen as unsuitable for serious programming and what changed that perception.
Why it matters: This explains the problem the chapter's tools are meant to address. - Roles of three modern tools: Distinguish the contributions of TypeScript, ESLint, and Prettier to writing JavaScript.
Why it matters: Each tool addresses a different part of making JavaScript code easier to develop and maintain. - Node.js as tooling runtime: Describe Node.js's role in running JavaScript development tools.
Why it matters: It explains why the chapter expects a Node.js development environment.
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.
- Early detection of type mismatches: Explain why TypeScript can flag an invalid JavaScript setTimeout call before it runs, even though the same code is valid TypeScript syntax.
Why it matters: Checking types before running code can reveal bugs that might otherwise fail silently or surface only at runtime. - Dynamic and static type enforcement: Distinguish JavaScript’s runtime type enforcement from TypeScript’s compile-time type checking.
Why it matters: This distinction explains when each language can reveal a type mismatch. - Type coercion despite static checking: Explain why TypeScript does not remove JavaScript’s implicit type coercion and how its checker can account for a coerced result.
Why it matters: Static checking and implicit coercion are separate issues: TypeScript can warn about a coerced result without changing what JavaScript does at runtime.
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.
- TypeScript project and configuration: Describe how to prepare the example project: use package.json to track dependencies, install typescript as a dev dependency, and create a starter tsconfig.json with tsc --init. Explain what kinds of decisions the configuration supplies to tsc.
Why it matters: This setup gives the compiler the dependency and configuration it needs to work on the project. - Type checking and emitted output: Explain what happens when tsc runs on the example project, including its default file selection, the reported type error, and the JavaScript output. Describe how tsc --noEmit changes that behavior.
Why it matters: A type-checking error and JavaScript emission are separate outcomes, so the reader can choose whether to produce files. - Compiler output and strict mode: Describe the transformations the section attributes to the TypeScript compiler and explain why the example output contains "use strict" despite otherwise matching its source.
Why it matters: It explains why the generated JavaScript can closely resemble its source without being byte-for-byte identical.
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.
- Type inference and implicit any: Explain why TypeScript gives the pluralize function’s untyped parameters the any type and why that produces a compiler error in the section’s configuration.
Why it matters: It explains why valid JavaScript can still produce a TypeScript error. - Function type annotation choices: Describe how annotating individual parameters or the function itself supplies parameter types, and what checking and inference those types enable.
Why it matters: Either annotation style gives the checker information it needs to catch incompatible function code and calls. - Named reusable type declarations: Explain why a complex type might be extracted into a named declaration and how declared types can be used and shared.
Why it matters: Naming a type can make annotations easier to read and let multiple places use the same definition.
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.
- Property checks and narrowing: Explain how checking for a property narrows a union type and why an object’s properties, rather than its declared type name, determine whether it qualifies.
Why it matters: Property checks let you safely work with values that could have more than one type. - Types versus runtime checks: Distinguish TypeScript types from JavaScript runtime values, and explain why the property checks remain in compiled code but the types do not.
Why it matters: It keeps type annotations from being mistaken for checks that JavaScript can perform at runtime. - Mutable structure inference limit: Describe TypeScript’s difficulty tracking modifications to mutable objects and arrays, and state when `as const` can address it.
Why it matters: It explains why an apparently clear change to an object or array may not produce the inference you expect. - Function inference and predicates: Explain why TypeScript narrows from `isString` but not `isShortString`, and how an `x is string` predicate can produce a misleading `never` branch.
Why it matters: A predicate’s false-case promise can be just as consequential as its true-case promise. - Cautious type assertions: Explain what `as` tells the type checker and why the section warns against using it carelessly.
Why it matters: An assertion can get past an inference limitation, but it puts responsibility for accuracy on you.
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.
- Direct TypeScript execution: Explain why running TypeScript directly can speed development and describe the Node.js support and syntax limits given here.
Why it matters: Direct execution can remove a slow compilation step from everyday development work. - Type stripping versus checking: Distinguish running TypeScript by stripping types from checking whether its types are valid.
Why it matters: A script running successfully does not mean its TypeScript types have been checked. - Continuing need for compilation: Explain why direct execution does not eliminate compilation or transpilation.
Why it matters: Running TypeScript locally does not settle how to prepare code for browsers or older runtimes. - Editor feedback and CI checks: Describe how developers can get type feedback without waiting for a full project-wide check during every development task.
Why it matters: This preserves useful type feedback while keeping routine development fast.
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.
- Limits of test coverage: Explain why testing alone cannot guarantee that code will behave as intended, even with 100% code coverage.
Why it matters: This explains why the section recommends another way to look for mistakes alongside tests. - Static analysis and ESLint: Describe how static analysis helps catch possible mistakes without running code, and distinguish the roles assigned here to TypeScript and ESLint.
Why it matters: It clarifies what ESLint adds to the checks already provided by TypeScript.
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.
- TypeScript linting project setup: Describe the setup sequence: create a package.json, install TypeScript, eslint, @eslint/js, and typescript-eslint, generate a TypeScript configuration, and create eslint.config.mjs.
Why it matters: These are the pieces needed to run ESLint on the project’s TypeScript code. - Core and type-aware rules: Distinguish the configuration’s unconditional ESLint recommended rules from its typescript-eslint strictTypeChecked and stylisticTypeChecked rule sets, and explain the benefit and cost of type-aware linting.
Why it matters: The two rule groups explain why this setup can catch both general code problems and problems visible only through types. - Unnecessary condition diagnostics: Explain why the first example condition triggers both no-constant-condition and @typescript-eslint/no-unnecessary-condition, while the second triggers only the type-aware rule.
Why it matters: The example makes the practical difference between ordinary and type-aware lint rules visible. - Configuration editor type feedback: Distinguish the editor feedback provided by @ts-check in the JavaScript ESLint configuration file from the errors reported by running tsc.
Why it matters: Editor type feedback for the ESLint configuration should not be mistaken for coverage by the TypeScript build.
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.
- Strict rule tradeoffs: Explain why enabling more ESLint rules can be useful yet frustrating, using the non-null assertion rule as an example.
Why it matters: A linter warning is a reason to examine the code, not automatic proof that the code is buggy. - Projectwide versus local exceptions: Distinguish rewriting code, turning a rule off for the project, and keeping it enabled while making case-by-case exceptions.
Why it matters: Choosing the scope of an exception lets a team retain useful warnings without unnecessarily rewriting acceptable code. - Explained inline rule exceptions: Describe how ESLint inline configuration comments make a next-line exception and why an explanation is useful.
Why it matters: A targeted, explained exception can satisfy the linter while making the reason clear to reviewers.
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.
- Rules for clearer code: Explain why a team might use ESLint rules to discourage certain JavaScript patterns and enforce a shared code style.
Why it matters: Lint rules can turn a team’s style decisions into consistent checks. - Variable shadowing rule: Describe variable shadowing, why it can hurt readability, and how a project can opt in to ESLint’s no-shadow rule.
Why it matters: The no-shadow example shows how a legal language feature can still be a deliberate team-style choice. - Plugin style rule range: Describe the kinds of code-style choices available through ESLint plugins beyond the core rule sets.
Why it matters: Plugins give teams options for enforcing style choices beyond the built-in rules.
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.
- Prettier’s opinionated formatting: Explain why the section favors Prettier over relying on an ESLint line-length rule for formatting.
Why it matters: A shared formatter helps teams spend less effort resolving formatting choices that make code harder to read.
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.
- Project Prettier setup: Describe installing Prettier as a development dependency, creating an empty project config, and running it to tidy a JavaScript file.
Why it matters: A project-level setup lets Prettier format code consistently without requiring custom options. - Default formatting options: Explain that an empty config inherits Prettier's defaults and describe how the illustrated options affect quotes, indentation, and semicolons.
Why it matters: These defaults explain the style Prettier produces before you change any settings. - Formatting scope and exclusions: Explain that Prettier formats recognized file types beyond JavaScript and that a .prettierignore file can prevent it from formatting generated files such as package-lock.json.
Why it matters: An ignore file keeps broad formatting coverage from touching generated files unnecessarily.
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.
- Editor formatting and commit hooks: Explain why editor integration is recommended for immediate formatting, and why a Husky pre-commit hook is useful when collaborators may not use the same editor setup.
Why it matters: A commit hook helps keep formatting automatic across a team without requiring everyone to configure their editor alike. - Husky prepare script setup: Describe the role of Husky's prepare script and the adjustment needed when package.json is in a subdirectory of the Git repository.
Why it matters: The hook can run only after the repository's Git hooks path has been configured correctly. - Commit formatting and staged files: Describe what the configured pre-commit script does and how lint-staged can limit the files processed.
Why it matters: Limiting formatting to staged changes can make commit-time automation more practical in large projects. - Pre-commit check tradeoffs: Explain why a pre-commit hook can run checks beyond formatting, but why large projects may move TypeScript checks to CI.
Why it matters: A useful commit check should catch problems without making every commit unnecessarily slow.
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
- The Node.js runtime (chapter 3) · Examines the Node.js runtime in its own right.
- Targeting browsers with transpilation (chapter 13) · TypeScript as a compiler for JavaScript projects
- Importing and exporting in ESM (chapter 13) · Assumes familiarity with running the TypeScript compiler.
- Defining parameter types (chapter 5) · Type annotations and declarations in TypeScript
- Defining getters and setters (chapter 9) · TypeScript type annotations and declarations provide context for describing a variable’s type.
- Defining return types in TypeScript (chapter 5) · Discussing function return types.
Concepts explained in this chapter
- type checkingThe process of verifying that expressions, values, and operations conform to a language's declared or inferred type rules.
- ESLintAn open-source linter for JavaScript and TypeScript with configurable rules and automatic fixes.
- typescript-eslintA collection of tools and configurations for TypeScript-aware linting with ESLint.
- TypeScriptA statically typed superset of JavaScript that compiles to JavaScript.
- PrettierPrettier is an opinionated source-code formatter for JavaScript and related languages.
- linter ruleA configurable static-analysis condition that determines when a linter reports a source-code construct.
- TypeScript compilerA tool that compiles TypeScript source code to JavaScript, optionally targeting a selected ECMAScript version.
- tsconfig.jsonA JSON configuration file that specifies TypeScript compiler inputs, type libraries, checking settings, emission behavior, and the target ECMAScript version.
- JavaScriptJavaScript is a high-level programming language standardized as ECMAScript and widely used for web and general-purpose software development.
- type hintsSyntax attached to a TypeScript declaration or expression that explicitly specifies expected types and can supply information for type checking and inference.
- type inferenceThe compile-time determination of types from surrounding declarations, expressions, or values without requiring explicit annotations.
- as constA TypeScript const assertion that preserves literal types and applies readonly semantics to the asserted literal expression.
- as operatorTypeScript syntax for asserting a compile-time type for an expression without changing its runtime value.
- Node.jsA standalone JavaScript runtime built around the V8 engine and designed for server-side and general-purpose applications.
- type coercionThe implicit conversion of a value from one type to another during an operation or expression evaluation.
- development dependencyA project dependency needed for development activities such as building and testing, but not normally at application runtime.
- package.jsonpackage.json is a project manifest file that records package metadata, scripts, and dependencies for JavaScript package tools.
- strict modeA JavaScript execution mode that applies stricter syntax and runtime semantics.
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.
