Grynhaus Ben - The TypeScript Workshop. A Practical Guide To Confident, Effective TypeScript Programming.pdf

(10287 KB) Pobierz
The TypeScript
Workshop
A practical guide to confident, effective
TypeScript programming
Ben Grynhaus, Jordan Hudgens, Rayon Hunte, Matt Morgan,
and Wekoslav Stefanovski
The TypeScript Workshop
Copyright © 2021 Packt Publishing
All rights reserved. No part of this course may be reproduced, stored in a retrieval
system, or transmitted in any form or by any means, without the prior written
permission of the publisher, except in the case of brief quotations embedded in
critical articles or reviews.
Every effort has been made in the preparation of this course to ensure the accuracy
of the information presented. However, the information contained in this course
is sold without warranty, either express or implied. Neither the authors nor Packt
Publishing, and its dealers and distributors will be held liable for any damages caused
or alleged to be caused directly or indirectly by this course.
Packt Publishing has endeavored to provide trademark information about all of the
companies and products mentioned in this course by the appropriate use of capitals.
However, Packt Publishing cannot guarantee the accuracy of this information.
Authors:
Ben Grynhaus, Jordan Hudgens, Rayon Hunte, Matt Morgan,
and Wekoslav Stefanovski
Reviewers:
Yusuf Salman and Cihan Yakar
Managing Editor:
Mahesh Dhyani
Acquisitions Editors:
Royluis Rodrigues and Sneha Shinde
Production Editor:
Shantanu Zagade
Editorial Board:
Megan Carlisle, Mahesh Dhyani, Heather Gopsill, Manasa Kumar,
Alex Mazonowicz, Monesh Mirpuri, Bridget Neale, Abhishek Rane, Brendan Rodrigues,
Ankita Thakur, Nitesh Thakur, and Jonathan Wray
First published: July 2021
Production reference: 1280721
ISBN: 978-1-83882-849-3
Published by Packt Publishing Ltd.
Livery Place, 35 Livery Street
Birmingham B3 2PB, UK
Table of Contents
Preface
Chapter 1:
TypeScript Fundamentals
i
1
Introduction .............................................................................................. 2
The Evolution of TypeScript ........................................................................ 2
Design Goals of TypeScript ......................................................................... 5
Getting Started with TypeScript ............................................................. 6
The TypeScript Compiler
............................................................................. 7
Setting Up a TypeScript Project .................................................................. 8
Exercise 1.01: Using tsconfig.json and Getting Started
with TypeScript ............................................................................................. 9
Types and Their Uses ................................................................................. 11
TypeScript and Functions .................................................................... 13
Exercise 1.02: Working with Functions in TypeScript ............................ 19
TypeScript and Objects ........................................................................ 22
Exercise 1.03: Working with Objects ........................................................ 26
Basic Types ............................................................................................ 28
Exercise 1.04: Examining typeof
............................................................... 30
Strings .......................................................................................................... 31
Numbers
..................................................................................................... 32
Booleans ..................................................................................................... 33
Arrays .......................................................................................................... 33
Tuples .......................................................................................................... 36
Schwartzian transform
.....................................................................................37
Exercise 1.05: Using Arrays and Tuples to Create
an Efficient Sort of Objects
....................................................................... 39
Enums
.......................................................................................................... 43
Any and Unknown ..................................................................................... 45
Null and Undefined
................................................................................... 47
Never ........................................................................................................... 48
Function Types ........................................................................................... 49
Making Your Own Types ...................................................................... 50
Exercise 1.06: Making a Calculator Function .......................................... 51
Activity 1.01: Creating a Library for Working with Strings .................... 55
Summary
................................................................................................ 58
Chapter 2: Declaration Files
61
Introduction ........................................................................................... 62
Declaration Files ................................................................................... 62
Exercise 2.01: Creating a Declaration File from Scratch
....................... 64
Exceptions ................................................................................................... 71
Third-Party Code Libraries ................................................................... 73
DefinitelyTyped
.......................................................................................... 73
Analyzing an External Declaration File .................................................... 74
Exercise 2.02: Creating Types with External Libraries ........................... 76
Development Workflow with DefinitelyTyped
....................................... 80
Exercise 2.03: Creating a Baseball Lineup Card Application ................. 81
Activity 2.01: Building a Heat Map Declaration File ............................... 83
Summary
................................................................................................ 84
Zgłoś jeśli naruszono regulamin