Learning TypeScript

Book description

TypeScript has conquered the world of JavaScript: it's one of the world's fastest growing and most popular languages across developer surveys, widely used in consumer and business companies alike, and frequently credited for helping massive web applications scale. But what is TypeScript? How does it work, why does it work, and how can we use it?

Learning TypeScript takes beginner to intermediate JavaScript programmers from knowing nothing about "types" or a "type system" to full mastery of the fundamentals of TypeScript. It's more than a means to find bugs and typos--it's a useful system for declaring the way our JavaScript should work and helping us stick to it. You'll learn how TypeScript:

  • interacts with JavaScript
  • analyzes and understands code
  • augments your existing development pattern
  • helps you document your code
  • works with IDEs to provide refactoring tools
  • assists local development in refactoring code
  • helps you develop more quickly with fewer bugs

Publisher resources

View/Submit Errata

Table of contents

  1. Preface
    1. Who Should Read This Book
    2. Why I Wrote This Book
    3. Navigating This Book
      1. Examples and Projects
    4. Conventions Used in This Book
    5. Using Code Examples
    6. O’Reilly Online Learning
    7. How to Contact Us
    8. Acknowledgments
  2. I. Concepts
  3. 1. From JavaScript to TypeScript
    1. History of JavaScript
    2. Vanilla JavaScript’s Pitfalls
      1. Costly Freedom
      2. Loose Documentation
      3. Weaker Developer Tooling
    3. TypeScript!
    4. Getting Started in the TypeScript Playground
      1. TypeScript in Action
      2. Freedom Through Restriction
      3. Precise Documentation
      4. Stronger Developer Tooling
      5. Compiling Syntax
    5. Getting Started Locally
      1. Running Locally
      2. Editor Features
    6. What TypeScript Is Not
      1. A Remedy for Bad Code
      2. Extensions to JavaScript (Mostly)
      3. Slower Than JavaScript
      4. Finished Evolving
    7. Summary
  4. 2. The Type System
    1. What’s in a Type?
      1. Type Systems
      2. Kinds of Errors
    2. Assignability
      1. Understanding Assignability Errors
    3. Type Annotations
      1. Unnecessary Type Annotations
    4. Type Shapes
      1. Modules
    5. Summary
  5. 3. Unions and Literals
    1. Union Types
      1. Declaring Union Types
      2. Union Properties
    2. Narrowing
      1. Assignment Narrowing
      2. Conditional Checks
      3. Typeof Checks
    3. Literal Types
      1. Literal Assignability
    4. Strict Null Checking
      1. The Billion-Dollar Mistake
      2. Truthiness Narrowing
      3. Variables Without Initial Values
    5. Type Aliases
      1. Type Aliases Are Not JavaScript
      2. Combining Type Aliases
    6. Summary
  6. 4. Objects
    1. Object Types
      1. Declaring Object Types
      2. Aliased Object Types
    2. Structural Typing
      1. Usage Checking
      2. Excess Property Checking
      3. Nested Object Types
      4. Optional Properties
    3. Unions of Object Types
      1. Inferred Object-Type Unions
      2. Explicit Object-Type Unions
      3. Narrowing Object Types
      4. Discriminated Unions
    4. Intersection Types
      1. Dangers of Intersection Types
    5. Summary
  7. II. Features
  8. 5. Functions
    1. Function Parameters
      1. Required Parameters
      2. Optional Parameters
      3. Default Parameters
      4. Rest Parameters
    2. Return Types
      1. Explicit Return Types
    3. Function Types
      1. Function Type Parentheses
      2. Parameter Type Inferences
      3. Function Type Aliases
    4. More Return Types
      1. Void Returns
      2. Never Returns
    5. Function Overloads
      1. Call-Signature Compatibility
    6. Summary
  9. 6. Arrays
    1. Array Types
      1. Array and Function Types
      2. Union-Type Arrays
      3. Evolving Any Arrays
      4. Multidimensional Arrays
    2. Array Members
      1. Caveat: Unsound Members
    3. Spreads and Rests
      1. Spreads
      2. Spreading Rest Parameters
    4. Tuples
      1. Tuple Assignability
      2. Tuple Inferences
    5. Summary
  10. 7. Interfaces
    1. Type Aliases Versus Interfaces
    2. Types of Properties
      1. Optional Properties
      2. Read-Only Properties
      3. Functions and Methods
      4. Call Signatures
      5. Index Signatures
      6. Nested Interfaces
    3. Interface Extensions
      1. Overridden Properties
      2. Extending Multiple Interfaces
    4. Interface Merging
      1. Member Naming Conflicts
    5. Summary
  11. 8. Classes
    1. Class Methods
    2. Class Properties
      1. Function Properties
      2. Initialization Checking
      3. Optional Properties
      4. Read-Only Properties
    3. Classes as Types
    4. Classes and Interfaces
      1. Implementing Multiple Interfaces
    5. Extending a Class
      1. Extension Assignability
      2. Overridden Constructors
      3. Overridden Methods
      4. Overridden Properties
    6. Abstract Classes
    7. Member Visibility
      1. Static Field Modifiers
    8. Summary
  12. 9. Type Modifiers
    1. Top Types
      1. any, Again
      2. unknown
    2. Type Predicates
    3. Type Operators
      1. keyof
      2. typeof
    4. Type Assertions
      1. Asserting Caught Error Types
      2. Non-Null Assertions
      3. Type Assertion Caveats
    5. Const Assertions
      1. Literals to Primitives
      2. Read-Only Objects
    6. Summary
  13. 10. Generics
    1. Generic Functions
      1. Explicit Generic Call Types
      2. Multiple Function Type Parameters
    2. Generic Interfaces
      1. Inferred Generic Interface Types
    3. Generic Classes
      1. Explicit Generic Class Types
      2. Extending Generic Classes
      3. Implementing Generic Interfaces
      4. Method Generics
      5. Static Class Generics
    4. Generic Type Aliases
      1. Generic Discriminated Unions
    5. Generic Modifiers
      1. Generic Defaults
    6. Constrained Generic Types
      1. keyof and Constrained Type Parameters
    7. Promises
      1. Creating Promises
      2. Async Functions
    8. Using Generics Right
      1. The Golden Rule of Generics
      2. Generic Naming Conventions
    9. Summary
  14. III. Usage
  15. 11. Declaration Files
    1. Declaration Files
    2. Declaring Runtime Values
      1. Global Values
      2. Global Interface Merging
      3. Global Augmentations
    3. Built-In Declarations
      1. Library Declarations
      2. DOM Declarations
    4. Module Declarations
      1. Wildcard Module Declarations
    5. Package Types
      1. declaration
      2. Dependency Package Types
      3. Exposing Package Types
    6. DefinitelyTyped
      1. Type Availability
    7. Summary
  16. 12. Using IDE Features
    1. Navigating Code
      1. Finding Definitions
      2. Finding References
      3. Finding Implementations
    2. Writing Code
      1. Completing Names
      2. Automatic Import Updates
      3. Code Actions
    3. Working Effectively with Errors
      1. Language Service Errors
    4. Summary
  17. 13. Configuration Options
    1. tsc Options
      1. Pretty Mode
      2. Watch Mode
    2. TSConfig Files
      1. tsc --init
      2. CLI Versus Configuration
    3. File Inclusions
      1. include
      2. exclude
    4. Alternative Extensions
      1. JSX Syntax
      2. resolveJsonModule
    5. Emit
      1. outDir
      2. target
      3. Emitting Declarations
      4. Source Maps
      5. noEmit
    6. Type Checking
      1. lib
      2. skipLibCheck
      3. Strict Mode
    7. Modules
      1. module
      2. moduleResolution
      3. Interoperability with CommonJS
      4. isolatedModules
    8. JavaScript
      1. allowJs
      2. checkJs
      3. JSDoc Support
    9. Configuration Extensions
      1. extends
      2. Configuration Bases
    10. Project References
      1. composite
      2. references
      3. Build Mode
    11. Summary
  18. IV. Extra Credit
  19. 14. Syntax Extensions
    1. Class Parameter Properties
    2. Experimental Decorators
    3. Enums
      1. Automatic Numeric Values
      2. String-Valued Enums
      3. Const Enums
    4. Namespaces
      1. Namespace Exports
      2. Nested Namespaces
      3. Namespaces in Type Definitions
      4. Prefer Modules Over Namespaces
    5. Type-Only Imports and Exports
    6. Summary
  20. 15. Type Operations
    1. Mapped Types
      1. Mapped Types from Types
      2. Changing Modifiers
      3. Generic Mapped Types
    2. Conditional Types
      1. Generic Conditional Types
      2. Type Distributivity
      3. Inferred Types
      4. Mapped Conditional Types
    3. never
      1. never and Intersections and Unions
      2. never and Conditional Types
      3. never and Mapped Types
    4. Template Literal Types
      1. Intrinsic String Manipulation Types
      2. Template Literal Keys
      3. Remapping Mapped Type Keys
    5. Type Operations and Complexity
    6. Summary
  21. Glossary
  22. Index
  23. About the Author

Product information

  • Title: Learning TypeScript
  • Author(s): Josh Goldberg
  • Release date: June 2022
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781098110338