Typescript Mini Reference: A Hitchhiker's Guide to the Modern Programming Languages, #4
By Harry Yoon
()
About this ebook
Become a Better Javascript Programmer!
Typescript is a better Javascript, It is a statically typed, safe programming language, which uses more or less the same syntax as Javascript. Typescript programs are compiled to Javascript, and they can be run anywhere Javascript is supported. It is widely used by professional Javascript developers, for frontend development (e.g., Angular, React, and VueJS) as well as for backend development (e.g., Node.Js and Deno).
This book is a "mini" language reference on the Typescript programming language. We go through all essential features of Typescript (as of versions 4.8 and 4.9), and some new ECMAScript features, in this book.
Although it is written as a reference, however, you can read it more or less from beginning to end and you should be able to get the overall picture of the Typescript language if you have some prior experience with programming in Typescript or modern Javascript (ES2015 and later). This book is, however, not intended for complete beginners.
The book covers
- Typescript development basics.
- ES module system.
- Basic JS/TS types. Type annotations.
- Variable declarations. Type inference.
- Algebraic data types. Generics.
- Objects. Classes.
- Function types. Arrow functions.
- How to create new types from existing types.
Order your copy today!
Other titles in Typescript Mini Reference Series (7)
Golang Mini Reference: A Hitchhiker's Guide to the Modern Programming Languages, #1 Rating: 0 out of 5 stars0 ratingsC# Mini Reference: A Hitchhiker's Guide to the Modern Programming Languages, #2 Rating: 0 out of 5 stars0 ratingsPython Mini Reference: A Hitchhiker's Guide to the Modern Programming Languages, #3 Rating: 0 out of 5 stars0 ratingsTypescript Mini Reference: A Hitchhiker's Guide to the Modern Programming Languages, #4 Rating: 0 out of 5 stars0 ratingsRust Mini Reference: A Hitchhiker's Guide to the Modern Programming Languages, #5 Rating: 0 out of 5 stars0 ratingsHaskell Mini Reference: A Hitchhiker's Guide to the Modern Programming Languages, #10 Rating: 0 out of 5 stars0 ratingsLua Mini Reference: A Hitchhiker's Guide to the Modern Programming Languages, #12 Rating: 0 out of 5 stars0 ratings
Read more from Harry Yoon
The Art of Go - Basics: Introduction to Programming in Go: Read and Learn Programming Rating: 0 out of 5 stars0 ratingsEinstein's Relativity: The Special Theory and the General Theory - Free Chapter 2 Rating: 0 out of 5 stars0 ratingsEinstein's Seven Essays on Relativity, Geometry, and Scientific Methods Rating: 0 out of 5 stars0 ratingsEinstein's Relativity: The Special Theory and the General Theory - Free Chapter 3 Rating: 0 out of 5 stars0 ratingsEinstein's Relativity: The Special Theory and the General Theory - Free Chapter 1 Rating: 0 out of 5 stars0 ratingsEinstein's Relativity Part I - The Special Theory of Relativity Rating: 0 out of 5 stars0 ratings
Related to Typescript Mini Reference
Titles in the series (7)
Golang Mini Reference: A Hitchhiker's Guide to the Modern Programming Languages, #1 Rating: 0 out of 5 stars0 ratingsC# Mini Reference: A Hitchhiker's Guide to the Modern Programming Languages, #2 Rating: 0 out of 5 stars0 ratingsPython Mini Reference: A Hitchhiker's Guide to the Modern Programming Languages, #3 Rating: 0 out of 5 stars0 ratingsTypescript Mini Reference: A Hitchhiker's Guide to the Modern Programming Languages, #4 Rating: 0 out of 5 stars0 ratingsRust Mini Reference: A Hitchhiker's Guide to the Modern Programming Languages, #5 Rating: 0 out of 5 stars0 ratingsHaskell Mini Reference: A Hitchhiker's Guide to the Modern Programming Languages, #10 Rating: 0 out of 5 stars0 ratingsLua Mini Reference: A Hitchhiker's Guide to the Modern Programming Languages, #12 Rating: 0 out of 5 stars0 ratings
Related ebooks
Your First Week With Node.js Rating: 0 out of 5 stars0 ratingsThe Joy of JavaScript Rating: 4 out of 5 stars4/5JavaScript: Best Practice Rating: 0 out of 5 stars0 ratingsHow JavaScript Works Rating: 0 out of 5 stars0 ratingsTypeScript Essentials Rating: 4 out of 5 stars4/5Golang Mini Reference: A Hitchhiker's Guide to the Modern Programming Languages, #1 Rating: 0 out of 5 stars0 ratingsLearn ClojureScript: Functional programming for the web Rating: 0 out of 5 stars0 ratingsPractical Advanced TypeScript Rating: 0 out of 5 stars0 ratings50 C# Concepts Every Developer Should Know Rating: 0 out of 5 stars0 ratingsUnleashing the Power of TypeScript Rating: 0 out of 5 stars0 ratingsLearning Go Programming: Build ScalableNext-Gen Web Application using Golang (English Edition) Rating: 0 out of 5 stars0 ratingsVue.js: 11 Practical Projects Rating: 0 out of 5 stars0 ratingsVue.js 2 Cookbook Rating: 0 out of 5 stars0 ratingsPractical Rust 1.x Cookbook Rating: 0 out of 5 stars0 ratingsAtomic Kotlin Rating: 0 out of 5 stars0 ratingsDeploying Node.js Rating: 5 out of 5 stars5/5Building iOS 17 Apps with Xcode Storyboards: Develop iOS 17 Apps with Xcode 15 and Swift Rating: 0 out of 5 stars0 ratingsModern Web Development with Go: Build real-world, fast, efficient and scalable web server apps using Go programming language Rating: 0 out of 5 stars0 ratingsReact Design Patterns and Best Practices Rating: 0 out of 5 stars0 ratingsMastering Go: Navigating the World of Concurrent Programming Rating: 0 out of 5 stars0 ratingsNode.js: Novice to Ninja Rating: 0 out of 5 stars0 ratingsRust Mini Reference: A Hitchhiker's Guide to the Modern Programming Languages, #5 Rating: 0 out of 5 stars0 ratingsReact.js Design Patterns: Learn how to build scalable React apps with ease (English Edition) Rating: 0 out of 5 stars0 ratingsVue.js: Tools & Skills Rating: 0 out of 5 stars0 ratingsJump Start Vue.js Rating: 4 out of 5 stars4/5
Programming For You
Coding All-in-One For Dummies Rating: 4 out of 5 stars4/5Excel 101: A Beginner's & Intermediate's Guide for Mastering the Quintessence of Microsoft Excel (2010-2019 & 365) in no time! Rating: 0 out of 5 stars0 ratingsPython: For Beginners A Crash Course Guide To Learn Python in 1 Week Rating: 4 out of 5 stars4/5Python: Learn Python in 24 Hours Rating: 4 out of 5 stars4/5JavaScript All-in-One For Dummies Rating: 5 out of 5 stars5/5Excel : The Ultimate Comprehensive Step-By-Step Guide to the Basics of Excel Programming: 1 Rating: 5 out of 5 stars5/5Python Programming : How to Code Python Fast In Just 24 Hours With 7 Simple Steps Rating: 4 out of 5 stars4/5SQL QuickStart Guide: The Simplified Beginner's Guide to Managing, Analyzing, and Manipulating Data With SQL Rating: 4 out of 5 stars4/5Linux: Learn in 24 Hours Rating: 5 out of 5 stars5/5Learn NodeJS in 1 Day: Complete Node JS Guide with Examples Rating: 3 out of 5 stars3/5Java: Programming: Your Step by Step Guide to Easily Learn Java in 7 Days Rating: 4 out of 5 stars4/5iMac For Dummies Rating: 0 out of 5 stars0 ratingsBeginning Programming with C++ For Dummies Rating: 4 out of 5 stars4/5SQL All-in-One For Dummies Rating: 3 out of 5 stars3/5Microsoft Azure For Dummies Rating: 0 out of 5 stars0 ratingsPYTHON PROGRAMMING Rating: 4 out of 5 stars4/5Teach Yourself C++ Rating: 4 out of 5 stars4/5Beginning Programming with Python For Dummies Rating: 3 out of 5 stars3/5Learn PowerShell in a Month of Lunches, Fourth Edition: Covers Windows, Linux, and macOS Rating: 5 out of 5 stars5/5Learn to Code. Get a Job. The Ultimate Guide to Learning and Getting Hired as a Developer. Rating: 5 out of 5 stars5/5Learn SQL in 24 Hours Rating: 5 out of 5 stars5/5
Reviews for Typescript Mini Reference
0 ratings0 reviews
Book preview
Typescript Mini Reference - Harry Yoon
Preface
Typescript is a high-level programming language that is different and distinct from Javascript. But, Typescript mostly uses the same Javascript (or, more precisely ECMAScript) syntax. This is a curse and blessing.
On the one hand, many Javascript developers find Typescript more approachable/accessible and easier to get started with. This is probably one of the reasons why Typescript has been so successful, e.g., in terms of its wide adoption.
On the other hand, few Javascript developers use Typescript to its full potential. For many, Typescript is just Javascript with simple type annotations. Once you you learn the fundamentals of Typescript, however, that cannot be further from the truth. Typescript is, deep down, a rather different language from Javascript, in many respects.
Typescript’s marketing slogan, in the early days, used to be TypeScript is a superset of JavaScript. This phrase, when interpreted literally, does not mean very much. For one thing, a programming language is not a mathematical set, and hence one programming language cannot be a superset of another. Regardless, Typescript uses the same or similar Javascript syntax in many parts of its grammar, including (almost) all statements and expressions.
Typescript’s extension over Javascript is primarily limited to types. Javascript is a dynamically and loosely typed language. It has pros and cons. For small projects, or for quick prototyping, dynamic languages like Javascript or Python can be extremely convenient. On the other hand, when you work on bigger and longer-term projects, using statically typed languages tends to be increasingly more advantageous.
Typescript’s new slogan is TypeScript is JavaScript with syntax for types. And, it emphasizes the tooling aspect of the programming language, at any scale. Typescript is widely used with many Javascript application frameworks such as Angular, React, and Vuejs, which are primarily intended for building large-scale Javascript apps. In fact, Typescript got a big break, as a new language, when the Angular team adopted Typescript as their default programming language for Angular version 2.0 (and, onward). As the saying goes, the rest is history. As of this writing (January 2023), React Native, another Javascript-based hybrid mobile app development framework, also adopted Typescript as their primary language.
This book is an unofficial Typescript language reference. Regardless of your background, and your experience with Javascript and other programming languages, you will learn the essence of Typescript, and the core programming concepts in Typescript.
This book mostly focuses on the language features that are related to static typing. If you have some familiarity with programming in Javascript, you can read the book more or less from beginning to end, and you will get the full picture of Typescript’s (rather unique) type system, among other things. On the flip side, written as an informal reference, this book may not be ideal for complete beginners. This book is definitely not a tutorial on Typescript.
One thing to note is that although the official names of these two programming languages are JavaScript and TypeScript, we mainly use simpler
names such as Javascript and Typescript in this book, and even refer to them just as JS or TS, for brevity.
As stated, this book is not an authoritative language reference. Although we have taken every effort to ensure the accuracy of the content, there still may be some errors or misrepresentations. The readers are encouraged to consult the official documentation while reading this book.
Dear Readers:
Please read this before you purchase, or start investing your time on, this book.
This book is a language reference, written in an informal style. It is one of the books in a series which cover different programming languages that are widely used. These books are not meant to teach you how to program in those languages in a tutorial style, however.
Going through this kind of language reference-style books once in a while, at various stages of your learning, can boost your programming skills significantly. But, you will only learn so much based on what you already know. We learn incrementally. Do not try to boil the ocean at your first reading unless you are a truly experienced developer.
These mini references are intended to be read quickly, or even just browsed, in a few hours or a few days. If you feel like you are struggling with every paragraph, on the other hand, then this book is clearly not a right one for you. We suggest you look for an alternative book that is more suitable to you. If you have already purchased this book, you can return it if it’s still within the return time window.
Otherwise, here’s a quick suggestion. First of all, you do not have to understand or remember everything. Just quickly go through the book from beginning to end, and see which parts you are more familiar with and which parts you are not. Then, come back to this book later, say, after practicing programming for a little while, and read it again. Do you find the book a bit easier to read?
You can repeat these steps once in a while. We all tend to forget things, and a quick refresher is always a good idea. You will learn, or re-learn, something new
every time.
Good luck!
PS: A slightly more elaborate advice is given at the end of the book.
1. Introduction
A type determines what kind of values are valid for a given object and what sort of operations are available for the object, among other things. Types are the core part of all high-level programming languages. The main difference between the dynamically typed languages like Javascript and the statically typed languages like Typescript is when the types are checked, e.g., run time vs build time.
Programs written in statically typed languages are generally easier to read, whereas it tends to be easier to write programs in dynamically typed languages. Although this is a gross oversimplification, it generally holds true. Hence, statically typed languages are often preferred in a large project involving multiple developers. On the flip side, there is clearly an overhead in using statically typed languages.
That’s precisely the difference between Typescript and Javascript. When you start using Typescript, there is an upfront cost, in term of learning curve and general setup, etc., but as the project grows bigger, the benefits quickly start to outweigh the additional cost.
We start the book with the absolute basics of Typescript, and the general development process using Typescript. We go through some basic usages of the Typescript compiler, tsc, and the configuration file, tsconfig.json. If you have used Typescript before, you can skip most of this first chapter.
Next, we discuss the high-level organization of Javascript and Typescript programs using ES modules. Except for truly trivial code, most Typescript programs will need to be organized into modules.
There are primarily two contexts in which typing is important. The types of variables and functions. Function parameters and return values are also variables, broadly speaking, and hence we primarily deal with the types of variables when programming in statically typed languages like Typescript. In contrast, in languages like Javascript, variables do not have associated types. There are three different ways to declare variables in Javascript (and hence in Typescript). We discuss each of them in the Variable Declaration chapter.
As in any statically typed programming languages, the types of Typescript can be divided into simple types and compound types, which are built from other types. In the Basic Types chapter, we go through various simple types of Typescript, including Javascript primitive types like number, string, and boolean as well as null and undefined, and other special types like any and unknown. In addition, Typescript allows using simple values as types, called the literal types. We also briefly look at some of the most fundamental (non-simple) types in Typescript, such as arrays, tuples, enums, and functions.
In Typescript, we can define new custom types in various different ways. Some of them are syntactically defined without names (e.g., union types), and we can use type aliases to give them reusable names, if need be. As a matter of fact, it is rather convenient, although not required, to use type aliases for any types that are to be used more than once in a program.
In many statically typed programming languages, variables are declared with types. In Typescript, we annotate variables. Although it is not a precise distinction, type annotations are only relevant at build time, and they have no runtime implications. In the Type Annotations chapter, we describe how to annotate variables and functions. Type annotations and type inferences are discussed throughout the book, not just in this chapter.
Typescript’s static type checker relies on the type annotations (and, type inference rules) to enforce type safety. In certain situations, the developer may have certain information that is not readily available to the type checker. In such a case, he or she can use various forms of assertions to convey that information to the Typescript compiler.
Dynamically and weakly typed languages like Javascript do not, and need not, have generics, which is sometimes called the parametrized types. Generics permits defining a series of related types using type parameters, each of which is still strongly typed. If you are coming from a purely Javascript background, generics may seem a bit strange.