The Generics workshop is your guide to mastering one of TypeScript's most complex features.
Through dozens of exercises, you'll get to grips with generics from the lowest level, up through examples of how your favorite TS libraries use them.
Just finished all the Generics sections. Really enjoyed it! It covers realistic problems from our day to day work, and when you reach the end you have strong grasp of how to use generic more efficiently, and in a prettier way that is more accurate of what you wanted to achieve in the first place.
Intro to Generics
The best place to start is the start. We'll learn the syntax for assigning generics to functions and classes. We'll learn what's possible with the syntax, and build our very first runtime generic function.
Passing Type Arguments
TypeScript can infer generics based on arguments passed to functions- but there's an alternative. You can manually pass types as type arguments to functions. We'll cover exactly why this is useful, when it's useful, and what limitations this approach has.
The Art of Generic Inference
Understanding generics in functions requires deep knowledge of the TypeScript compiler. You usually have multiple options when it comes to representing a generic in a function - and there's often not a right answer. We'll examine exercises which help illuminate these decisions.
There's more to generics than inference. In this section we'll cover generics with conditional types, generic currying, and generics on interfaces. Each come with their own challenges.
Sometimes, you don't need a generic. Function overloads provide another method for inferring types from arguments passed to a function. In this section, we'll compare and contrast overloads with generics - and even discuss how they can be combined.