
TypeScript Design Patterns
Beschreibung
Alles über E-Books | Antworten auf Fragen rund um E-Books, Kopierschutz und Dateiformate finden Sie in unserem Info- & Hilfebereich.
Alle Preise
Weitere Details
Weitere Ausgaben
Andere Ausgaben

Person
Vilic Vane is a JavaScript engineer with over 8 years of experience in web development. He started following the TypeScript project since it went public, and hes also a contributor of the project. He is now working at Ruff, a startup company building an IoT platform that runs JavaScript on embedded devices.
Inhalt
- Cover
- Copyright
- Credits
- About the Author
- About the Reviewer
- www.PacktPub.com
- Table of Contents
- Preface
- Chapter 1: Tools and Frameworks
- Installing the prerequisites
- Installing Node.js
- Installing TypeScript compiler
- Choosing a handy editor
- Visual Studio Code
- Configuring Visual Studio Code
- Opening a folder as a workspace
- Configuring a minimum build task
- Sublime Text with TypeScript plugin
- Installing Package Control
- Installing the TypeScript plugin
- Other editor or IDE options
- Atom with the TypeScript plugin
- Visual Studio
- WebStorm
- Getting your hands on the workflow
- Configuring a TypeScript project
- Introduction to tsconfig.json
- Compiler options
- target
- module
- declaration
- sourceMap
- jsx
- noEmitOnError
- noEmitHelpers
- noImplicitAny
- experimentalDecorators*
- emitDecoratorMetadata*
- outDir
- outFile
- rootDir
- preserveConstEnums
- strictNullChecks
- stripInternal*
- isolatedModules
- Adding source map support
- Downloading declarations using typings
- Installing typings
- Downloading declaration files
- Option "save"
- Testing with Mocha and Istanbul
- Mocha and Chai
- Writing tests in JavaScript
- Writing tests in TypeScript
- Getting coverage information with Istanbul
- Testing in real browsers with Karma
- Creating a browser project
- Installing Karma
- Configuring and starting Karma
- Integrating commands with npm
- Why not other fancy build tools?
- Summary
- Chapter 2: The Challenge of Increasing Complexity
- Implementing the basics
- Creating the code base
- Defining the initial structure of the data to be synchronized
- Getting data by comparing timestamps
- Two-way synchronizing
- Things that went wrong while implementing the basics
- Passing a data store from the server to the client does not make sense
- Making the relationships clear
- Growing features
- Synchronizing multiple items
- Simply replacing data type with an array
- Server-centered synchronization
- Synchronizing from the server to the client
- Synchronizing from client to server
- Synchronizing multiple types of data
- Supporting multiple clients with incremental data
- Updating the client side
- Updating server side
- Supporting more conflict merging
- New data structures
- Updating client side
- Updating the server side
- Things that go wrong while implementing everything
- Piling up similar yet parallel processes
- Data stores that are tremendously simplified
- Getting things right
- Finding abstraction
- Implementing strategies
- Wrapping stores
- Summary
- Chapter 3: Creational Design Patterns
- Factory method
- Participants
- Pattern scope
- Implementation
- Consequences
- Abstract Factory
- Participants
- Pattern scope
- Implementation
- Consequences
- Builder
- Participants
- Pattern scope
- Implementation
- Consequences
- Prototype
- Singleton
- Basic implementations
- Conditional singletons
- Summary
- Chapter 4: Structural Design Patterns
- Composite Pattern
- Participants
- Pattern scope
- Implementation
- Consequences
- Decorator Pattern
- Participants
- Pattern scope
- Implementation
- Classical decorators
- Decorators with ES-next syntax
- Consequences
- Adapter Pattern
- Participants
- Pattern scope
- Implementation
- Consequences
- Bridge Pattern
- Participants
- Pattern scope
- Implementation
- Consequences
- Façade Pattern
- Participants
- Pattern scope
- Implementation
- Consequences
- Flyweight Pattern
- Participants
- Pattern scope
- Implementation
- Consequences
- Proxy Pattern
- Participants
- Pattern scope
- Implementation
- Consequences
- Summary
- Chapter 5: Behavioral Design Patterns
- Chain of Responsibility Pattern
- Participants
- Pattern scope
- Implementation
- Consequences
- Command Pattern
- Participants
- Pattern scope
- Implementation
- Consequences
- Memento Pattern
- Participants
- Pattern scope
- Implementation
- Consequences
- Iterator Pattern
- Participants
- Pattern scope
- Implementation
- Simple array iterator
- ES6 iterator
- Consequences
- Mediator Pattern
- Participants
- Pattern scope
- Implementation
- Consequences
- Summary
- Chapter 6: Behavioral Design Patterns: Continuous
- Strategy Pattern
- Participants
- Pattern scope
- Implementation
- Consequences
- State Pattern
- Participants
- Pattern scope
- Implementation
- Consequences
- Template Method Pattern
- Participants
- Pattern scope
- Implementation
- Consequences
- Observer Pattern
- Participants
- Pattern scope
- Implementation
- Consequences
- Visitor Pattern
- Participants
- Pattern scope
- Implementation
- Consequences
- Summary
- Chapter 7: Patterns and Architectures in JavaScript and TypeScript
- Promise-based web architecture
- Promisifying existing modules or libraries
- Views and controllers in Express
- Abstraction of responses
- Abstraction of permissions
- Expected errors
- Defining and throwing expected errors
- Transforming errors
- Modularizing project
- Asynchronous patterns
- Writing predictable code
- Asynchronous creational patterns
- Asynchronous middleware and hooks
- Event-based stream parser
- Summary
- Chapter 8: SOLID Principles
- Single responsibility principle
- Example
- Choosing an axis
- Open-closed principle
- Example
- Abstraction in JavaScript and TypeScript
- Refactor earlier
- Liskov substitution principle
- Example
- The constraints of substitution
- Interface segregation principle
- Example
- Proper granularity
- Dependency inversion principle
- Example
- Separating layers
- Summary
- Chapter 9: The Road to Enterprise Application
- Creating an application
- Decision between SPA and "normal" web applications
- Taking team collaboration into consideration
- Building and testing projects
- Static assets packaging with webpack
- Introduction to webpack
- Bundling JavaScript
- Loading TypeScript
- Splitting code
- Loading other static assets
- Adding TSLint to projects
- Integrating webpack and tslint command with npm scripts
- Version control
- Git flow
- Main branches
- Supporting branches
- Feature branches
- Release branches
- Hotfix branches
- Summary of Git flow
- Pull request based code review
- Configuring branch permissions
- Comments and modifications before merge
- Testing before commits
- Git hooks
- Adding pre-commit hook automatically
- Continuous integration
- Connecting GitHub repository with Travis-CI
- Deployment automation
- Passive deployment based on Git server side hooks
- Proactive deployment based on timers or notifications
- Summary
- Index
Choosing a handy editor
A compiler without a good editor won't be enough (if you are not a believer of Notepad). Thanks to the efforts made by the TypeScript community, there are plenty of great editors and IDEs ready for TypeScript development.
However, the choice of an editor could be much about personal preferences. In this section, we'll talk about the installation and configuration of Visual Studio Code and Sublime Text. But other popular editors or IDEs for TypeScript will also be listed with brief introductions.
Visual Studio Code
Visual Studio Code is a free lightweight editor written in TypeScript. And it's an open source and cross-platform editor that already has TypeScript support built-in.
You can download Visual Studio Code from https://code.visualstudio.com/ and the installation will probably take no more than 1 minute.
The following screenshot shows the debugging interface of Visual Studio Code with a TypeScript source file:
Configuring Visual Studio Code
As Code already has TypeScript support built-in, extra configurations are actually not required. But if the version of TypeScript compiler you use to compile the source code differs from what Code has built-in, it could result in unconformity between editing and compiling.
To stay away from the undesired issues this would bring, we need to configure TypeScript SDK used by Visual Studio Code manually:
- Press F1, type
Open User Settings, and enter. Visual Studio Code will open the settings JSON file by the side of a read-only JSON file containing all the default settings. - Add the field
typescript.tsdkwith the path of thelibfolder under the TypeScript package we previously installed:1. Execute the command
npm root -gin your console to get the root of global Node.js modules.2. Append the root path with
/typescript/libas the SDK path.Note
You can also have a TypeScript package installed locally with the project, and use the local TypeScript
libpath for Visual Studio Code. (You will need to use the locally installed version for compiling as well.)
Opening a folder as a workspace
Visual Studio Code is a file- and folder-based editor, which means you can open a file or a folder and start work.
But you still need to properly configure the project to take the best advantage of Code. For TypeScript, the project file is tsconfig.json, which contains the description of source files and compiler options. Know little about tsconfig.json? Don't worry, we'll come to that later.
Here are some features of Visual Studio Code you might be interested in:
- Tasks: Basic task integration. You can build your project without leaving the editor.
- Debugging: Node.js debugging with source map support, which means you can debug Node.js applications written in TypeScript.
- Git: Basic Git integration. This makes comparing and committing changes easier.
Configuring a minimum build task
The default key binding for a build task is Ctrl + Shift + B or cmd + Shift + B on OS X. By pressing these keys, you will get a prompt notifying you that no task runner has been configured. Click Configure Task Runner and then select a TypeScript build task template (either with or without the watch mode enabled). A tasks.json file under the .vscode folder will be created automatically with content similar to the following:
Now create a test.ts file with some hello-world code and run the build task again. You can either press the shortcut we mentioned before or press Ctrl/Cmd + P, type task tsc , and enter.
If you were doing things correctly, you should be seeing the output test.js by the side of test.ts.
There are some useful configurations for tasking that can't be covered. You may find more information on the website of Visual Studio Code: https://code.visualstudio.com/.
From my perspective, Visual Studio Code delivers the best TypeScript development experience in the class of code editors. But if you are not a fan of it, TypeScript is also available with official support for Sublime Text.
Sublime Text with TypeScript plugin
Sublime Text is another popular lightweight editor around the field with amazing performance.
The following image shows how TypeScript IntelliSense works in Sublime Text:
The TypeScript team has officially built a plugin for Sublime Text (version 3 preferred), and you can find a detailed introduction, including useful shortcuts, in their GitHub repository here: https://github.com/Microsoft/TypeScript-Sublime-Plugin.
Note
There are still some issues with the TypeScript plugin for Sublime Text. It would be nice to know about them before you start writing TypeScript with Sublime Text.
Installing Package Control
Package Control is de facto package manager for Sublime Text, with which we'll install the TypeScript plugin.
If you don't have Package Control installed, perform the following steps:
- Click Preferences > Browse Packages..., it opens the Sublime Text packages folder.
- Browse up to the parent folder and then into the Install Packages folder, and download the file below into this folder: https://packagecontrol.io/Package%20Control.sublime-package
- Restart Sublime Text and you should now have a working package manager.
Now we are only one step away from IntelliSense and refactoring with Sublime Text.
Installing the TypeScript plugin
With the help of Package Control, it's easy to install a plugin:
- Open the Sublime Text editor; press Ctrl + Shift + P for Windows and Linux or Cmd + Shift + P for OS X.
- Type
Install Packagein the command palette, select Package Control: Install Package and wait for it to load the plugin repositories. - Type
TypeScriptand select to install the official plugin.
Now we have TypeScript ready for Sublime Text, cheers!
Like Visual Studio Code, unmatched TypeScript versions between the plugin and compiler could lead to problems. To fix this, you can add the field "typescript_tsdk" with a path to the TypeScript lib in the Settings - User file.
Other editor or IDE options
Visual Studio Code and Sublime Text are recommended due to their ease of use and popularity respectively. But there are many great tools from the editor class to full-featured IDE.
Though we're not going through the setup and configuration of those tools, you might want to try them out yourself, especially if you are already working with some of them.
However, the configuration for different editors and IDEs (especially IDEs) could differ. It is recommended to use Visual Studio Code or Sublime Text for going through the workflow and examples in this book.
Atom with the TypeScript plugin
Atom is a cross-platform editor created by GitHub. It has a notable community with plenty of useful plugins, including atom-typescript. atom-typescript is the result of the hard work of Basarat Ali Syed, and it's used by my team before Visual Studio Code. It has many handy features that Visual Studio Code does not have yet, such as module path suggestion, compile on save, and so on.
Like Visual Studio Code, Atom is also an editor based on web technologies. Actually, the shell used by Visual Studio Code is exactly what's used by Atom: Electron, another popular project by GitHub, for building cross-platform desktop applications.
Atom is proud of being hackable, which means you can customize your own Atom editor pretty much as you want.
Then you may be wondering why we turned to Visual Studio Code. The main reason is that Visual Studio Code is being backed by the same company that develops TypeScript, and another reason might be the performance issue with Atom.
But anyway, Atom could be a great choice for a start.
Visual...
Systemvoraussetzungen
Dateiformat: ePUB
Kopierschutz: Adobe-DRM (Digital Rights Management)
Systemvoraussetzungen:
- Computer (Windows; MacOS X; Linux): Installieren Sie bereits vor dem Download die kostenlose Software Adobe Digital Editions (siehe E-Book Hilfe).
- Tablet/Smartphone (Android; iOS): Installieren Sie bereits vor dem Download die kostenlose App Adobe Digital Editions oder die App PocketBook (siehe E-Book Hilfe).
- E-Book-Reader: Bookeen, Kobo, Pocketbook, Sony, Tolino u.v.a.m. (nicht Kindle)
Das Dateiformat ePUB ist sehr gut für Romane und Sachbücher geeignet – also für „fließenden” Text ohne komplexes Layout. Bei E-Readern oder Smartphones passt sich der Zeilen- und Seitenumbruch automatisch den kleinen Displays an.
Mit Adobe-DRM wird hier ein „harter” Kopierschutz verwendet. Wenn die notwendigen Voraussetzungen nicht vorliegen, können Sie das E-Book leider nicht öffnen. Daher müssen Sie bereits vor dem Download Ihre Lese-Hardware vorbereiten.
Bitte beachten Sie: Wir empfehlen Ihnen unbedingt nach Installation der Lese-Software diese mit Ihrer persönlichen Adobe-ID zu autorisieren!
Weitere Informationen finden Sie in unserer E-Book Hilfe.
Dateiformat: PDF
Kopierschutz: Adobe-DRM (Digital Rights Management)
Systemvoraussetzungen:
- Computer (Windows; MacOS X; Linux): Installieren Sie bereits vor dem Download die kostenlose Software Adobe Digital Editions (siehe E-Book Hilfe).
- Tablet/Smartphone (Android; iOS): Installieren Sie bereits vor dem Download die kostenlose App Adobe Digital Editions oder die App PocketBook (siehe E-Book Hilfe).
- E-Book-Reader: Bookeen, Kobo, Pocketbook, Sony, Tolino u.v.a.m. (nicht Kindle)
Das Dateiformat PDF zeigt auf jeder Hardware eine Buchseite stets identisch an. Daher ist eine PDF auch für ein komplexes Layout geeignet, wie es bei Lehr- und Fachbüchern verwendet wird (Bilder, Tabellen, Spalten, Fußnoten). Bei kleinen Displays von E-Readern oder Smartphones sind PDF leider eher nervig, weil zu viel Scrollen notwendig ist.
Mit Adobe-DRM wird hier ein „harter” Kopierschutz verwendet. Wenn die notwendigen Voraussetzungen nicht vorliegen, können Sie das E-Book leider nicht öffnen. Daher müssen Sie bereits vor dem Download Ihre Lese-Hardware vorbereiten.
Bitte beachten Sie: Wir empfehlen Ihnen unbedingt nach Installation der Lese-Software diese mit Ihrer persönlichen Adobe-ID zu autorisieren!
Weitere Informationen finden Sie in unserer E-Book Hilfe.