From 3c3ab6e6aa58d5a575a693f4133135bb7a986556 Mon Sep 17 00:00:00 2001 From: Matt Pocock Date: Fri, 21 Jun 2024 10:43:29 +0100 Subject: [PATCH] Added summaries to each chapter --- book-content/chapters/02-ide-superpowers.md | 14 ++++++++++++++ ...03-typescript-in-the-development-pipeline.md | 10 ++++++++++ .../04-essential-types-and-annotations.md | 17 +++++++++++++++++ .../05-unions-literals-and-narrowing.md | 12 ++++++++++++ 4 files changed, 53 insertions(+) diff --git a/book-content/chapters/02-ide-superpowers.md b/book-content/chapters/02-ide-superpowers.md index 43c63d7..9add37e 100644 --- a/book-content/chapters/02-ide-superpowers.md +++ b/book-content/chapters/02-ide-superpowers.md @@ -577,3 +577,17 @@ function getRandomPercentage() { ``` These are just some of the options provided by the Quick Fix menu. There's so much you can achieve with them, and we're only scratching the surface. Keep exploring and experimenting to discover their full potential! + +## Summary + +TypeScript's benefits are most strongly felt as you write your code. It gives your IDE superpowers. In this chapter, we learned about: + +- Autocomplete, which can be manually triggered with `Ctrl + Space`. +- Error checking, which catches runtime errors and makes sure the code you write is doing what you think it's doing. +- Handling multi-line errors by reading them from the bottom up. +- Introspecting variables and declarations, by hovering over them to see their types. +- Navigating your codebase with "Go to Definition" and "Go to References". +- Renaming symbols across your codebase. +- Automatic imports, which add import statements to the top of your file as you type. +- Quick Fixes, which provide a range of options for refactoring your code. +- JSDoc comments, which let you add documentation to your code that's revealed on hover. diff --git a/book-content/chapters/03-typescript-in-the-development-pipeline.md b/book-content/chapters/03-typescript-in-the-development-pipeline.md index 90de3be..aa0678f 100644 --- a/book-content/chapters/03-typescript-in-the-development-pipeline.md +++ b/book-content/chapters/03-typescript-in-the-development-pipeline.md @@ -242,3 +242,13 @@ Inside the `tsconfig.json` file, there's an option called `noEmit` that tells `t By setting `noEmit` to `true`, no JavaScript files will be created when you run `tsc`. This makes TypeScript act more like a linter than a transpiler. This makes a `tsc` step a great addition to a CI/CD system, since it can prevent merging a pull request with TypeScript errors. Later in the book, we'll look closer at more advanced TypeScript configurations for application development. + +## Summary + +You should now have a feel for how to integrate TypeScript into your development pipeline. We've covered: + +- TypeScript can't run in the browser - it needs to be transpiled to JavaScript first. +- The TypeScript CLI `tsc` can transpile TypeScript files to JavaScript, and check for errors. +- The `tsc --watch` command will automatically recompile TypeScript files on save. +- TypeScript can be used as a linter by setting `noEmit` to `true` in the `tsconfig.json` file. +- Using TypeScript as a linter is most common when working with frontend frameworks like Vite. diff --git a/book-content/chapters/04-essential-types-and-annotations.md b/book-content/chapters/04-essential-types-and-annotations.md index 6abd0ff..1194e49 100644 --- a/book-content/chapters/04-essential-types-and-annotations.md +++ b/book-content/chapters/04-essential-types-and-annotations.md @@ -2405,3 +2405,20 @@ async function fetchData(): Promise { ``` By wrapping the `number` inside of `Promise<>`, we make sure that the `data` is awaited before the type is figured out. + +## Summary + +You should now have a solid grounding of TypeScript's fundamentals. You've learned about: + +- Annotating function parameters, including using optional parameters and defaults, to make sure the correct types are passed in. +- Annotating variables to ensure they contain the correct types. +- The basic types, like `string`, `number` and `boolean`. +- You don't need to provide a type annotation for everything - TypeScript can infer types automatically from your code. +- How `any` disables type checking, and how to avoid it. +- How to declare object types, including with optional properties. +- The two ways to handle array types: `string[]` and `Array`. +- How to declare tuples, an array with a fixed number of elements. +- How to reuse types using type aliases. +- How to pass types to functions, like `Set` and `Map`. +- Typing async functions using `Promise`, and enforcing the correct return type of a function. +- How to type callbacks, including `void` - and the difference between `void` and `undefined`. diff --git a/book-content/chapters/05-unions-literals-and-narrowing.md b/book-content/chapters/05-unions-literals-and-narrowing.md index abb8bb8..e5aff8c 100644 --- a/book-content/chapters/05-unions-literals-and-narrowing.md +++ b/book-content/chapters/05-unions-literals-and-narrowing.md @@ -1873,3 +1873,15 @@ if (shape.kind === "square") { By inspecting `square` first, all shape cases that aren't squares default to circles. The circle is treated as optional, which preserves our discriminated union and keeps the function flexible. Sometimes, just flipping the runtime logic makes TypeScript happy! + +## Summary + +You should now have a solid understanding of how TypeScript uses unions to express different possibilities for a value. Let's summarize: + +- A union type is a type formed by combining two or more types using the `|` operator. +- Literal types can be used to express specific strings, numbers or booleans, like `"loading"`, `42`, or `true`. +- Types can be wider or narrower versions of other types. For instance, `string` is a wider version of `"hello"`, and `number` is a wider version of `42`. +- TypeScript can narrow down unions to specific values in your code. You can use `typeof`, `in`, and `instanceof` checks to help TypeScript narrow down the type of a value. +- `unknown` is a type that represents any value. You can use type assertions or type guards to narrow down the type of an `unknown` value. +- The `never` type represents something that can never happen. You can use it to represent functions that always error, so never return. +- Discriminated unions are an extremely useful pattern in TypeScript. They are a set of objects, each with a literal type property that acts as a discriminant. They help solve the 'bag of optionals' problem.