You are previewing Pro TypeScript.
O'Reilly logo
Pro TypeScript

Book Description

JavaScript is everywhere, both as a pure language and in popular libraries like Angular, jQuery and Knockout, but users of modern object-oriented languages like Java and C# often find JavaScript frustrating to use and hard to extend to large-scale applications. TypeScript is an innovative open source language from Microsoft that combines powerful language features and enhanced tooling support with the key attractions of JavaScript as a flexible, dynamic language that can run in any browser and on any operating system. Pro TypeScript tells you everything you need to know about this exciting new language and how to use it in your applications.

Starting with an introduction to the language and its features, the book takes you through some of the major features of TypeScript in depth, from working with the type system through object-orientation to understanding the runtime and the TypeScript compiler. The book then covers some of the factors you need to consider when running a TypeScript application in the browser, including interacting with the DOM, making asynchronous requests, and working with useful browser APIs, followed by a demonstration of server-side TypeScript using the popular Node.js framework.

Because TypeScript compiles to plain JavaScript, exception handling, memory management and garbage collection can differ depending on where you run your program, so these topics get a chapter to themselves. You'll also find out how to include popular JavaScript frameworks in your applications, so you can combine the benefits of TypeScript with some of the best JavaScript code thats already out there waiting to be used. The final chapter gives an overview of automated testing for TypeScript applications.

Pro TypeScript offers a balanced and practical guide to a language that will transform your experience of JavaScript development.

Table of Contents

  1. Cover
  2. Title
  3. Copyright
  4. Dedication
  5. Contents at a Glance
  6. Contents
  7. About the Author
  8. Acknowledgments
  9. Introduction
  10. Chapter 1: TypeScript Language Features
    1. JavaScript Is Valid TypeScript
    2. Variables
    3. Types
      1. Type Annotations
      2. Primitive Types
      3. Arrays
      4. Enumerations
      5. Type Assertions
    4. Operators
      1. Increment and Decrement
      2. Binary Operators
      3. Bitwise Operators
      4. Logical Operators
      5. Type Operators
    5. Functions
      1. Optional Parameters
      2. Default Parameters
      3. Rest Parameters
      4. Overloads
      5. Specialized Overload Signatures
      6. Arrow Functions
    6. Interfaces
    7. Classes
      1. Constructors
      2. Access Modifiers
      3. Properties and Methods
      4. Class Heritage
      5. Scope
      6. Type Information
    8. Modules
      1. Internal Modules
      2. External Modules
      3. Module Loading
      4. Export Assignments
      5. Module Merging
    9. Generics
      1. Generic Functions
      2. Generic Interfaces
      3. Generic Classes
      4. Type Constraints
    10. TypeScript Futures
    11. Summary
  11. Chapter 2: The Type System
    1. Type Systems
    2. Optional Static Types
    3. Structural Typing
    4. Type Erasure
    5. Type Inference
      1. Best Common Type
      2. Contextual Types
      3. Widened Types
      4. When to Annotate
    6. Duplicate Identifiers
    7. Type Checking
    8. Ambient Declarations
      1. Declaration Files
      2. Definitely Typed
    9. Summary
    10. Key Points
  12. Chapter 3: Object Orientation in TypeScript
    1. Object Orientation in TypeScript
      1. Open Recursion
      2. Encapsulation
      3. Delegation
      4. Polymorphism
    2. SOLID Principles
      1. The Single Responsibility Principle (SRP)
      2. The Open–Closed Principle (OCP)
      3. The Liskov Substitution Principle (LSP)
      4. The Interface Segregation Principle (ISP)
      5. The Dependency Inversion Principle (DIP)
    3. Design Patterns
      1. The Strategy Pattern
      2. The Abstract Factory Pattern
      3. Practical Example
    4. Mixins
      1. TypeScript Mixins
      2. When to Use Mixins
      3. Restrictions
    5. Summary
    6. Key Points
  13. Chapter 4: Understanding the Runtime
    1. Runtime Features
    2. Scope
      1. Variable Hoisting
      2. Callbacks
      3. Passing Functions as Arguments
    3. Events
      1. TypeScript’s Custom-Event Mechanism
      2. Event Phases
    4. Extending Objects
      1. Extending the Prototype
      2. Sealing Objects
      3. Alternatives to Extending
    5. Summary
    6. Key Points
  14. Chapter 5: Running TypeScript in a Browser
    1. The Anatomy of a Web Browser
      1. Reflows
      2. The Interesting Components
    2. The Document Object Model
      1. Finding Elements
      2. Changing Elements
      3. Events
      4. Frameworks and Libraries
    3. Network
      1. AJAX
      2. WebSockets
      3. Real-Time Communications
    4. Storage
      1. Session Storage
      2. Local Storage
      3. Storage Restrictions
      4. IndexedDB
      5. Storage Roundup
    5. Geolocation
    6. Sensors
      1. Battery Status
      2. Proximity Sensor
      3. Light Sensor
      4. Motion and Orientation
      5. Temperature, Noise, and Humidity
      6. Sensor Roundup
    7. Web Workers
    8. Packaging Your Program
    9. Summary
    10. Key Points
  15. Chapter 6: Running TypeScript on a Server
    1. Install Node
    2. Creating a New Project
    3. Simple Node Program
    4. Request Information
    5. Using Express to Write Applications
      1. Simple Express Program
      2. Multiple Routes
      3. Handling Errors
      4. Express Book Project
    6. Summary
    7. Key Points
  16. Chapter 7: Exceptions, Memory, and Performance
    1. Exceptions
      1. Throwing Exceptions
      2. Exception Handling
    2. Memory
      1. Releasing Resources
      2. Garbage Collection
    3. Performance
    4. Summary
    5. Key Points
  17. Chapter 8: Using JavaScript Libraries
    1. Creating Type Definitions
      1. Creating a TypeScript Application with Knockout
      2. Silencing the Compiler
      3. Iteratively Improving Type Definitions
    2. Converting a JavaScript Application
    3. Summary
    4. Key Points
  18. Chapter 9: Automated Testing
    1. Framework Choices
    2. Testing with Jasmine
      1. Installing Jasmine
      2. The First Specification
      3. Driving the Implementation
      4. Refactoring
      5. Isolating Dependencies
    3. Summary
    4. Key Points
  19. Appendix 1: JavaScript Quick Reference
    1. Variables
    2. Functions
    3. Conditional Statements
    4. Loops
  20. Appendix 2: TypeScript Compiler
    1. Getting Help
    2. Common Flags
      1. Module Kind
      2. ECMAScript Target Version
      3. Generate Declarations
      4. Remove Comments
      5. Combined Output
      6. No Implicit Any
  21. Appendix 3: Bitwise Flags
    1. Bit Flags Explained
    2. Bitwise Operations
  22. Appendix 4: Coding Katas
    1. Performing a Kata
    2. The Fizz Buzz Kata
      1. Requirement 1
      2. Requirement 2
      3. Requirement 3
      4. Requirement 4
      5. Requirement 5
      6. Fizz Buzz Summary
    3. Summary
  23. Index