learning_typescript_by_josh_goldberg_preface

Learning TypeScript by Josh Goldberg Preface

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 that 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 benefited 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 “vanillaJavaScript

How a type system models code

How a type checker analyzes code

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

How TypeScript works with IDEs to provide code exploration and 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 2021 and 2020 State of the Octoverses have it at the platform’s 4th top language, up from 7th in 2019 and 2018 and 10th in 2017.

StackOverflow’s 2021 Developer Survey has it at the world’s third most loved language (72.73% 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 directly supporting TypeScript files.

However, despite this plethora of popular project support, I was rather disappointed 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 years ago was a disappointment. While other books on TypeScript from publishers including O’Reilly books now exist prior to this one, I couldn’t find a book that focuses on the foundations of the language quite the way I wanted: why it works the way it does and how its core features work together. I’m thrilled to be able to spend the time to really make a clear, comprehensive introduction to TypeScript language fundamentals for readers who aren’t already familiar with its principles.

Learning TypeScript has two purposes:

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

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 I, “Concepts”: How JavaScript came to be, what TypeScript adds to it, and the foundations of a type system as TypeScript creates it.

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

Part III, “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.

For fun, each chapter starts with a haiku to get into the spirit of its contents and ends with a pun.

I’ve thrown in one last “Extra Creditsection after those to cover lesser-used but still occasionally useful TypeScript features. You won’t need to deeply know them to consider yourself a TypeScript developer. But, they’re all useful concepts that will likely come up as you use TypeScript for real world projects. Once you’ve finished understanding the prior three sections I highly recommend studying up on the extra credit section.

[[Example]]s and [[Project]]s

Unlike many other resources that introduce TypeScript, this book intentionally focuses on introducing language features with standalone examples showing just the new information rather than delving into medium or large sized projects projects. I prefer this method of teaching because it puts a spotlight on the TypeScript language first and foremost. TypeScript is useful across so many frameworks and platforms – many of which undergo API updates regularly – that I didn’t want to keep anything framework- or platform-specific in this book.

That being said, it is supremely useful when learning a programming language to exercise concepts immediately after they’re introduced. I highly recommend taking a break after each chapter to rehearse that chapter’s contents. Each chapter ends with a suggestion to visit its section in https://learningtypescript.com and work through the examples and projects listed there.

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://learningtypescript.com.

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

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: “Learning Typescript by Josh Goldberg (O’Reilly). Copyright 2022 Josh Goldberg, 978-1-098-11033-8.”

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

Acknowledgments

This book was a team effort and I’d like to sincerely thank everybody who made it possible. First and foremost my superhuman editor-in-chief Rita Fernando, for an incredible amount of patience and excellent guidance throughout the authoring journey. Thanks as well to the tech reviewers for their consistently top notch pedagogical insights and TypeScript expertise: Adam Reineke, Ryan Cavanaugh, Sara Gallagher, Michael Hoffman, Mike Boyle, and Dan Vanderkam.

Fair Use Sources

TypeScript: TypeScript Glossary, TypeScript Best Practices, Web Development Best Practices, JavaScript Best Practices, TypeScript Fundamentals, TypeScript Inventor - TypeScript Language Designer: Anders Hejlsberg of Microsoft on October 1, 2012; TypeScript Keywords, TypeScript Built-In Data Types, TypeScript Data Structures - TypeScript Algorithms, TypeScript Syntax, TypeScript on Linux, TypeScript on macOS, TypeScript on Windows, TypeScript on Android, TypeScript on iOS, TypeScript Installation, TypeScript Containerization (TypeScript with Docker, TypeScript with Podman, TypeScript and Kubernetes), TypeScript OOP - TypeScript Design Patterns, Clean TypeScript - TypeScript Style Guide, TypeScript Best Practices - TypeScript BDD, Web Browser, Web Development, HTML-CSS, TypeScript Frameworks (Angular), JavaScript Libraries (React.js with TypeScript, Vue.js with TypeScript, jQuery with TypeScript), TypeScript on the Server (TypeScript with Node.js, TypeScript with Deno, TypeScript with Express.js), TypeScript Compiler (tsc, tsconfig.json), TypeScript Transpiler (Transpile TypeScript into JavaScript), Babel and TypeScript, TypeScript Package Management, NPM and TypeScript, NVM and TypeScript, Yarn Package Manager and TypeScript, TypeScript IDEs (Visual Studio Code, Visual Studio, JetBrains WebStorm), TypeScript Development Tools, TypeScript Linter, TypeScript Debugging (Chrome DevTools, JavaScript Source Maps), TypeScript Testing (TypeScript TDD, Selenium, Jest, Mocha.js, Jasmine, Tape Testing (tap-producing test harness for Node.js and browsers), Supertest, React Testing Library, Enzyme.js React Testing, Angular TestBed), TypeScript DevOps - TypeScript SRE, TypeScript Data Science - TypeScript DataOps, TypeScript Machine Learning, TypeScript Deep Learning, Functional TypeScript, TypeScript Concurrency (WebAssembly - WASM) - TypeScript Async (TypeScript Await, TypeScript Promises, TypeScript Workers - Web Workers, Service Workers, Browser Main Thread), TypeScript Concurrency, TypeScript History, TypeScript Bibliography, Manning TypeScript Series, TypeScript Glossary, TypeScript T, TypeScript Courses, TypeScript Standard Library, TypeScript Libraries, TypeScript Frameworks (Angular), TypeScript Research, JavaScript, TypeScript GitHub, Written in TypeScript, TypeScript Popularity, TypeScript Awesome, TypeScript Versions. (navbar_typescript - see also navbar_javascript, navbar_javascript_libraries, navbar_typescript_libraries, navbar_typescript_versions, navbar_typescript_standard_library, navbar_typescript_libraries, navbar_typescript_reserved_words, navbar_typescript_functional, navbar_typescript_concurrency, navbar_typescript_async, navbar_javascript_standard_library, navbar_react.js, navbar_angular, navbar_vue, navbar_javascript_standard_library, navbar_web_development)


© 1994 - 2024 Cloud Monk Losang Jinpa or Fair Use. Disclaimers

SYI LU SENG E MU CHYWE YE. NAN. WEI LA YE. WEI LA YE. SA WA HE.


learning_typescript_by_josh_goldberg_preface.txt · Last modified: 2024/04/28 03:37 (external edit)