Any source code or other supplementary material referenced by the author in this book is available to readers on GitHub via the books product page, located at www.apress.com/9781484270103 . For more detailed information, please visit www.apress.com/source-code .
ISBN 978-1-4842-7010-3 e-ISBN 978-1-4842-7011-0
https://doi.org/10.1007/978-1-4842-7011-0
Adam Freeman 2021
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.
The use of general descriptive names, registered names, trademarks, service marks, etc. in this publication does not imply, even in the absence of a specific statement, that such names are exempt from the relevant protective laws and regulations and therefore free for general use.
The publisher, the authors and the editors are safe to assume that the advice and information in this book are believed to be true and accurate at the date of publication. Neither the publisher nor the authors or the editors give a warranty, expressed or implied, with respect to the material contained herein or for any errors or omissions that may have been made. The publisher remains neutral with regard to jurisdictional claims in published maps and institutional affiliations.
Distributed to the book trade worldwide by Apress Media, LLC, 1 New York Plaza, New York, NY 10004, U.S.A. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@springer-sbm.com, or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation.
The Author(s), under exclusive license to APress Media, LLC, part of Springer Nature 2021
A. Freeman Essential TypeScript 4 https://doi.org/10.1007/978-1-4842-7011-0_1
1. Your First TypeScript Application
The best way to get started with TypeScript is to dive in. In this chapter, I take you through a simple development process to create an application that keeps track of to-do items. Later chapters show how TypeScript features work in detail, but a simple example will be enough to demonstrate how the basic TypeScript features work. Dont worry if you dont understand everything in this chapter. The idea is just to get an overall sense of how TypeScript works and how it fits into an application.
Getting Ready for This Book
Four packages are required to get ready for this book. Perform each installation described in the following sections and run the test provided for each of them to ensure that the packages work as they should.
Step 1: Install Node.js
First, download and install Node.js, also known as Node, from to check that Node and NPM are working.
node --version
npm --version
Listing 1-1.
Checking Node and NPM
The output from the first command should be v14.15.4 , indicating that Node is working and the correct version has been installed. The output from the second command should be 6.14.10 , which indicates that NPM is working.
Step 2: Install Git
The second task is to download and install the Git version management tool from to check that Git is working.
git --version
Listing 1-2.
Checking Git
At the time of writing, the latest version of Git for all platforms is 2.30.0.
Step 3: Install TypeScript
The third step is to install the TypeScript package. Use a command prompt to run the command shown in Listing .
npm install --global typescript@4.2.2
Listing 1-3.
Installing the TypeScript Package
Once the package has been installed, run the command shown in Listing to ensure that the compiler was installed correctly.
tsc --version
Listing 1-4.
Testing the TypeScript Compiler
The TypeScript compiler is called tsc , and the output from the command in Listing should be Version 4.2.2 .
Step 4: Install a Programmers Editor
The final step is to install a programmers editor that supports TypeScript. Most popular editors can be used for TypeScript development, but if you dont have a preferred editor, then download and install Visual Studio Code from https://code.visualstudio.com . Visual Studio Code is an open-source, cross-platform code editor that is free to use and is the editor I used while writing the examples for this book.
If you are using Visual Studio Code, run the command code to start the editor or use the program icon created during installation, and you will see the welcome screen shown in Figure . (You may need to add Visual Studio Code to your command prompt path before using the code command.)
Figure 1-1.
The Visual Studio Code welcome screen
Tip
Some editors will let you specify a different version of TypeScript than the one contained in the project, which can cause errors to be displayed in the code editor even when the command-line tools show successful compilation. If you are using Visual Studio Code, for example, you will see the version of TypeScript that is used displayed at the bottom right of the editor window when you edit a TypeScript file. Click the version that is shown, click Select TypeScript Version, and select the version you require.