Skip to content
Kendrick's Website Kendrick's GitHub Kendrick's Youtube Kendrick's Travel blog

[Book Review] Practical TypeScript - TypeScript Cookbook

4 min read
Cover

Practical TypeScript - TypeScript Cookbook

The book I'm reviewing this time is "Practical TypeScript".

Author Biography

An Austrian developer and architect. Author of 'TypeScript in 50 Lessons' and runs a popular TypeScript and technology blog. In his spare time, he organizes Rust Linz meetups and European TypeScript conferences. He enjoys Italian food, Belgian beer, and British vinyl records. He also works as an independent consultant and Rust and TypeScript instructor at oida.dev.

This book can be seen as a sequel to the author's previous book "TypeScript in 50 Lessons".

Interestingly, the subtitle is "105 Techniques for Error Liberation", and you can immediately understand this meaning when you look at the book.

You can see traces of the author's attempt to naturally provide insights to readers by creating 105 problems for the TypeScript features explained in each chapter and providing solutions/discussions for them.

It's like this.

Book Introduction

It starts with basic TypeScript project setup. It covers tsconfig needed to use TypeScript, presents 11 problems for various use cases and project setup environments, and discusses how to solve them using tsconfig and Definitely Typed.

From "Chapter 2: Basic Types" onwards, it enters the real world of TypeScript. It then covers TypeScript in React.

I've briefly organized what I learned from the book:

  • Type Annotations should only be specified when necessary. In OOP, all types are generally specified, so I thought TypeScript would also recommend this, but surprisingly, because of type inference, it's recommended to use annotations only when you must use Type annotation, and use type inference otherwise.
  • For objects, there are object, Object, and {}, but it's recommended to use object for compound types and {} for others. {} is similar to Object.
  • Distinction between types and interfaces Use type aliases within project boundaries, and use interfaces for types that others need to consume. This is an interesting difference from OOP. This was a highly debated topic among users in the past, but now that the differences between types and interfaces have been aligned, there's almost no difference. However, interfaces automatically extend when duplicate interface names exist, while types identify duplicates and generate errors, so type usage is recommended to avoid problems caused by external modules and plugins. More details can be found here.
  • Complete checking method using assertNever function When adding new components to union types, you can use the assertNever function to check for errors in advance in code's switch cases, etc.

It also supports helper types like SelectPartial and Exclude that can parse to desired formats, and this book covers them comprehensively, which provided much inspiration.

Later chapters cover type development strategies, discussing how to use TS in practice for easy maintenance by comprehensively covering TypeScript features and functionality introduced so far. This book introduces this as low maintenance types, providing a compilation of tips that are greatly helpful in practice.

An impressive part is the last chapter's "Knowing When to Stop".

When you learn something new, you want to use it, and often end up making things more complex than necessary. The author seems to be aware of this, so at the end, he recommends stopping at appropriate times and solutions that focus more on increasing productivity.

The author hopes that readers will use types appropriate for necessary situations with insights gained from solving the 105 problems in this book, and concludes the book by saying that loose types can be a problem and that you can easily stop anytime after going through refinement processes.

This book has one drawback: the translation is somewhat disappointing. For example, seeing Chapter 10 titled "TypeScript and Reaction", I wondered if it was Interaction? But it was the React library. It does say React directly in the content. Translating Promise as fulfillment and rejection also means fulfill/reject. Certain terms have been too localized to Korean, which I think might confuse practical developers who have become accustomed to English, unlike before. Of course, not everything is like this - there are also terms like export and assertion check that are translated directly from English.

Conclusion

  • Since this book was published in 2023, it doesn't cover the latest TypeScript extension features added recently.
  • I strongly recommend this book to those with OOP experience but encountering TypeScript for the first time. You can troubleshoot problems you'll encounter in advance.

"This review was written after receiving the book for Hanbit Media's <I am a Reviewer> activity."