Generics keep a type constant throughout your code but don’t specify the exact type. They’re a useful way to make your functions, interfaces, and types more reusable without losing TypeScript’s benefits.
The “hello world” of Generics is the Identity function - it just returns what it gets as an argument.
To make Generics more practical, we must do one of two things: Set constraints on what the Generic Type can be Specify the structure of your data, without constraining the Type
We show toy examples of both, and then show an example from the Vue 3 source code.
After this video, you should be able to decipher most TypeScript code.
Vue 3 Example