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

Learning TypeScript 2.x - Second Edition

Book Description

Exploit the features of TypeScript to easily create your very own web applications

About This Book
  • Develop modular, scalable, maintainable, and adaptable web applications by taking advantage of TypeScript
  • Walk through the fundamentals of TypeScript with the help of practical examples
  • Enhance your web development skills using TypeScript 2.x
Who This Book Is For

If you are a developer aiming to learn TypeScript to build attractive web applications, this book is for you. No prior knowledge of TypeScript is required. However, a basic understanding of JavaScript would be an added advantage.

What You Will Learn
  • Understand TypeScript in depth, including its runtime and advanced type system features
  • Master the core principles of the object-oriented programming and functional programming paradigms with TypeScript
  • Save time using automation tools such as Gulp, Webpack, ts-node, and npm scripts
  • Develop robust, modular, scalable, maintainable, and adaptable applications with testing frameworks such as Mocha, Chai, and Sinon.JS
  • Put your TypeScript skills to practice by developing full-stack web applications with Node.js, React and Angular
  • Use the APIs of the TypeScript compiler to build custom code analysis tool
In Detail

TypeScript is an open source and cross-platform statically typed superset of JavaScript that compiles to plain JavaScript and runs in any browser or host.

This book is a step-by-step guide that will take you through the use and benefits of TypeScript with the help of practical examples. You will start off by understanding the basics as well as the new features of TypeScript 2.x. Then, you will learn how to work with functions and asynchronous programming APIs. You will continue by learning how to resolve runtime issues and how to implement TypeScript applications using the Object-oriented programming (OOP) and functional programming (FP) paradigms. Later, you will automate your development workflow with the help of tools such as Webpack. Towards the end of this book, you will delve into some real-world scenarios by implementing some full-stack TypeScript applications with Node.js, React and Angular as well as how to optimize and test them. Finally, you will be introduced to the internal APIs of the TypeScript compiler, and you will learn how to create custom code analysis tools.

Style and approach

