The Missing
TypeScript Reference

Learning to program, in TypeScript, from nothing.

Start with Chapter 1 →

Contents

Index of terms

Part 1: Writing programs

Productive from the first chapter, using the tools TypeScript actually gives you.

  1. 1Installing a TypeScript Toolchain Without Losing Your MindBefore you can write a line of TypeScript, your computer needs Node and the TypeScript compiler. It's the same three steps on a Mac, on Windows, and on Linux, and you'll finish with a program you wrote and ran yourself.Project: A short greeting
  2. 2Your First Type ErrorChapter 1's error was a typo, the kind any compiler catches. This one is different, TypeScript noticing that a value has quietly changed what kind of thing it is, and stopping you before that mistake reaches JavaScript.Project: Fix the profile card
  3. 3Values, and the Types They Insist OnLast chapter showed TypeScript catching a value that changed what kind of thing it was. Here's the proper introduction to let and const, the three types you'll use constantly, and what happens when you don't give TypeScript enough to work with.Project: A receipt
  4. 4Strings: Text TypeScript Already UnderstandsEvery chapter so far has stuck text together with +. There is a nicer way to do it, plus how to measure a string, pull a piece out of it, and find something inside it.Project: An initials formatter
  5. 5Input: Letting the Reader Talk BackEvery program so far has said the same thing every time it runs, because you were the one filling in the values. This is where a program starts listening, and where you meet the one command every reader has to remember to finish typing.Project: Mad-libs
  6. 6Arithmetic, and the Number Type's Sharp EdgesChapter 3 said TypeScript has a single number type where other languages have several, and promised there was a cost. Here is the arithmetic, and here is the bill.Project: Seconds into hours, minutes and seconds
  7. 7Deciding Things: if, else, and TruthinessEvery program so far runs every line, top to bottom, no matter what. This is where a program starts choosing, and where the string that chapter 5 handed you turns out to be more dangerous than it looked.Project: Ticket pricing with real rules
  8. 8Loops: Doing It Again Until It's DoneA program that can repeat itself is a different kind of thing from one that runs top to bottom. This chapter also covers what to do when a loop never stops, because yours will, probably today.Project: The number guessing game
  9. 9Functions: Giving Work a NameThree chapters have now ended by pointing at this one. Every time you found yourself copying four lines and changing one word, this was the missing piece.Project: Rules with names
  10. 10Arrays: A List That GrowsChapter 5 handed you a list and asked you to take it on trust. Here is what it actually was, how to build one yourself, and the hole in the type system that this chapter exists to show you.Project: A thousand dice
  11. 11Reading, Logging, and Fixing BugsA program that crashes tells you where to look. A program that quietly returns the wrong number tells you nothing. This chapter is about the second kind, which is the kind you will spend your time on.Project: Hunt the planted bug
  12. 12Objects: Things That Belong TogetherAn array holds many of one thing. An object holds one of many things: a title, a page count and whether you finished it, all in one value with names on the parts.Project: A reading list
  13. 13Copies, References, and the Trap Objects SetThree chapters have promised you this one. A number and an object behave completely differently when you copy them, pass them, or compare them, and not knowing which you have is the source of some genuinely baffling bugs.Project: The scoreboard that edits itself
  14. 14Sorting and Searching Without Writing EitherChapter 8 had you writing running totals and longest-so-far loops by hand. Every one of those is a method on an array already, and the sorting one has two traps in it that are worth meeting deliberately.Project: A high-score table
  15. 15Classes: Types You Design YourselfChapter 12 gave you a shape with names on the parts. A class puts the data and the functions that work on it in the same place, and lets you decide what the outside world is allowed to touch.Project: A deck of cards
  16. 16Constructors, and Objects That Can't Be BrokenChapter 15's Deck was only correct if somebody remembered to call fill on it. A constructor removes the remembering, because it runs when the object is made and there is no moment at which a half-built one exists.Project: A deck that deals itself
  17. 17Files: Making Your Program RememberEvery program you have written forgets everything the moment it ends. A file fixes that, and it is also the first place where TypeScript's guarantees genuinely stop.Project: A to-do list that survives quitting
  18. 18Maps: Looking Things Up by NameAn array finds things by position. A Map finds them by whatever you like, and it exists because the obvious alternative, a plain object, has sharp edges that only show up on real data.Project: Word frequency over a real file
  19. 19Validation: Assuming the WorstSix chapters have pointed at this one. Every place where data comes from outside your program is a place your types are guesses, and this is how you turn a guess into something you have actually checked.Project: A to-do list that cannot be broken
  20. 20Splitting a Program Across FilesEvery program so far has been one file. Real ones are not, and the moment you split one you meet a compile flag that nobody warns you about, because the program will build perfectly and then refuse to run.Project: Word frequency, in pieces
  21. 21Interlude: Build Something RealTwenty chapters of tools, and nothing new in this one. You are going to build a program that reads a file, validates it, records things, reports on them, and survives being run tomorrow.Project: The expense tracker

Part 2: Lifting the floor

Back to the machinery TypeScript and JavaScript were handling for you.

  1. 22Where Do the Types Go?coming soonErasure, and what's really running.
  2. 23Prototypes, and What This Points Tocoming soonWhy arrow functions and regular functions disagree.
  3. 24Closures: Functions That Remembercoming soonScope, and why closures aren't magic.
  4. 25The Event Loop: What Async Actually Meanscoming soonPaying off chapter 5, at last.
  5. 26Promises, Underneath Async and Awaitcoming soonPromise chains, and error handling around await.
  6. 27Generics: One Function, Any Typecoming soonThe <T> from chapter 10, explained at last.
  7. 28Unions, Narrowing, and Discriminated Unionscoming soonThe type system's own control flow.
  8. 29Structural Typing and Utility Typescoming soonWhy TypeScript cares about shape, not name.
  9. 30What TypeScript Can't Save You Fromcoming soonany, and why type safety isn't a guarantee.
  10. 31Modules, Packages, and npm Underneathcoming soonpackage.json, node_modules, and CommonJS vs ES modules.
  11. 32Where to Go Nextcoming soonThe TypeScript handbook, DefinitelyTyped, and reading other people's code.