Preface
Introduction
- Who This Book Is For
- How to Use This Book
- What This Book Is Not
- A Note on Runtime
- About the Author
Chapter 1: Getting Started
- Installing Node.js and a TypeScript Runner
- Your First TypeScript Program
- Template Literals: String Interpolation
- Variables: const and let
- Type Annotations (Your First Taste)
- Functions
- Arrow Functions
- The Entry Point
- tsconfig.json: Project Configuration
- Chapter Exercises
- Summary
Chapter 2: Types & Type Annotations
- The Basic Types
- Type Annotations
- Type Inference
- null and undefined
- Union Types
- Literal Types and Enums
- Type Aliases
- any and unknown
- Type Assertions
- Chapter Exercises
- Summary
Chapter 3: Arrays, Tuples & Iteration
- Typed Arrays
- Mixed-Type Arrays
- Tuples
- readonly Arrays and Tuples
- Iteration: for…of (The Each Replacement)
- forEach: Each with an Index
- Traditional for Loop (C-Style)
- map: Transform Every Element
- filter: Select Elements
- reduce: Aggregate Values
- some and every: Predicate Checks
- Spread and Rest: The Splat Operator
- Chapter Exercises
- Summary
Chapter 4: Objects, Records & Maps
- Object Types: The Hash Replacement
- Optional Properties
- Index Signatures: Hash-Like Objects
- Record: Typed Hash
- Map: When Keys Are Dynamic
- Set: Unique Collections
- Object Destructuring: Hash Splat
- Spread: Merging Objects
- readonly Properties
- Chapter Exercises
- Summary
Chapter 5: Functions Deep Dive
- Function Declarations vs Arrow Functions
- Typed Parameters and Return Types
- Optional and Default Parameters
- Rest Parameters: Ruby’s Splat
- Callbacks and Function Types
- Void and Never Return Types
- Function Overloads
- Chapter Exercises
- Summary
Chapter 6: Classes
- A Ruby Class, Translated
- Access Modifiers: public, private, protected
- JavaScript Private Fields (#)
- Parameter Properties (Shorthand)
- Inheritance: extends
- Abstract Classes
- Getters and Setters
- this and Arrow Functions
- Chapter Exercises
- Summary
Chapter 7: Interfaces & Type Aliases
- Interfaces: The Basics
- interface vs type
- Extending Interfaces
- Optional and readonly Properties in Interfaces
- Index Signatures in Interfaces
- Structural Typing in Practice
- When Interfaces Feel Like Ruby Modules
- Chapter Exercises
- Summary
Chapter 8: Modules & Package Management
- import and export: The New require
- Named Exports vs Default Exports
- Re-exports and Barrel Files
- package.json: The Gemfile
- npm, Yarn, and pnpm
- node_modules and the Magic of Resolution
- Your First TypeScript Project (Walkthrough)
- tsconfig.json Paths: Module Aliases
- Installing Type Definitions
- Chapter Exercises
- Summary
Chapter 9: Error Handling
- try/catch/finally: The Direct Mapping
- Typed Catch with instanceof
- Custom Error Classes
- The TypeScript Way: Errors as Values
- Strict Null Checks: Eliminating NoMethodError on nil
- Optional Chaining: Safe Navigation
- Nullish Coalescing: The || Alternative
- Chapter Exercises
- Summary
Chapter 10: Async Programming
- The Event Loop
- Callbacks and Callback Hell
- Promises: The Foundation
- async/await: Write Async Code That Looks Synchronous
- The async Main Function
- Async Iteration
- Ruby Threads vs TypeScript Promises
- Chapter Exercises
- Summary
Chapter 11: JSON & Type Narrowing
- JSON.stringify: Value to JSON
- JSON.parse: JSON to Value (and the any Problem)
- Type Assertions: Trust Me
- Type Guards: Prove It at Runtime
- Zod: Schema Validation
- Discriminated Unions: The JSON Pattern
- Working with Unknown JSON Shapes
- Chapter Exercises
- Summary
Chapter 12: Generics & Utility Types
- The Problem Generics Solve
- Generic Functions
- Generic Interfaces and Types
- Generic Constraints
- Built-in Utility Types
- Generics vs Duck Typing
- Chapter Exercises
- Summary
Appendix A: Ruby ↔ TypeScript Quick Reference
- Syntax & Structure
- Variables & Types
- Data Structures
- Functions & Methods
- Error Handling
- Concurrency & Async
- Modules & Packages
- Common Patterns
- TypeScript CLI Cheat Sheet
- File Layout