Grokking TypeScript
0% completed
Generics and Generic Functions
A function that works with many types, and that still remembers which type it received on each call, is called a generic function. The same idea also applies to classes, interfaces and type aliases, and together these features are called generics. This chapter starts with functions, because the work the compiler does is easiest to see there.
One Body, Many Types
Suppose you need the first element of an array. With the tools the course has taught so far, you write one function for each array type:
function getFirstNumber(arr: number[]): number { return arr[0]; }
.....
.....
.....
Like the course? Get enrolled and start learning!
Reading Progress
0%