Preface

My journey to TypeScript was not a direct or quick one. I started off in school primarily writing Java, then C++, and like many new developers raised on statically typed languages, I looked down on JavaScript as “just” the sloppy little scripting language people throw onto websites.

My first substantial project in the language was a silly remake of the original Super Mario Bros. video game in pure HTML5/CSS/JavaScript and, typical of many first projects, was an absolute mess. In the beginning of the project I instinctively disliked JavaScript’s weird flexibility and lack of guardrails. It was only towards the end that I really began to respect JavaScript’s features and quirks: its flexibility as a language, its ability to mix and match small functions, and its ability to just work in user browsers within seconds of page load.

By the time I finished that first project, I had fallen in love with JavaScript.

Static analysis (tools to analyze your code without running it) such as TypeScript also gave me a queasy gut feeling at first. JavaScript is so breezy and fluid, I thought, why bog ourselves down with rigid structures and types?. Were we reverting back to the worlds of Java and C++ that I had left behind?

It took me all of ten minutes of struggling to read through my old, convoluted JavaScript code to understand how messy things could get without static analysis. The act of cleaning that code up showed me all the places where I would have benefitted from some structure. From that point on, I was hooked onto adding as much static analysis to my projects as I could.

It’s been nearly a decade since I first tinkered with TypeScript and I enjoy it as much as ever. The language is still evolving with new features and more useful than ever in providing safety and structure to JavaScript.

I hope that by reading Learning TypeScript you can learn to appreciate TypeScript the way I do: not just as a means to find bugs and typos --and certainly not a substantial change to JavaScript code patterns-- but as JavaScript with types: a beautiful system for declaring the way our JavaScript should work, and helping us stick to it.

Who Should Read This Book

If you have an understanding of writing JavaScript code, can run basic commands in a terminal, and are interested in learning about TypeScript, this book is for you.

Maybe you’ve heard TypeScript can help you write a lot of JavaScript with fewer bugs (true!) or document your code well for other people to read (also true!). Maybe you’ve seen TypeScript show up in a lot of job postings, or in a new role you’re starting.

Whatever your reason, as long as you come in knowing the fundamentals of JavaScript -variables, functions, closures/scope, and classes- this book will take you from no TypeScript knowledge to mastering the fundamentals and most important features of the language. By the end of this book you will understand:

  • The history and context for why TypeScript is useful on top of “vanilla’ JavaScript

  • How a type system analyzes and understands code

  • How to use development-only type annotations to inform the type system

  • How TypeScript works with IDEs to provide refactoring tools

And you will be able to:

  • Articulate the benefits of TypeScript and general characteristics of its type system

  • Add type annotations where useful in your code

  • Represent moderately complex types using TypeScript’s built-in inferences and new syntax

  • Use TypeScript to assist local development in refactoring code

Why I Wrote This Book

TypeScript is a wildly popular language in both industry and open source:

  • GitHub’s 2020 State of the Octoverse has it at the 4th top language, up from 7th in 2019 and 2018 and 10th in 2017.

  • StackOverflow’s 2020 Developer Survey has it at the world’s second most loved language (67.1% of users).

  • The 2020 State of JS survey has TypeScript at consistently having both high satisfaction and usage amounts as both a build tool and variant of JavaScript.

For frontend developers, TypeScript is well supported in all major UI libraries and frameworks, including Angular -which strongly recommends TypeScript-, Gatsby, Next.js, React, Svelte, and Vue. For backend developers, TypeScript generates JavaScript that runs natively in Node.js; Deno, a similar runtime by Node’s creator, emphasizes first class TypeScript support.

However, despite this plethora of popular project support, I was rather dissapointed by the lack of good introductory content online when I first learned the language. Many of the online documentation sources didn’t do a great job of explaining what a type system is or how to use it. They often assumed a great deal of prior knowledge in both JavaScript and strongly typed languages, or were written with only cursory code examples.

Not seeing an O’Reilly book with a cute animal cover introducing TypeScript was a dissapointment. I’m thrilled to be able to spend the time to really make a good, clear introduction to TypeScript for readers who aren’t already experts in its principles.

Navigating This Book

Learning TypeScript has two purposes:

  1. You can read through it once to to understand TypeScript as a whole.

  2. Later, you can refer back to it as a practical introductory TypeScript language reference.

This book ramps up from concepts to practical use across three general sections of chapters:

  • Part 1: Concepts: How JavaScript came to be, why TypeScript adds to it, and the basics of a type system as TypeScript creates it.

  • Part 2: Features: Fleshing out how the type system interacts with the major parts of JavaScript you’d work with when writing TypeScript code.

  • Part 3: Usage: Now that you understand the features that make up the TypeScript language, how to use them in real world situations to improve your code reading and editing experience.

I’ve thrown in one last chapter after that on “Cursory Miscellaneous Topics” to cover fancy TypeScript features. You won’t need to know them to consider yourself a TypeScript developer (and arguably most should be avoided whenever possible). They’re just nifty and interesting enough that I couldn’t resist giving them a mention.

Conventions Used in This Book

The following typographical conventions are used in this book:

Italic

Indicates new terms, URLs, email addresses, filenames, and file extensions.

Constant width

Used for program listings, as well as within paragraphs to refer to program elements such as variable or function names, databases, data types, environment variables, statements, and keywords.

Constant width bold

Shows commands or other text that should be typed literally by the user.

Constant width italic

Shows text that should be replaced with user-supplied values or by values determined by context.

Tip

This element signifies a tip or suggestion.

Note

This element signifies a general note.

Warning

This element indicates a warning or caution.

Using Code Examples

Supplemental material (code examples, exercises, etc.) is available for download at https://github.com/LearningTypeScript/learning-typescript.

If you have a technical question or a problem using the code examples, please send email to .

This book is here to help you get your job done. In general, if example code is offered with this book, you may use it in your programs and documentation. You do not need to contact us for permission unless you’re reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing examples from O’Reilly books does require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a significant amount of example code from this book into your product’s documentation does require permission.

We appreciate, but generally do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: “Book Title by Some Author (O’Reilly). Copyright 2012 Some Copyright Holder, 978-0-596-xxxx-x.”

If you feel your use of code examples falls outside fair use or the permission given above, feel free to contact us at .

O’Reilly Online Learning

Note

For more than 40 years, O’Reilly Media has provided technology and business training, knowledge, and insight to help companies succeed.

Our unique network of experts and innovators share their knowledge and expertise through books, articles, and our online learning platform. O’Reilly’s online learning platform gives you on-demand access to live training courses, in-depth learning paths, interactive coding environments, and a vast collection of text and video from O’Reilly and 200+ other publishers. For more information, visit http://oreilly.com.

How to Contact Us

Please address comments and questions concerning this book to the publisher:

  • O’Reilly Media, Inc.
  • 1005 Gravenstein Highway North
  • Sebastopol, CA 95472
  • 800-998-9938 (in the United States or Canada)
  • 707-829-0515 (international or local)
  • 707-829-0104 (fax)

Email to comment or ask technical questions about this book.

For news and information about our books and courses, visit http://oreilly.com.

Find us on Facebook: http://facebook.com/oreilly

Follow us on Twitter: http://twitter.com/oreillymedia

Watch us on YouTube: http://www.youtube.com/oreillymedia

Acknowledgments

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.14.141.228