|
| 1 | +# TypeScript |
| 2 | + |
| 3 | +In this module, we'll learn about [TypeScript](https://www.typescriptlang.org/), |
| 4 | +a superset of the JavaScript programming language developed by Microsoft. You may be |
| 5 | +familiar with the basic concept of types from JavaScript or another programming |
| 6 | +language. In TypeScript, these types are more strongly enforced. For example, |
| 7 | +you cannot add together a number and a string (`1 + "foo"`) as you would in |
| 8 | +JavaScript. We'll examine the benefits of this throughout this module. |
| 9 | + |
| 10 | +For example, in the following code in TypeScript, notice that the `user.name` |
| 11 | +field does not exist (only `user.firstName`, `user.lastName`). |
| 12 | + |
| 13 | +``` |
| 14 | +const user = { |
| 15 | + firstName: "Angela", |
| 16 | + lastName: "Davis", |
| 17 | + role: "Professor", |
| 18 | +} |
| 19 | + |
| 20 | +console.log(user.name) |
| 21 | +``` |
| 22 | + |
| 23 | +Actually, TypeScript won't even allow you to run this code: you'll get an error. |
| 24 | + |
| 25 | +``` |
| 26 | +Property 'name' does not exist on type '{ firstName: string; lastName: string; |
| 27 | +role: string; }'. |
| 28 | +``` |
| 29 | + |
| 30 | +We'll learn to understand such error messages and write type-safe code using |
| 31 | +TypeScript, in addition to understanding why TypeScript has become so popular. |
| 32 | + |
| 33 | + |
| 34 | +While following along in this module, you can play around with some of the code by using the |
| 35 | +[TypeScript playground](https://www.typescriptlang.org/play). You can also |
| 36 | +install the TypeScript compiler locally by running `npm install -g typescript`. |
| 37 | + |
| 38 | +## What is static typing and compilation? |
| 39 | +JavaScript is **dynamically typed** and **interpreted** programming language. |
| 40 | +In general, both of these mean that little preprocessing is done before running |
| 41 | +JavaScript code. |
| 42 | + |
| 43 | +When we say JavaScript is dynamically typed, it means that JavaScript variables have types, such as string, boolean, |
| 44 | +number, but these are determined at the time that the code is run -- not |
| 45 | +beforehand. |
| 46 | + |
| 47 | +As an interpreted progamming language, the source code is not |
| 48 | +preprocessed, and code is interpreted, or executed, from human-readable code to |
| 49 | +things that the machine understands at the time that it runs. |
| 50 | + |
| 51 | +You may have noticed that there is an error with types in your JavaScript code |
| 52 | +-- say you tried to add an array and a number together -- this error will not be |
| 53 | +caught until the time that the code is run. This is a consequence of being both |
| 54 | +dynamically typed and interpreted; we are allowed to add together two types that |
| 55 | +don't make sense (what does it mean to add a list to a number?) and the error |
| 56 | +only appears if we run the code. |
| 57 | + |
| 58 | +TypeScript is a **compiled** and **statically typed** language. In a statically typed language, the types of a variable are known at the |
| 59 | +time that the code is written, not just when the code runs. In a compiled |
| 60 | +language, code needs to go through another program, called the compiler, |
| 61 | +before it can be run. While many languages compile, or "translate", from human readable code to |
| 62 | +machine code, TypeScript actually compiles to JavaScript, though it's not very |
| 63 | +readable. |
| 64 | + |
| 65 | +## Why TypeScript? |
| 66 | + |
| 67 | +### Reducing bugs |
| 68 | +Have you ever made any of these mistakes in JavaScript and had to spend a |
| 69 | +nontrivial amount of time to fix them? |
| 70 | + |
| 71 | +* Called a function with the wrong number of arguments: `twoArgFunction(x)` |
| 72 | +* Called a function with too many arguments: `oneArgFunction(x, y)` |
| 73 | +* Tried to get the index of a non-array type: `someString[5]` |
| 74 | +* Indexed an array with the wrong type, rather than a number: `someArray["foo"]` |
| 75 | +* Had a variable be `null` or `undefined` when you thought that it wasn't |
| 76 | +* `undefined is not a function` |
| 77 | + |
| 78 | +With TypeScript, these bugs (and many more) are easily caught at compile-time with a useful |
| 79 | +error message. In the next section, we'll take a look at some examples of such |
| 80 | +error messages and how they can easily help you track issues. |
| 81 | + |
| 82 | +Static typing is often considered advantageous for scaling codebases. When |
| 83 | +working in a large product (or even a small one), it saves everyone time and |
| 84 | +development effort if as many mistakes can be caught *before* the code is run, |
| 85 | +not afterwards. Compiled and typed languages certainly are not guaranteed to be |
| 86 | +bug-free, but in general, by the time you run the code, you will have fewer bugs |
| 87 | +than interpreted languages. In a post-mortem analysis by Airbnb from their |
| 88 | +attempt to migrate their whole codebase to TypeScript, they estimated that [38% |
| 89 | +of their bugs were preventable by |
| 90 | +TypeScript](https://youtu.be/P-J9Eg7hJwE?t=711). |
| 91 | + |
| 92 | +### Autocomplete |
| 93 | +In addition to the benefits of using types to help reduce the number of bugs, |
| 94 | +static types also allow for stronger auto-complete in editors, since, for |
| 95 | +example, the allowable function calls and variable names that can be typed in a |
| 96 | +certain place will be known even before the code will |
| 97 | +run. |
| 98 | + |
| 99 | +### Refactoring |
| 100 | +Having types makes refactoring code much easier. For example, if you are |
| 101 | +renaming a function, editors can use statically known information about types to |
| 102 | +rename all instances of that function in a single click. This is not possible |
| 103 | +with JavaScript; in the absence of type information, the editor cannot know |
| 104 | +ahead of time whether two function calls named `myFunction()` are really the |
| 105 | +exact same function, or whether they are two different functions in different |
| 106 | +files that happen to have the same name. |
| 107 | + |
| 108 | +This is only one example, but in general, TypeScript allows you to refactor code |
| 109 | +with less risk of breaking things. |
| 110 | + |
| 111 | +### Disadvantages |
| 112 | +However, compiling is an extra step in the process, and in large codebases, it |
| 113 | +can take some time to compile the code (though speeding up compilers is a focal |
| 114 | +point of compiler teams). You can read more about some of the tradeoffs between static and dynamic types |
| 115 | +[in this article](https://instil.co/blog/static-vs-dynamic-types/). |
| 116 | + |
| 117 | + |
| 118 | +## Basic Types |
| 119 | + |
| 120 | +### Primitives |
| 121 | +JavaScript has three primitive types -- `string`, `number`, `boolean` -- and they are present in TypeScript as |
| 122 | +well. |
| 123 | + |
| 124 | +Below, let's compare some JavaScript and TypeScript declarations of basic |
| 125 | +variables. |
| 126 | + |
| 127 | +``` |
| 128 | +// JavaScript |
| 129 | +const graduatingYear = 36; |
| 130 | +const isInBootcamp = true; |
| 131 | +const name = "Foo Bar"; |
| 132 | +``` |
| 133 | + |
| 134 | +Now in TypeScript, when we declare a variable, ;q |
| 135 | + |
| 136 | + |
| 137 | +### Arrays |
| 138 | + |
| 139 | +### Functinos |
| 140 | + |
| 141 | + |
| 142 | +### Avoiding `any` |
0 commit comments