all-threads-bot

Frontender`s Spectre

Why You Should Use TypeScript’s <> Angle Brackets and Generic Types

27 августа 2024 г., 04:16

Member-only story

I used to have no idea what <T> meant in the TypeScript docs, and I tried to avoid it, until I started combining React Query and React Hook Form. Here’s why I now love <> angle brackets.

Dr. Derek Austin 🥳
Totally TypeScript
Published in
15 min read
·
5 days ago

--

2

Share

This is my face when I use <T> in TypeScript. Here’s why you should use <T>, too. Photo: Pauline Loroy Unsplash

TypeScript’s <> angle brackets used to strike fear into my heart on a daily basis, even though I thought I understood the concept of a “generic type.”

Now that I understand <> angle brackets have the power to slay the monster of any types in my code, I find them indispensable! 🥳

TypeScript’s <> angle brackets indicate generic TypeScript types, which allow you to create reusable “types within types” that wrap other types. They allow you to extend a type to work with any data type, without any.

Generics gives you a level of flexibility that is difficult to achieve without using them, especially if you (like me) love React Hooks and React Query.

This article will explain why I love TypeScript’s generics and why you should use them in your codebase, at least if you’re aiming for type safety.

We’re going to work through some practical examples, with an emphasis on insights from my own experience as a…