You write a function that works on numbers. Then you need the same function for strings. You copy-paste it and change the type annotation. Then arrays. Then objects. Now you have four functions with identical bodies and different type signatures — or you gave up and typed everything any, and the compiler stopped helping you at the one place you needed it most: the boundary between what you pass in and what you get back.
What you'll learn
By the end of this guide you'll be able to:
Write generic functions that preserve the exact type of their input in their output, instead of widening it to any or unknown
Constrain a type parameter with extends so the compiler enforces a shape without collapsing to one concrete type






