TYPESCRIPTChapter 3 · TS Advanced
Reusable code with generics
Generics let a function work over many types while keeping the link between input and output. <T> is a type variable filled in when the function is called.
Worked example
How it reads
- <T> is a type placeholder bound when the function is called
- first(items: T[]): T returns the same type the array holds

Cloud tip: You rarely need to pass <string> explicitly; TypeScript infers T from the argument you give.


