Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added summaries to each chapter #32

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions book-content/chapters/02-ide-superpowers.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
10 changes: 10 additions & 0 deletions book-content/chapters/03-typescript-in-the-development-pipeline.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
17 changes: 17 additions & 0 deletions book-content/chapters/04-essential-types-and-annotations.md
Original file line number Diff line number Diff line change
Expand Up @@ -2405,3 +2405,20 @@ async function fetchData(): Promise<number> {
```

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<string>`.
- 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`.
12 changes: 12 additions & 0 deletions book-content/chapters/05-unions-literals-and-narrowing.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.