Unleashing the Power of TypeScript
By Steve Kinney
()
About this ebook
If you're a JavaScript developer struggling to get to grips with TypeScript or use it effectively in your projects, this book will guide you in making the most of this powerful language and its latest features, and help you overcome some common misunderstandings and pitfalls.
TypeScript 5.0 is a major advancement to the user-friendliness and flexibility of the language. It nudges us toward better coding practices, promotes cleaner code, and catches potential errors before they become a headache. It's a great time to start using TypeScript, but if you're a JavaScript developer struggling to get to grips with the language or use it effectively in your projects, this short book is for you. It will guide you in making the most of this powerful language and its latest features, and help you overcome some common misunderstandings and pitfalls.
It covers:
- Simplifying Reducers in React with TypeScript
- A Gentle Introduction to Generics in TypeScript
- Navigating the Perils and Pitfalls of Using React’s Context API with TypeScript
- Extending DOM Elements and Creating Polymorphic Components
- A Guided Tour of the Three Biggest Features in TypeScript 5.0
This book is for intermediate-level JavaScript developers who want to get to grips with the powerful features that TypeScript offers.
Steve Kinney
Steve Kinney is the head of engineering for frontend and developer tools at Temporal and an instructor with Frontend Masters. Previously, Steve founded the front-end engineering program at the Turing School of Software and Design and was a New York City public school teacher for the better part of a decade.
Related to Unleashing the Power of TypeScript
Related ebooks
React Design Patterns and Best Practices Rating: 0 out of 5 stars0 ratingsAdvanced C++ Interview Questions You'll Most Likely Be Asked: Job Interview Questions Series Rating: 0 out of 5 stars0 ratingsPractical Advanced TypeScript Rating: 0 out of 5 stars0 ratingsSpring Boot Intermediate Microservices: Resilient Microservices with Spring Boot 2 and Spring Cloud Rating: 0 out of 5 stars0 ratingsReact Components Rating: 0 out of 5 stars0 ratingsLearn Angular: Build a Todo App Rating: 0 out of 5 stars0 ratingsJavaScript Patterns JumpStart Guide (Clean up your JavaScript Code) Rating: 4 out of 5 stars4/5Your First Week With Node.js Rating: 0 out of 5 stars0 ratingsJavaScript Interview Questions You'll Most Likely Be Asked: Job Interview Questions Series Rating: 0 out of 5 stars0 ratingsReact.js Design Patterns: Learn how to build scalable React apps with ease (English Edition) Rating: 0 out of 5 stars0 ratingsLearn Angular: 4 Angular Projects Rating: 0 out of 5 stars0 ratingsWorking with Vue.js Rating: 0 out of 5 stars0 ratingsReact: Building Modern Web Applications Rating: 5 out of 5 stars5/5Unleashing the Power of Astro Rating: 0 out of 5 stars0 ratingsPro Android with Kotlin: Developing Modern Mobile Apps Rating: 0 out of 5 stars0 ratingsPractical Enterprise React: Become an Effective React Developer in Your Team Rating: 0 out of 5 stars0 ratingsReactJS for Jobseekers: The Only Guide You Need to Learn React and Crack Interviews (English Edition) Rating: 0 out of 5 stars0 ratingsReact.js Essentials Rating: 4 out of 5 stars4/5Mastering React Native Rating: 5 out of 5 stars5/5Jump Start Vue.js Rating: 4 out of 5 stars4/5Just the basics of JavaScript Rating: 0 out of 5 stars0 ratingsSvelte: A Beginner's Guide Rating: 0 out of 5 stars0 ratingsCoding Languages: Angular With Typescript, Machine Learning With Python And React Javascript Rating: 0 out of 5 stars0 ratingsUpdate to Modern C++ Rating: 0 out of 5 stars0 ratingsQuick Start Kubernetes Rating: 0 out of 5 stars0 ratingsTypeScript Blueprints Rating: 0 out of 5 stars0 ratingsJavascript - 50 functions and tutorial Rating: 0 out of 5 stars0 ratingsLearn Vue.js: The Collection Rating: 0 out of 5 stars0 ratingsSpring Boot and Single-Page Applications: Securing Your API with a Single-Page Application Frontend - Second Edition Rating: 0 out of 5 stars0 ratings
Programming For You
Python: For Beginners A Crash Course Guide To Learn Python in 1 Week Rating: 4 out of 5 stars4/5Python Programming : How to Code Python Fast In Just 24 Hours With 7 Simple Steps Rating: 4 out of 5 stars4/5HTML & CSS: Learn the Fundaments in 7 Days Rating: 4 out of 5 stars4/5Java for Beginners: A Crash Course to Learn Java Programming in 1 Week Rating: 5 out of 5 stars5/5SQL: For Beginners: Your Guide To Easily Learn SQL Programming in 7 Days Rating: 5 out of 5 stars5/5SQL QuickStart Guide: The Simplified Beginner's Guide to Managing, Analyzing, and Manipulating Data With SQL Rating: 4 out of 5 stars4/5Learn to Code. Get a Job. The Ultimate Guide to Learning and Getting Hired as a Developer. Rating: 5 out of 5 stars5/5Coding All-in-One For Dummies Rating: 4 out of 5 stars4/5Python Machine Learning By Example Rating: 4 out of 5 stars4/5101 Amazing Nintendo NES Facts: Includes facts about the Famicom Rating: 4 out of 5 stars4/5Pokemon Go: Guide + 20 Tips and Tricks You Must Read Hints, Tricks, Tips, Secrets, Android, iOS Rating: 5 out of 5 stars5/5Linux: Learn in 24 Hours Rating: 5 out of 5 stars5/5Grokking Algorithms: An illustrated guide for programmers and other curious people Rating: 4 out of 5 stars4/5Learn SQL in 24 Hours Rating: 5 out of 5 stars5/5SQL All-in-One For Dummies Rating: 3 out of 5 stars3/5Excel : The Ultimate Comprehensive Step-By-Step Guide to the Basics of Excel Programming: 1 Rating: 5 out of 5 stars5/5PYTHON: Practical Python Programming For Beginners & Experts With Hands-on Project Rating: 5 out of 5 stars5/5Modern C++ for Absolute Beginners: A Friendly Introduction to C++ Programming Language and C++11 to C++20 Standards Rating: 0 out of 5 stars0 ratingsPython Projects for Beginners: A Ten-Week Bootcamp Approach to Python Programming Rating: 0 out of 5 stars0 ratings
Reviews for Unleashing the Power of TypeScript
0 ratings0 reviews
Book preview
Unleashing the Power of TypeScript - Steve Kinney
Preface
Who Should Read This Book?
This book is for intermediate-level JavaScript developers who want to get to grips with the powerful features that TypeScript offers.
Conventions Used
Code Samples
Code in this book is displayed using a fixed-width font, like so:
A Perfect Summer's Day
It was a lovely day for a walk in the park.
The birds were singing and the kids were all back at school.
You’ll notice that we’ve used certain layout styles throughout this book to signify different types of information. Look out for the following items.
Tips, Notes, and Warnings
Hey, You!
Tips provide helpful little pointers.
Ahem, Excuse Me ...
Notes are useful asides that are related—but not critical—to the topic at hand. Think of them as extra tidbits of information.
Make Sure You Always ...
... pay attention to these important points.
Watch Out!
Warnings highlight any gotchas that are likely to trip you up along the way.
Supplementary Materials
https://www.sitepoint.com/community/ are SitePoint’s forums, for help on any tricky problems.
books@sitepoint.com is our email address, should you need to contact us to report a problem, or for any other reason.
Chapter 1: Simplifying Reducers in React with TypeScript
Many of the example React applications we see tend to be small and easily understandable. But in the real world, React apps tend to grow to a scale that makes it impossible for us to keep all of the context in our head. This can often lead to unexpected bugs when a given value isn’t what we thought it would be. That object you thought had a certain property on it? It turns out it ended up being undefined somewhere along the way. That function you passed in? Yeah, that’s undefined too. That string you’re trying to match against? It looks like you misspelled it one night when you were working after hours.
Using TypeScript in a React application enhances code quality by providing static type checking, which catches errors early in the development process. It also improves readability and maintainability through explicit type annotations, making it easier for teams to understand the code structure. Additionally, TypeScript features like interfaces and generics make it easier to build robust, scalable applications.
Large applications also tend to come with increasingly complicated state management. Reducers are a powerful pattern for managing state in client-side applications. Reducers became popular with Redux, and are now built into React with the useReducer hook, but they’re framework and language agnostic. At the end of the day, a reducer is just a function. Redux and React’s useReducer just add some additional functionality to trigger updates accordingly. We can use Redux with any frontend framework or without one all together. We could also write our own take on Redux pretty easily, if we were so inclined.
That said, Redux (and other implementations of the Flux architecture that it’s based on) often get criticized for requiring a lot of boilerplate code, and for being a bit cumbersome to use. At the risk of making an unintentional pun, we can leverage TypeScript not only to reduce the amount of boilerplate required, but also to make the overall experience of using reducers more pleasant.
Following Along with This Tutorial
You’re welcome to follow along with this tutorial in your own environment. I’ve also created a GitHub repository that you can clone, as well as a CodeSandbox demo that you can use to follow along.
Reducer Basics
A reducer, at its most fundamental level, is simply a function that takes two arguments: the current state, and an object that represents some kind of action that has occurred. The reducer returns the new state based on that action.
A diagram showing how a reducer worksThe following code is regular JavaScript, but it could easily be converted to TypeScript by adding any types to state and action:
export const incrementAction = { type: 'Increment' };
export const decrementAction = { type: 'Decrement' };
export const counterReducer = (state, action)