Explore 1.5M+ audiobooks & ebooks free for days

From $11.99/month after trial. Cancel anytime.

Typescript Mini Reference: A Hitchhiker's Guide to the Modern Programming Languages, #4
Typescript Mini Reference: A Hitchhiker's Guide to the Modern Programming Languages, #4
Typescript Mini Reference: A Hitchhiker's Guide to the Modern Programming Languages, #4

Typescript Mini Reference: A Hitchhiker's Guide to the Modern Programming Languages, #4

Rating: 0 out of 5 stars

()

Read preview

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!

 

LanguageEnglish
PublisherCoding Books Press
Release dateApr 25, 2023
ISBN9798215361290
Typescript Mini Reference: A Hitchhiker's Guide to the Modern Programming Languages, #4

Other titles in Typescript Mini Reference Series (7)

View More

Read more from Harry Yoon

Related to Typescript Mini Reference

Titles in the series (7)

View More

Related ebooks

Programming For You

View More

Reviews for Typescript Mini Reference

Rating: 0 out of 5 stars
0 ratings

0 ratings0 reviews

What did you think?

Tap to rate

Review must be at least 10 words

    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.

    Enjoying the preview?
    Page 1 of 1