This is a step-by-step guide that covers the fundamentals of TypeScript with practical examples.

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. Title Page
  2. Copyright and Credits
    1. Learning TypeScript 2.x Second Edition
  3. Dedication
  4. Packt Upsell
    1. Why subscribe?
    2. PacktPub.com
  5. Contributors
    1. About the author
    2. About the reviewers
    3. Packt is searching for authors like you
  6. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Download the color images
      3. Conventions used
    4. Get in touch
      1. Reviews
  7. Introducing TypeScript
    1. The TypeScript architecture
      1. Design goals
      2. TypeScript components
    2. TypeScript language features
      1. Types
        1. Type inference and optional static type annotations
      2. Variables, basic types, and operators
        1. Variable scope (var, let, and const)
        2. Arithmetic operators
        3. Comparison operators
        4. Logical operators
        5. Bitwise operators
        6. Assignment operators
        7. Spread operator
      3. Flow control statements
        1. The single-selection structure (if)
        2. The double-selection structure (if...else)
        3. The inline ternary operator (?)
        4. The multiple-selection structure (switch)
        5. The expression is tested at the top of the loop (while)
        6. The expression is tested at the bottom of the loop (do...while)
        7. Iterate on each object's properties (for...in)
        8. Iterate values in an iterable (for...of)
        9. Counter-controlled repetition (for)
      4. Functions
      5. Classes
      6. Interfaces
      7. Namespaces
    3. Putting everything together
    4. Summary
  8. Working with Types
    1. The TypeScript type system's characteristics
      1. The line between TypeScript and JavaScript
      2. Type inference
      3. Optional static type annotations
      4. Structural type system
    2. Core features of the TypeScript type system
      1. Union types
      2. Type aliases
      3. Intersection types
      4. Non-nullable types
      5. The --strict mode
      6. The typeof operator
      7. Type guards
      8. Custom type guards
      9. Control flow analysis
    3. Literal types
      1. Discriminated unions
      2. The never type
      3. Enumerations
      4. Object literals
      5. Weak types
      6. The keyof operator
      7. Index signature
      8. Local types
      9. Type casting
    4. Advanced features of the TypeScript type system
      1. Generic types
      2. Generic constraints
      3. Mapped types
      4. Lookup types
      5. Mapped type modifiers
      6. Conditional types
      7. The infer keyword
      8. Built-in conditional types
      9. The polymorphic this type
      10. Ambient declarations
      11. Type declarations – .d.ts
    5. Summary
  9. Working with Functions
    1. Working with functions in TypeScript
      1. Function declarations and function expressions
      2. Function types
      3. Trailing commas in function arguments
      4. Functions with optional parameters
      5. Functions with default parameters
      6. Functions with REST parameters
      7. Function overloading
      8. Specialized overloading signature
      9. Function scope
      10. Immediately invoked functions
      11. Tag functions and tagged templates
    2. Asynchronous programming in TypeScript
      1. Callbacks and higher-order functions
      2. Arrow functions
      3. Callback hell
      4. Promises
      5. Covariant checking in callback parameters
      6. Generators
      7. Asynchronous functions – async and await
      8. Asynchronous generators
      9. Asynchronous iteration (for await...of)
      10. Delegating to another generator (yield*)
    3. Summary
  10. Object-Oriented Programming with TypeScript
    1. Classes
      1. Strict property initialization
      2. Inheritance
        1. Depth of the inheritance tree (DIT)
      3. Access modifiers
        1. The public access modifier
        2. The private access modifier
        3. The protected access modifier
      4. Parameter properties
      5. Class expressions
      6. Static members
      7. Optional members
      8. Read-only properties
      9. Method overriding
      10. Generic classes
        1. Generic constraints
        2. Multiple types in generic type constraints
        3. The new operator in generic types
      11. Association, aggregation, and composition
        1. Association
        2. Aggregation
        3. Composition
      12. Mixins (multiple inheritance)
        1. The diamond problem
        2. Implementing mixins
      13. Iterables
    2. Abstract classes
    3. Interfaces
    4. SOLID principles, encapsulation, and polymorphism
      1. SOLID – the single responsibility principle
        1. Encapsulation
      2. SOLID – the open/closed principle
        1. Polymorphism
      3. SOLID – the Liskov substitution principle 
      4. SOLID – the interface segregation principle
      5. SOLID – the dependency inversion principle
    5. Summary
  11. Working with Dependencies
    1. Third-party dependencies
      1. Package management tools
        1. The rise and fall of package management tools
        2. npm
        3. Type definitions
          1. Modules with native support for TypeScript
          2. Modules with external support for TypeScript
          3. Modules with no support for TypeScript
          4. The ECMAScript specification type definitions (lib.d.ts)
          5. External TypeScript helpers (tslib)
    2. Internal modules (module and namespace)
      1. Nested internal modules
      2. Cross-file internal modules
      3. Internal module aliases
      4. Compiling internal modules
    3. External modules
      1. Module loaders and module definition syntaxes
      2. External modules at design time and runtime
      3. ES6 modules (runtime and design time)
      4. Legacy external modules (design time only)
      5. AMD modules (runtime only)
      6. CommonJS modules (runtime only)
      7. UMD modules (runtime only)
      8. SystemJS modules (runtime only)
      9. Modules summary
    4. Managing dependencies in OOP
      1. Dependency injection versus dependency inversion
      2. Inversion of control containers
        1. InversifyJS basics
      3. Circular dependencies
    5. Summary
  12. Understanding the Runtime
    1. The execution environment
    2. Understanding the event loop
      1. Frames
      2. Stack
      3. Queue
      4. Heap
      5. The event loop
    3. The this operator
      1. The this operator in the global context
      2. The this operator in a function context
      3. The call, apply, and bind methods
    4. Prototypes
      1. Instance properties versus class properties
      2. Prototypal inheritance
      3. The prototype chain and property shadowing
      4. Accessing the prototype of an object
      5. The new operator
    5. Closures
      1. Static variables powered by closures
      2. Private members powered by closures
    6. Summary
  13. Functional Programming with TypeScript
    1. FP concepts
      1. Pure functions
      2. Side effects
      3. Referential transparency
      4. Immutability
      5. Functions as first-class citizens
      6. Lambda expressions
      7. Function arity
      8. Higher-order functions
      9. The benefits of FP
      10. Is TypeScript a FP language?
    2. FP techniques
      1. Composition
      2. Partial application
      3. Currying
      4. Pipes
      5. Pointfree style
      6. Recursion
    3. Category theory
      1. Functor
        1. Applicative
        2. Maybe
        3. Either
        4. Monad
    4. Real-world FP
      1. Immutable.js
      2. Ramda
      3. React and MobX
    5. Summary
  14. Working with Decorators
    1. Prerequisites
    2. Annotations versus decorators
      1. The class decorators
      2. The method decorators
      3. The property decorators
      4. The parameter decorators
      5. Decorators with arguments
      6. The reflect metadata API
      7. The decorator factory
    3. Summary
  15. Automating Your Development Workflow
    1. A modern development workflow
    2. Prerequisites
      1. Node.js
      2. Visual Studio Code
      3. Git and GitHub
      4. Companion source code
    3. Source control tools
    4. Package management tools
    5. The TypeScript compiler
    6. Linting tools
    7. Working with npm scripts
    8. Gulp
      1. Controlling the gulp task execution order
        1. Passing in a callback to the task definition function
        2. Returning a promise
        3. Returning a stream
    9. Webpack
    10. Webpack development server
    11. Unit testing and test coverage
    12. Visual Studio Code
      1. Quick fixes
      2. Debugging utilities
      3. Source control utilities
    13. ts-node
    14. Continuous integration (CI) tools
    15. Scaffolding tools
    16. Why does the command line win?
    17. Summary
  16. Node.js Development with TypeScript
    1. Understanding Node.js
      1. Understanding non-blocking I/O
      2. The main components of Node.js
        1. V8
        2. Libuv
        3. Bindings
        4. The Node.js core API (node-core)
      3. Node.js environment versus browser environment
    2. The Node.js ecosystem
      1. The Node.js core API
      2. The style of the Node.js core API
      3. The npm ecosystem
    3. Setting up Node.js
    4. Node.js development
      1. Working with the filesystem
      2. Working with databases
      3. Working with REST APIs
        1. Hello world (http)
        2. Hello world (Express.js)
        3. Routing with Express
        4. Express middleware
        5. Architecting Node.js application – the MVC design pattern
          1. Model
          2. Repository
          3. Controller
          4. Database
          5. View
          6. Index
      4. Controllers and routing with inversify-express-utils
        1. Model, repository, database, and view
        2. Types
        3. Controller
        4. InversifyJS configuration
        5. Index
      5. Other applications of Node.js
    5. Summary
  17. Frontend Development with React and TypeScript
    1. Working with React
      1. About the sample application
      2. Serving a React application with Node.js
      3. Working with react-dom and JSX
      4. Working with the react-router
      5. Working with React components
        1. Components as classes
        2. Properties and state
        3. Functional stateless components
        4. React component life cycle
        5. Smart components and dumb components
    2. Working with MobX
      1. Understanding the MobX architecture
      2. Working with actions and observables
      3. Dependency injection in MobX
      4. MobX alternatives
    3. Development tools
    4. Summary
  18. Frontend Development with Angular and TypeScript
    1. Working with Angular
    2. About the sample application
    3. Serving an Angular application with Node.js
    4. Bootstrapping an Angular application
    5. Working with NgModules
    6. Working with Angular components
      1. Our first component
      2. Components and directives
      3. Data binding
      4. Working with @Attribute and @Input
      5. Using structural directives
      6. Using the <ng-content> directive
      7. Working with @Output and EventEmitter
      8. Working with the component's host
      9. Working with the Angular router
      10. Angular component life cycle hooks
      11. Working with services
      12. Smart components and dumb components
      13. Dependency injection in Angular
    7. Summary
  19. Application Performance
    1. Prerequisites
      1. Google Chrome
      2. Node.js
    2. Performance and resources
    3. Common performance metrics
      1. Availability
      2. Response time
      3. Processing speed
      4. Bandwidth
      5. Latency
      6. Scalability
    4. Performance analysis
      1. Network performance analysis
      2. Network performance and user experience
        1. Network performance best practices and rules
      3. GPU performance analysis
        1. Frames per second (FPS)
      4. CPU performance analysis
      5. Memory performance analysis
      6. The garbage collector
      7. Performance analysis in Node.js applications
    5. Performance automation
      1. Performance optimization automation
      2. Performance monitoring automation
      3. Performance testing automation
    6. Exception handling
      1. The Error class
      2. The try...catch statements and throw statements
    7. Summary
  20. Application Testing
    1. Testing terminology
      1. Assertions
      2. Specs
      3. Test cases
      4. Suites
      5. Spies
      6. Dummies
      7. Stubs
      8. Mocks
      9. Test coverage
    2. Prerequisites
      1. Mocha
      2. Chai
      3. Sinon.JS
      4. nyc
      5. Webpack
      6. Enzyme
      7. SuperTest
      8. PM2
      9. Nightwatch.js and ChromeDriver
    3. Testing methodologies
      1. Test-driven development (TDD)
      2. Behavior-driven development (BDD)
    4. Tests plans and test types
      1. Unit tests
      2. Integration tests
      3. Regression tests
      4. Performance and load tests
      5. End-to-end (e2e) tests
      6. User-acceptance tests (UAT)
    5. The example application
    6. Unit tests and integration tests with Mocha
      1. Back to basics
      2. Testing asynchronous code
      3. Asserting exceptions
      4. Testing a web service with SuperTest
      5. Working with tests suites
      6. Isolating components with Sinon.JS
      7. jsdom
      8. Testing React web components with Enzyme
      9. TDD versus BDD with Mocha and Chai
    7. End-to-end tests with Nightwatch.js
    8. Summary
  21. Working with the TypeScript Compiler and the Language Services
    1. The TypeScript compiler's internal architecture
      1. Scanner
      2. Lexemes and tokens
      3. Parser
      4. AST
      5. Symbols
      6. Binder
      7. Type checker
      8. Emitter
      9. Language service
    2. Understanding the abstract syntax tree (AST)
      1. TypeScript AST Viewer
      2. Sample application
        1. interfaces.ts
        2. katana.ts
        3. ninja.ts
        4. main.ts
        5. broken.ts
      3. Traversing the TypeScript AST
      4. Working with ts-simple-ast
        1. Traversing the AST with ts-simple-ast
        2. Diagnostics with ts-simple-ast
        3. Accessing class details with ts-simple-ast
        4. Accessing module details with ts-simple-ast
        5. Accessing the language services API
    3. Implementing a yUML compiler
    4. VS Code extensions
    5. Summary
  22. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think