O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Programming TypeScript

Book Description

Any programmer working with a dynamically typed language will tell you how hard it is to scale to more lines of code and more engineers. That’s why Facebook, Google, and Microsoft invented gradual static type layers for their dynamically typed JavaScript and Python code. This practical book shows you how one such type layer, TypeScript, is unique among them: it makes programming fun with its powerful static type system.

If you’re a programmer with intermediate JavaScript experience, author Boris Cherny will teach you how to master the TypeScript language. You’ll understand how TypeScript can help you eliminate bugs in your code and enable you to scale your code across more engineers than you could before.

In this book, you’ll:

  • Start with the basics: Learn about TypeScript’s different types and type operators, including what they’re for and how they’re used
  • Explore advanced topics: Understand TypeScript’s sophisticated type system, including how to safely handle errors and build asynchronous programs
  • Dive in hands-on: Use TypeScript with your favorite frontend and backend frameworks, migrate your existing JavaScript project to TypeScript, and run your TypeScript application in production

Table of Contents

  1. Preface
    1. How This Book Is Organized
    2. Style
      1. Conventions Used in This Book
    3. Using Code Examples
    4. O’Reilly Online Learning
    5. How to Contact Us
    6. Acknowledgments
  2. 1. Introduction
  3. 2. TypeScript: A 10_000 Foot View
    1. The Compiler
    2. The Type System
      1. TypeScript Versus JavaScript
    3. Code Editor Setup
      1. tsconfig.json
      2. tslint.json
    4. index.ts
    5. Exercises
  4. 3. All About Types
    1. Talking About Types
    2. The ABCs of Types
      1. any
      2. unknown
      3. boolean
      4. number
      5. bigint
      6. string
      7. symbol
      8. Objects
      9. Intermission: Type Aliases, Unions, and Intersections
      10. Arrays
      11. Tuples
      12. null, undefined, void, and never
      13. Enums
    3. Summary
    4. Exercises
  5. 4. Functions
    1. Declaring and Invoking Functions
      1. Optional and Default Parameters
      2. Rest Parameters
      3. call, apply, and bind
      4. Typing this
      5. Generator Functions
      6. Iterators
      7. Call Signatures
      8. Contextual Typing
      9. Overloaded Function Types
    2. Polymorphism
      1. When Are Generics Bound?
      2. Where Can You Declare Generics?
      3. Generic Type Inference
      4. Generic Type Aliases
      5. Bounded Polymorphism
      6. Generic Type Defaults
    3. Type-Driven Development
    4. Summary
    5. Exercises
  6. 5. Classes and Interfaces
    1. Classes and Inheritance
    2. super
    3. Using this as a Return Type
    4. Interfaces
      1. Declaration Merging
      2. Implementations
      3. Implementing Interfaces Versus Extending Abstract Classes
    5. Classes Are Structurally Typed
    6. Classes Declare Both Values and Types
    7. Polymorphism
    8. Mixins
    9. Decorators
    10. Simulating final Classes
    11. Design Patterns
      1. Factory Pattern
      2. Builder Pattern
    12. Summary
    13. Exercises
  7. 6. Advanced Types
    1. Relationships Between Types
      1. Subtypes and Supertypes
      2. Variance
      3. Assignability
      4. Type Widening
      5. Refinement
    2. Totality
    3. Advanced Object Types
      1. Type Operators for Object Types
      2. The Record Type
      3. Mapped Types
      4. Companion Object Pattern
    4. Advanced Function Types
      1. Improving Type Inference for Tuples
      2. User-Defined Type Guards
    5. Conditional Types
      1. Distributive Conditionals
      2. The infer Keyword
      3. Built-in Conditional Types
    6. Escape Hatches
      1. Type Assertions
      2. Nonnull Assertions
      3. Definite Assignment Assertions
    7. Simulating Nominal Types
    8. Safely Extending the Prototype
    9. Summary
    10. Exercises
  8. 7. Handling Errors
    1. Returning null
    2. Throwing Exceptions
    3. Returning Exceptions
    4. The Option Type
    5. Summary
    6. Exercises
  9. 8. Asynchronous Programming, Concurrency, and Parallelism
    1. JavaScript’s Event Loop
    2. Working with Callbacks
    3. Regaining Sanity with Promises
    4. async and await
    5. Async Streams
      1. Event Emitters
    6. Typesafe Multithreading
      1. In the Browser: With Web Workers
      2. In NodeJS: With Child Processes
    7. Summary
    8. Exercises
  10. 9. Frontend and Backend Frameworks
    1. Frontend Frameworks
      1. React
      2. Angular
    2. Typesafe APIs
    3. Backend Frameworks
    4. Summary
  11. 10. Namespaces.Modules
    1. A Brief History of JavaScript Modules
    2. import, export
      1. Dynamic Imports
      2. Using CommonJS and AMD Code
      3. Module Mode Versus Script Mode
    3. Namespaces
      1. Collisions
      2. Compiled Output
    4. Declaration Merging
    5. Summary
    6. Exercise
  12. 11. Interoperating with JavaScript
    1. Type Declarations
      1. Ambient Variable Declarations
      2. Ambient Type Declarations
      3. Ambient Module Declarations
    2. Gradually Migrating from JavaScript to TypeScript
      1. Step 1: Add TSC
      2. Step 2a: Enable Typechecking for JavaScript (Optional)
      3. Step 2b: Add JSDoc Annotations (Optional)
      4. Step 3: Rename Your Files to .ts
      5. Step 4: Make It strict
    3. Type Lookup for JavaScript
    4. Using Third-Party JavaScript
      1. JavaScript That Comes with Type Declarations
      2. JavaScript That Has Type Declarations on DefinitelyTyped
      3. JavaScript That Doesn’t Have Type Declarations on DefinitelyTyped
    5. Summary
  13. 12. Building and Running TypeScript
    1. Building Your TypeScript Project
      1. Project Layout
      2. Artifacts
      3. Dialing In Your Compile Target
      4. Enabling Source Maps
      5. Project References
      6. Error Monitoring
    2. Running TypeScript on the Server
    3. Running TypeScript in the Browser
    4. Publishing Your TypeScript Code to NPM
    5. Triple-Slash Directives
      1. The types Directive
      2. The amd-module Directive
    6. Summary
  14. 13. Conclusion
  15. A. Type Operators
  16. B. Type Utilities
  17. C. Scoped Declarations
    1. Does It Generate a Type?
    2. Does It Merge?
  18. D. Recipes for Writing Declaration Files for Third-Party JavaScript Modules
    1. Types of Exports
      1. Globals
      2. ES2015 Exports
      3. CommonJS Exports
      4. UMD Exports
    2. Extending a Module
      1. Globals
      2. Modules
  19. E. Triple-Slash Directives
    1. Internal Directives
    2. Deprecated Directives
  20. F. TSC Compiler Flags for Safety
  21. G. TSX
  22. Index