To Sasha and Michael, who might also fall in love with types, someday.
Preface
This is a book for programmers of all walks: professional JavaScript engineers, C# people, Java sympathizers, Python lovers, Ruby aficionados, Haskell nerds. Whatever language(s) you write in, so long as you have some experience programming and know the basics of functions, variables, classes, and errors, this book is for you. Some experience with JavaScript, including a basic knowledge of the Document Object Model (DOM) and the network, will help you along the waywhile we dont dive deep into these concepts, they are a wellspring of excellent examples, and if youre not familiar with them the examples might not make as much sense.
Regardless of what programming languages youve used in the past, what unites all of us is our shared experience of tracking down exceptions, tracing through code line by line to figure out what went wrong and how we can fix it. This is the experience that TypeScript helps prevent by examining your code automatically and pointing out the mistakes you may have missed.
Its OK if you havent worked with a statically typed language before. Ill teach you about types and how to use them effectively to make your programs crash less, document your code better, and scale your applications across more users, engineers, and servers. Ill try to avoid big words when I can, and explain ideas in a way thats intuitive, memorable, and practical, using lots of examples along the way to help keep things concrete.
Thats the thing about TypeScript: unlike a lot of other typed languages, TypeScript is intensely practical. It invents completely new concepts so you can speak more concisely and precisely, letting you write applications in a way thats fun, modern, and safe.
How This Book Is Organized
This book has two aims: to give you a deep understanding of how the TypeScript language works (theory) and provide bucketfuls of pragmatic advice about how to write production TypeScript code (practice).
Because TypeScript is such a practical language, theory quickly turns to practice, and most of this book ends up being a mix of the two, with the first couple of chapters almost entirely theory, and the last few almost completely practice.
Ill start with the basics of what compilers, typecheckers, and types are. Ill then give a broad overview of the different types and type operators in TypeScript, what theyre for, and how you use them. Using what weve learned, Ill cover some advanced topics like TypeScripts most sophisticated type system features, error handling, and asynchronous programming. Finally, Ill wrap up with how to use TypeScript with your favorite frameworks (frontend and backend), migrating your existing JavaScript project to TypeScript, and running your TypeScript application in production.
Most chapters come with a set of exercises at the end. Try to do these yourselftheyll give you a deeper intuition for what we cover than just reading would. Answers for chapter exercises are available online, at https://github.com/bcherny/programming-typescript-answers.
Style
Throughout this book, I tried to stick to a single code style. Some aspects of this style are deeply personalfor example:
I only use semicolons when necessary.
I indent with two spaces.
I use short variable names like a
, f
, or _ where the program is a quick snippet, or where the structure of the program is more important than the details.
Some aspects of the code style, however, are things that I think you should do too. A few of these are:
You should use the latest JavaScript syntax and features (the latest JavaScript version is usually just called esnext). This will keep your code in line with the latest standards, improving interoperability and Googleability, and it can help reduce ramp-up time for new hires. It also lets you take advantage of powerful, modern JavaScript features like arrow functions, promises, and generators.
You should keep your data structures immutable with spreads (...
) most of the time.
You should make sure everything has a type, inferred when possible. Be careful not to abuse explicit types; this will help keep your code clear and terse, and improve safety by surfacing incorrect types rather than bandaiding over them.
You should keep your code reusable and generic. Polymorphism (see ) is your best friend.
Of course, these ideas are hardly new. But TypeScript works especially well when you stick to them. TypeScripts built-in downlevel compiler, support for read-only types, powerful type inference, deep support for polymorphism, and completely structural type system encourage good coding style, while the language remains incredibly expressive and true to the underlying JavaScript.
A couple more notes before we begin.
JavaScript doesnt expose pointers and references; instead it has value and reference types. Values are immutable, and include things like strings, numbers, and booleans, while references point to often-mutable data structures like arrays, objects, and functions. When I use the word value in this book, I usually mean it loosely to refer to either a JavaScript value or a reference.
Lastly, you might find yourself writing less-than-ideal TypeScript code in the wild when interoperating with JavaScript, or incorrectly typed third-party libraries, or legacy code, or if youre in a rush. This book largely presents how you