Pro TypeScript: Application-Scale Javascript Development Second Edition Fenton 2024 Scribd Download
Pro TypeScript: Application-Scale Javascript Development Second Edition Fenton 2024 Scribd Download
com
https://textbookfull.com/product/pro-typescript-
application-scale-javascript-development-second-
edition-fenton/
https://textbookfull.com/product/pro-typescript-application-scale-
javascript-development-2nd-edition-steve-fenton/
textbookfull.com
https://textbookfull.com/product/essential-typescript-4-from-beginner-
to-pro-second-edition-adam-freeman/
textbookfull.com
https://textbookfull.com/product/angular-development-with-typescript-
second-edition-yakov-fain/
textbookfull.com
https://textbookfull.com/product/trigonometry-robert-e-moyer-2/
textbookfull.com
Green Approaches to Biocomposite Materials Science and
Engineering 1st Edition Deepak Verma Et Al.
https://textbookfull.com/product/green-approaches-to-biocomposite-
materials-science-and-engineering-1st-edition-deepak-verma-et-al/
textbookfull.com
https://textbookfull.com/product/antimicrobials-in-agriculture-1st-
edition-arti-gupta-ram-prasad/
textbookfull.com
https://textbookfull.com/product/storms-in-her-head-freud-and-the-
construction-of-hysteria-1st-edition-muriel-dimen-adrienne-harris/
textbookfull.com
https://textbookfull.com/product/the-pragmatic-programmer-your-
journey-to-mastery-2nd-edition-david-thomas/
textbookfull.com
Reclaiming Education Moving Beyond the Culture of Reform
1st Edition Ron Scapp (Auth.)
https://textbookfull.com/product/reclaiming-education-moving-beyond-
the-culture-of-reform-1st-edition-ron-scapp-auth/
textbookfull.com
Steve Fenton
Pro TypeScript
Application-Scale JavaScript Development
2nd ed.
Steve Fenton
Basingstoke, UK
This work is subject to copyright. All rights are solely and exclusively
licensed by the Publisher, whether the whole or part of the material
is concerned, specifically the rights of translation, reprinting, reuse
of illustrations, recitation, broadcasting, reproduction on microfilms
or in any other physical way, and transmission or information storage
and retrieval, electronic adaptation, computer software, or by similar
or dissimilar methodology now known or hereafter developed.
While the advice and information in this book are believed to be true
and accurate at the date of publication, neither the authors nor the
editors nor the publisher can accept any legal responsibility for any
errors or omissions that may be made. The publisher makes no
warranty, express or implied, with respect to the material contained
herein.
Structure
This book is organized into ten chapters and four appendices.
Chapter 1 : TypeScript Language Features: describes the
language features in detail, from simple type annotations to
important structural elements, with stand-alone examples of
how to use each one.
Chapter 2 : Code Organization: clarifies how code can be
organized, loaded, and packaged with a view to growing your
program to millions of lines of code.
Chapter 3 : The Type System: explains the details of working
within TypeScript's structural type system and describes the
details on type erasure, type inference, and ambient
declarations.
Chapter 4 : Object Orientation in TypeScript: introduces the
important elements of object orientation and contains examples
of design patterns and SOLID principles in TypeScript. This
chapter also introduces the concept of mixins with practical
examples.
Chapter 5 : Understanding the Runtime: describes the impact
of scope, callbacks, events, and extensions on your program.
Chapter 6 : Running TypeScript in a Browser: a thorough
walkthrough including working with the Document Object
Model, AJAX, session and local storage, IndexedDB, geolocation,
hardware sensors, and web workers as well as information on
packaging your program for the Web.
Chapter 7 : Running TypeScript on a Server: an explanation of
running programs on a JavaScript server with examples for
Node and a basic end-to-end example application written in
Express and Mongoose.
Chapter 8 : Exceptions, Memory, and Performance: describes
exceptions and exception handling with information on memory
management and garbage collection. It includes a simple
performance testing utility to exercise and measure your
program.
Chapter 9 : Using JavaScript Libraries: explains how to
consume any of the millions of JavaScript libraries from within
your TypeScript program, including information on how to create
your own type definitions and how to migrate your JavaScript
program to TypeScript.
Chapter 10 : Automated Testing: a walkthrough of automated
testing in your TypeScript program with examples written using
the Jest framework.
Appendix 1: JavaScript Quick Reference: an introduction to the
essential JavaScript features for anyone who needs to brush up
on their JavaScript before diving into TypeScript.
Appendix 2: TypeScript Compiler: explains how to use the
compiler on the command line and describes many of the flags
you can pass to customize your build.
Appendix 3: Bitwise Flags: dives into the details of bitwise
flags including the low-level details of how they work as well as
examples using TypeScript enumerations.
Appendix 4: Coding Katas: introduces the concept of coding
katas and provides an example for you to try, along with
techniques you can use to make katas more effective.
Visit https://textbookfull.com
now to explore a rich
collection of eBooks, textbook
and enjoy exciting offers!
The TypeScript Components
TypeScript is made up of three distinct but complementary parts,
which are shown in Figure 2 .
const num = 1;
const str = '0';
// result is 20
const result = strTen * 2;
function process() {
// Error! Cannot find name
'accidentalGlobal;
accidentalGlobal = 5;
}
Lack of Types
The problem with JavaScript isn't that it has no types, because each
variable does have a type; it is just that the type can be changed by
each assignment. A variable may start off as a string, but an
assignment can change it to a number, an object, or even a function.
The real problem here is that the development tools cannot be
improved beyond a reasonable guess about the type of a variable. If
the development tools don’t know the types, the autocompletion and
type hinting is often too general to be useful.
By formalizing type information, TypeScript allows development
tools to supply specific contextual help that otherwise would not be
possible.
Which Problems Are Not Solved?
TypeScript is not a crutch any more than JSLint is a crutch. It
doesn’t hide JavaScript (as CoffeeScript tends to do).
—Ward Bell
Prerequisites
To benefit from the features of TypeScript, you'll need access to an
integrated development environment that supports the syntax and
compiler. The examples in this book were written using Visual Studio
2017, but you can use VS Code, WebStorm/PHPStorm, Eclipse,
Sublime Text, Vi, Emacs, or any other development tools that
support the language; you can even try many of the simpler
examples on the TypeScript Playground provided by Microsoft. I
often use the TypeScript Playground when answering questions on
the language.
From the Visual Studio 2013 Spring Update (Update 2),
TypeScript is a first-class language in Visual Studio. Prior to this, an
additional extension needed to be installed. Although the examples
in this book are shown in Visual Studio, you can use any of the
development tools that were listed above. In particular, Visual Studio
Code is a free cross-platform editor with native TypeScript support -
so you can write your TypeScript code on any machine you have on
hand, regardless of the operating system.
It is also worth downloading and installing Node (which is
required to follow many of the examples) as it will allow you to
access the Node Package Manager and the thousands of modules
and utilities available through it. For example, you can use task
runners such as Grunt and Gulp to watch your TypeScript files and
compile them automatically each time you change them if your
development tools don't do this for you.
Node is free and can be downloaded for multiple platforms from
the official Node website.
https://nodejs.org/
To avoid being greatly sidetracked, I have avoided using any task
runners to perform additional operations outside of Visual Studio,
but once you have mastered TypeScript you will almost certainly
want to add a task runner, such as Gulp or Grunt, to your
development workflow. I have referenced some web dependencies
from the node_modules folder in the examples in this book; but on
a real-world project I would use a task runner to lift and shift the
website dependencies into a different folder that I would be happy
to deploy to a web server. The node_modules folder often contains
a great deal of files that I would not deploy to a web server.
TypeScript Alternatives
TypeScript is not the only alternative to writing to plain JavaScript.
The strongest TypeScript alternative is Babel, a compiler that
exposes the latest ECMAScript features with plugins for down-level
compilation and polyfills to make your program work in current
browsers. The aim of the Babel project is to make the latest features
available much sooner than they otherwise would be, but Babel
doesn't introduce compile-time type checking. Babel also features in
many TypeScript workflows, where Babel is executed after the
TypeScript compiler, rather than using TypeScript to perform the
down-level compilation. You can read about Babel on the official
website:
https://babeljs.io/
For a number of years, CoffeeScript was a popular alternative
with a terse syntax that compiles to sensible JavaScript code.
CoffeeScript doesn't offer many of the additional features that
TypeScript offers, such as static type checking. It is also a very
different language to JavaScript, which means you need to translate
snippets of code you find online into CoffeeScript to use them. In
2017, however, CoffeeScript reached the top three "most dreaded
languages" in the Stack Overflow developer survey (next to VBA,
and Visual Basic 6). In the same survey, TypeScript landed in the top
three most loved languages. You can find out more about
CoffeeScript on the official website:
http://coffeescript.org/
Another alternative is Google's Dart language. Dart has much in
common with TypeScript. It is class-based, object oriented, and
offers optional types that can be checked by a static checker. Dart
was originally conceived as a replacement for JavaScript, and was
only intended to be compiled to JavaScript to provide wide support
in the short term while native support for Dart was added to
browsers.
It seems unlikely at this stage that Dart will get the kind of
browser support that JavaScript has won, so the compile-to-
JavaScript mechanism will likely remain core to Dart's future in the
web browser. The decision by Google to adopt TypeScript for the
Angular project may be indicative of their commitment to Dart,
although it is still described as a long-term project. You can read
about Dart on the official website for the language:
https://www.dartlang.org/
There are also converters that will compile from most languages
to JavaScript, including C#, Ruby, Java, and Haskell. These may
appeal to programmers who are uncomfortable stepping outside of
their primary programming language.
It is also worth bearing in mind that for small applications and
web page widgets, you can defer the decision and write the code in
plain JavaScript. With TypeScript in particular, there is no penalty for
starting in JavaScript as you can simply paste your JavaScript code
into a TypeScript file later on to make the switch. Equally, there is
little penalty for writing small programs in TypeScript, especially if
you already have a workflow in place to generate combined or
minified files each time you save your application.
Summary
TypeScript is an application-scale programming language that
provides early access to proposed new JavaScript features and
powerful additional features like static type checking. You can write
TypeScript programs to run in web browsers or on servers and you
can reuse code between browser and server applications.
TypeScript solves many problems in JavaScript, but it respects
the patterns and implementation of the underlying JavaScript
language, for example, the ability to have dynamic types.
You can use many integrated development environments with
TypeScript, with several providing first-class support including type
checking and autocompletion that will improve your productivity and
help eliminate mistakes at design time.
Key Points
TypeScript is a language, a compiler, and a language service.
You can paste existing JavaScript into your TypeScript program.
Compiling from TypeScript to JavaScript is known specifically as
transpiling.
TypeScript is not the only alternative way of writing JavaScript,
but it has gained incredible traction in its first five years.
Visit https://textbookfull.com
now to explore a rich
collection of eBooks, textbook
and enjoy exciting offers!
Acknowledgments
A book is enjoyable when you work with the right people, and those
people are Gwenan Spearing at Apress and my smart technical
reviewers Rich O’Kelly, Martin Milsom, Dan Horrocks-Burgess, and
Jamie Wright. I am incredibly grateful to Nancy Chen for being so
patient with my sporadic writing habits, last-minute changes, and
vague estimates.
This second edition would not have been possible without the
contributions of all the smart people that helped create the first
book; Mark Jones, Mark Rendle, Basarat Ali Syed, Christine Ricketts,
Boris Yankov, Diullei Gomes, Masahiro Wakame, Jason Jarrett, Bart
van der Schoor, John Reilly, Igor Oleinikov, Luke Hoban, Jonathan
Turner, and of course - Ryan Cavanaugh.
I am also grateful to all of my amazing colleagues, past and
present, who inspire me to keep learning and growing.
Contents
Chapter 1:TypeScript Language Features
Variables
Constants
Types
Type Annotations
Primitive Types
Enumerations
Union Types
Literal Types
Intersection Types
Arrays
Tuple Types
Dictionary Types
Mapped Types
Type Assertions
Type Guards
Discriminated Unions
Operators
Binary Operators
Bitwise Operators
Logical Operators
Type Operators
Destructuring
Spread Operator
Functions
Optional Parameters
Default Parameters
Rest Parameters
Overloads
Arrow Functions
Function Currying
Interfaces
Classes
Constructors
Access Modifiers
Class Heritage
Abstract Classes
Scope
Type Information
Generics
Generic Functions
Generic Interfaces
Generic Classes
Type Constraints
TypeScript Futures
Summary
Key Points
Namespaces
Modules
Module Re-Exporting
Default Exports
Exports Object
Random documents with unrelated
content Scribd suggests to you:
suuntaan, vaan saatiin ainoastaan kiertää eteen sattuvia esteitä ja
sentähden laivamies Bob määrättiin kokkaan, voidakseen ilmoittaa
perämiehelle jokaisen uuden vaaran.
Tämän lisäksi tuli vielä, että tuuli hiljeni ja töin tuskin täytti purjeet.
Jos näin hirmuisessa kuumuudessa meri olisi tyyntynyt, saivat silloin
odottaa surullista aikaa. Toinen vähemmän miellyttävä keksintö oli,
kun merenvirta oli heille vastainen, jota seikkaa eivät voineet keksiä
aukealla vedellä. Vaan mitä enemmän lähenivät rantaa, sitä
selvemmin huomasivat tuskin pääsevänsä paikalta. Vaan kuinka
saattoi käydäkään jos airoilla olisivat pahoitetut kuljettamaan
raskasta parkaasia?
Koko ajan, kun Black puheli, katseli katteini lähellä olevata rantaa
lakkaamatta. Nyt otti hän pitkän, vieressään olevan laivakiikarin ja
tarkasteli samoja tienoita vielä tarkemmin.
Parkaasi tuli yhä lähemmäksi rantaa, jolla jo taisi nähdä tuskin
huomattavan veden kuohun. Ranta näytti melkein veden rajaan
kasvavan metsää ja ainoastaan kaita viileke maata metsän ja veden
välissä olevan korallihiekalla peitetyn. Muutamat kukkulat olivat
peitetyt heikolla kasvullisuudella, vaan enemmän kaukana olevat
näyttivät paljailta, samoin kuin ehkä koko ympärillä oleva maakin,
joka ainoastaan kostealla merenrannalla näyttää runsaampaa
kasvullisuutta.
Aamiainen oli pian syöty; sillä kun syötiin kylmää ruokaa, ei sitä
kauan tarvittu valmistaa. Parkaasi oli myös Maten huolenpidon
kautta pidetty vesillä ja kun taas kaikki istuivat määrätyillä sijoillansa,
soudettiin iloisella mielellä maata kohden.
"Te ette vieläkään usko", sanoi katteini Oilytt, joka myöskin oli
huonolla tuulella, "vaan saatte anteeksi antaa, jos minulla on
toisenlainen ajatus. Tämän lahdekkeen suhteen olemme tosin
erehtyneet; vaan minä muistelen lukeneeni austraalialaisten jokien
suussa melkein aina olevan suolaista vettä, joka muuttuu juotavaksi
vasta ylempänä maan sisällä. Semmoista löydämme ainakin siellä."
"Se on luonnollista⁴.
"No poikaseni", sanoi katteini Oilytt, "jos niin on, niin saat kulettaa
sitte ruokavarojamme, sillä venheessä et myöskään voi olla
miksikään hyödyksi. Siis jäävät kirvesmies ja Jim venheelle, koska
tähän täytyy jättää kunnon merimies; vaan teitä neuvon etenkin,
ett'ette koske puteliani. Me muut seitsemän olemme kylliksi riittävät
tekemään tämän lyhyen retken ja sinä Jim lykkäät venheen ulos niin
kohta kuin olemme maalla ja soudat jonkun matkaa vesille, jossa
taidat laskea ankkurin pohjaan. Jos näet jonkun mustista olennoista,
vaikka en heitä luule parinkymmenen penikulman sisällä olevan
yhtään ainoata, saat ampua kiväärisi. Taitanethan edes sen verran
pyssyä käsitellä?"