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

Book Description

Build enterprise-ready, industrial strength web applications using TypeScript and leading JavaScript frameworks

In Detail

The TypeScript compiler and language has brought JavaScript development up to the enterprise level, yet still maintains backward compatibility with existing JavaScript browsers and libraries.

Packed with practical code samples, this book brings the benefits of strongly typed, object-oriented programming and design principles into the JavaScript development space. Starting with core language features, and working through more advanced topics such as generics and modules, you will learn how to gain maximum benefit from your JavaScript development with TypeScript. With a strong focus on test-driven development and coverage of many popular JavaScript frameworks, you can fast-track your TypeScript knowledge to a professional level. By the end of this book, you will be able to confidently implement a TypeScript application from scratch.

What You Will Learn

  • Gain an insight into core and advanced TypeScript language features including inheritance and generics

  • Integrate your existing JavaScript code and third-party JavaScript libraries by writing and using declaration files

  • Write TypeScript code to target popular JavaScript frameworks such as jQuery, Backbone, Angular, Node, and Marionette

  • Create extensive testing suites for your application, including unit testing, integration testing, and browser automation with Jasmine and Selenium

  • Organize your application code using modules, and utilize Asynchronous Module Loading with require.js

  • Explore advanced object-oriented techniques including dependency injection and strongly typed domain events

  • Build a complete single-page web application with TypeScript and Marionette, incorporating object-oriented design patterns along the way

  • Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

    Table of Contents

    1. Mastering TypeScript
      1. Table of Contents
      2. Mastering TypeScript
      3. Credits
      4. About the Author
      5. Acknowledgement
      6. About the Reviewers
      7. www.PacktPub.com
        1. Support files, eBooks, discount offers, and more
          1. Why subscribe?
          2. Free access for Packt account holders
      8. Preface
        1. What this book covers
        2. What you need for this book
        3. Who this book is for
        4. Conventions
        5. Reader feedback
        6. Customer support
          1. Downloading the example code
        7. Downloading the color images of this book
          1. Errata
          2. Piracy
          3. Questions
      9. 1. TypeScript – Tools and Framework Options
        1. What is TypeScript?
          1. EcmaScript
          2. The benefits of TypeScript
            1. Compiling
            2. Strong Typing
              1. TypeScript's "syntactic sugar"
            3. Type definitions for popular JavaScript libraries
              1. Definitely Typed
            4. Encapsulation
              1. TypeScript classes generate closures
            5. Public and private accessors
        2. TypeScript IDEs
          1. Visual Studio 2013
            1. Creating a Visual Studio Project
            2. Default project settings
            3. Debugging in Visual Studio
          2. WebStorm
            1. Creating a WebStorm project
            2. Default files
            3. Running the web page in Chrome
            4. Debugging in Chrome
          3. Brackets
            1. Installing Brackets
            2. Creating a Brackets project
            3. Using Brackets live preview
            4. Creating a TypeScript file
            5. Compiling our TypeScript
            6. Using Grunt
            7. Debugging in Chrome
        3. Summary
      10. 2. Types, Variables and Function Techniques
        1. Basic types
          1. JavaScript is not strongly typed
          2. TypeScript is strongly typed
          3. Type syntax
          4. Inferred typing
          5. Duck-typing
          6. Arrays
          7. The any type
          8. Explicit casting
          9. Enums
            1. Const enums
        2. Functions
          1. Anonymous functions
          2. Optional parameters
          3. Default parameters
          4. The arguments variable
          5. Function callbacks
          6. Function signatures
          7. Function callbacks and scope
          8. Function overloads
          9. Union types
            1. Type guards
            2. Type aliases
        3. Summary
      11. 3. Interfaces, Classes and Generics
        1. Interfaces
        2. Classes
          1. Class constructors
          2. Class functions
          3. Interface function definitions
        3. Inheritance
          1. Interface inheritance
          2. Class inheritance
          3. Function and constructor overloading with super
          4. JavaScript closures
        4. The Factory Design Pattern
          1. Business requirements
          2. What the Factory Design Pattern does
            1. The IPerson interface and the Person base class
            2. Specialist classes
            3. The Factory class
          3. Using the Factory class
        5. Class modifiers
          1. Constructor access modifiers
          2. Class property accessors
          3. Static functions
          4. Static properties
        6. Generics
          1. Generic syntax
          2. Instantiating generic classes
          3. Using the type T
          4. Constraining the type of T
          5. Generic interfaces
          6. Creating new objects within generics
        7. Runtime type checking
          1. Reflection
          2. Checking an object for a function
          3. Interface checking with generics
        8. Summary
      12. 4. Writing and Using Declaration Files
        1. Global variables
        2. Using JavaScript code blocks in HTML
          1. Structured data
        3. Writing your own declaration file
          1. The module keyword
          2. Interfaces
          3. Function overrides
          4. Rounding out our definition file
        4. Module merging
        5. Declaration Syntax Reference
          1. Function overrides
            1. The JavaScript syntax
            2. The declaration file syntax
          2. Nested namespaces
            1. The JavaScript syntax
            2. The declaration file syntax
          3. Classes
            1. The JavaScript syntax
            2. The declaration file syntax
          4. Class namespaces
            1. The JavaScript syntax
            2. The declaration file syntax
          5. Class constructor overloads
            1. The JavaScript syntax
            2. The declaration file syntax
          6. Class properties
            1. The JavaScript syntax
            2. The declaration file syntax
          7. Class functions
            1. The JavaScript syntax
            2. The declaration file syntax
          8. Static properties and functions
            1. The JavaScript syntax
            2. The declaration file syntax
          9. Global functions
            1. The JavaScript syntax
            2. The declaration file syntax
          10. Function signatures
            1. The JavaScript syntax
            2. The declaration file syntax
          11. Optional properties
            1. The JavaScript syntax
            2. The declaration file syntax
          12. Merging functions and modules
            1. The JavaScript syntax
            2. The declaration file syntax
        6. Summary
      13. 5. Third Party Libraries
        1. Downloading definition files
        2. Using NuGet
          1. Using the Extension Manager
          2. Installing declaration files
          3. Using the Package Manager Console
            1. Installing packages
            2. Searching for package names
            3. Installing a specific version
        3. Using TypeScript Definition Manager
          1. Querying for packages
          2. Using wildcards
          3. Installing definition files
        4. Using third party libraries
          1. Choosing a JavaScript framework
        5. Backbone
          1. Using inheritance with Backbone
          2. Using interfaces
          3. Using generic syntax
          4. Using ECMAScript 5
          5. Backbone TypeScript compatibility
        6. Angular
          1. Angular classes and $scope
          2. Angular TypeScript compatibility
        7. Inheritance – Angular versus Backbone
          1. Angular 2.0
        8. ExtJs
          1. Creating classes in ExtJs
          2. Using type casting
          3. ExtJs specific TypeScript compiler
        9. Summary
      14. 6. Test Driven Development
        1. Test Driven Development
        2. Unit, integration and acceptance tests
          1. Unit tests
          2. Integration tests
          3. Acceptance tests
        3. Using continuous integration
          1. Benefits of continuous integration
          2. Selecting a build server
            1. Team Foundation Server
            2. Jenkins
            3. TeamCity
        4. Unit testing frameworks
        5. Jasmine
          1. A simple Jasmine test
          2. Jasmine SpecRunner.html file
          3. Matchers
          4. Test startup and teardown
          5. Data-driven tests
          6. Using spies
          7. Using spies as fakes
          8. Asynchronous tests
          9. Using the done() function
          10. Jasmine fixtures
          11. DOM events
        6. Jasmine runners
          1. Testem
          2. Karma
          3. Protractor
            1. Using Selenium
        7. Integration tests
          1. Simulating integration tests
          2. Detailed test results
          3. Logging test results
          4. Finding page elements
          5. Working with page elements in Jasmine
        8. Summary
      15. 7. Modularization
        1. CommonJs
          1. Setting up Node in Visual Studio
          2. Creating a Node module
          3. Using a Node module
          4. Chaining asynchronous functions
        2. Using AMD
          1. Backbone
          2. Models, collections and views
          3. Creating a model
          4. The require.config file
          5. Fixing Require config errors
            1. Incorrect dependencies
            2. 404 errors
          6. Using Backbone.Collections
          7. Backbone views
          8. Using the Text plugin
          9. Rendering a collection
          10. Creating an application
          11. Using jQuery plugins
        3. Summary
      16. 8. Object-oriented Programming with TypeScript
        1. Program to an interface
        2. SOLID principles
          1. Single Responsibility
          2. Open Closed
          3. Liskov Substitution
          4. Interface Segregation
          5. Dependency Inversion
        3. Building a Service Locator
          1. The problem space
          2. Creating a Service
        4. Dependency Resolution
          1. Service Location
          2. Dependency Injection
          3. Service Location versus Dependency Injection
        5. A Service Locator
          1. Named interfaces
          2. Registering classes against named interfaces
          3. Using the Service Locator
          4. Testability
        6. The Domain Events Pattern
          1. Problem space
          2. Message and Handler Interfaces
          3. Multiple Event Handlers
          4. Firing an event
          5. Registering an Event handler for an Event
          6. Displaying error notifications
        7. Summary
      17. 9. Let's Get Our Hands Dirty
        1. Marionette
        2. Bootstrap
        3. Board Sales
        4. Page layout
          1. Installing Bootstrap
          2. Using Bootstrap
        5. Data structure
          1. Data interfaces
          2. Integration tests
          3. Traversing a collection
            1. Finding manufacturer names
            2. Finding board types
          4. Filtering a Collection
        6. Marionette application, regions and layouts
          1. Loading the main collection
          2. Marionette views
            1. The ManufacturerCollectionView class
            2. The ManufacturerView class
            3. The BoardView class
            4. The BoardSizeView class
            5. Filtering using the IFilterProvider interface
            6. The FilterCollection class
          3. Filtering views
          4. DOM events in Marionette
            1. Triggering a Detail view event
            2. Rendering the BoardDetailView
        7. The State Design Pattern
          1. Problem space
          2. State class diagram
            1. Concrete State classes
          3. The Mediator class
            1. Moving to a new State
          4. Implementing the IMediatorFunctions interface
          5. Triggering State changes
        8. Summary
      18. Index